diff --git a/packages/compiler-sfc/__tests__/compileScript.spec.ts b/packages/compiler-sfc/__tests__/compileScript.spec.ts index 01f0d32899b..11f73975207 100644 --- a/packages/compiler-sfc/__tests__/compileScript.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScript.spec.ts @@ -190,6 +190,7 @@ describe('SFC compile + `) + expect(bindings).toStrictEqual({ foo: 'props', bar: 'props' }) + }) + + it('recognizes props object declaration', () => { + const { bindings } = compile(` + + `) + expect(bindings).toStrictEqual({ + foo: 'props', + bar: 'props', + baz: 'props', + qux: 'props' + }) + }) + + it('recognizes setup return', () => { + const { bindings } = compile(` + + `) + expect(bindings).toStrictEqual({ foo: 'setup', bar: 'setup' }) + }) + + it('recognizes async setup return', () => { + const { bindings } = compile(` + + `) + expect(bindings).toStrictEqual({ foo: 'setup', bar: 'setup' }) + }) + + it('recognizes data return', () => { + const { bindings } = compile(` + + `) + expect(bindings).toStrictEqual({ foo: 'data', bar: 'data' }) + }) + + it('recognizes methods', () => { + const { bindings } = compile(` + + `) + expect(bindings).toStrictEqual({ foo: 'options' }) + }) + + it('recognizes computeds', () => { + const { bindings } = compile(` + + `) + expect(bindings).toStrictEqual({ foo: 'options', bar: 'options' }) + }) + + it('recognizes injections array declaration', () => { + const { bindings } = compile(` + + `) + expect(bindings).toStrictEqual({ foo: 'options', bar: 'options' }) + }) + + it('recognizes injections object declaration', () => { + const { bindings } = compile(` + + `) + expect(bindings).toStrictEqual({ foo: 'options', bar: 'options' }) + }) + + it('works for mixed bindings', () => { + const { bindings } = compile(` + + `) + expect(bindings).toStrictEqual({ + foo: 'options', + bar: 'props', + baz: 'setup', + qux: 'data', + quux: 'options', + quuz: 'options' + }) + }) + + it('works for script setup', () => { + const { bindings } = compile(` + + `) + expect(bindings).toStrictEqual({ + foo: 'props' + }) + }) +}) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index a8e378d0f71..1eec51604ad 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -7,6 +7,7 @@ import { Node, Declaration, ObjectPattern, + ObjectExpression, ArrayPattern, Identifier, ExpressionStatement, @@ -16,7 +17,10 @@ import { TSType, TSTypeLiteral, TSFunctionType, - TSDeclareFunction + TSDeclareFunction, + ObjectProperty, + ArrayExpression, + Statement } from '@babel/types' import { walk } from 'estree-walker' import { RawSourceMap } from 'source-map' @@ -56,11 +60,9 @@ export function compileScript( const scriptLang = script && script.lang const scriptSetupLang = scriptSetup && scriptSetup.lang const isTS = scriptLang === 'ts' || scriptSetupLang === 'ts' - const plugins: ParserPlugin[] = [ - ...(options.babelParserPlugins || []), - ...babelParserDefaultPlugins, - ...(isTS ? (['typescript'] as const) : []) - ] + const plugins: ParserPlugin[] = [...babelParserDefaultPlugins] + if (options.babelParserPlugins) plugins.push(...options.babelParserPlugins) + if (isTS) plugins.push('typescript') if (!scriptSetup) { if (!script) { @@ -70,10 +72,15 @@ export function compileScript( // do not process non js/ts script blocks return script } + const scriptAst = parse(script.content, { + plugins, + sourceType: 'module' + }).program.body return { ...script, content: hasCssVars ? injectCssVarsCalls(sfc, plugins) : script.content, - bindings: analyzeScriptBindings(script) + bindings: analyzeScriptBindings(scriptAst), + scriptAst } } @@ -118,15 +125,17 @@ export function compileScript( const scriptStartOffset = script && script.loc.start.offset const scriptEndOffset = script && script.loc.end.offset + let scriptAst + // 1. process normal