From d89e90067d97eb809060ced4a8ad31756545237b Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Mon, 3 Jan 2022 15:48:41 +0100 Subject: [PATCH] [fix] add injected globals to ts when there's no instance script Fixes #152 Fixes #107 Fixes #152 --- src/preprocess.js | 6 ++++-- test/samples/typescript-$$globals/.eslintrc.js | 8 ++++++++ test/samples/typescript-$$globals/Input.svelte | 3 +++ test/samples/typescript-$$globals/expected.json | 1 + 4 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 test/samples/typescript-$$globals/.eslintrc.js create mode 100644 test/samples/typescript-$$globals/Input.svelte create mode 100644 test/samples/typescript-$$globals/expected.json diff --git a/src/preprocess.js b/src/preprocess.js index 2e6ea6e..a6b170f 100644 --- a/src/preprocess.js +++ b/src/preprocess.js @@ -147,9 +147,11 @@ export const preprocess = text => { if (ast.module) { block.transformed_code += text.slice(ast.module.content.start, ast.module.content.end); } - if (ast.instance) { + if (ast.instance || vars.length) { block.transformed_code += '\n'; - block.transformed_code += vars.filter(v => v.injected).map(v => tsLet(v.name)).join(''); + } + block.transformed_code += vars.filter(v => v.injected).map(v => tsLet(v.name)).join(''); + if (ast.instance) { block.transformed_code += text.slice(ast.instance.content.start, ast.instance.content.end); } } else { diff --git a/test/samples/typescript-$$globals/.eslintrc.js b/test/samples/typescript-$$globals/.eslintrc.js new file mode 100644 index 0000000..dfbee4b --- /dev/null +++ b/test/samples/typescript-$$globals/.eslintrc.js @@ -0,0 +1,8 @@ +module.exports = { + parser: '@typescript-eslint/parser', + extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'], + plugins: ['@typescript-eslint'], + settings: { + 'svelte3/typescript': require('typescript'), + }, +}; diff --git a/test/samples/typescript-$$globals/Input.svelte b/test/samples/typescript-$$globals/Input.svelte new file mode 100644 index 0000000..2ba9538 --- /dev/null +++ b/test/samples/typescript-$$globals/Input.svelte @@ -0,0 +1,3 @@ +{$$slots.foo} +{$$props.bar} +{$$restProps.baz} diff --git a/test/samples/typescript-$$globals/expected.json b/test/samples/typescript-$$globals/expected.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/test/samples/typescript-$$globals/expected.json @@ -0,0 +1 @@ +[]