wip: fix tests
This commit is contained in:
parent
b4f7ab45ea
commit
bb47510aae
@ -290,23 +290,24 @@ describe('SFC compile <script setup>', () => {
|
||||
|
||||
describe('errors', () => {
|
||||
test('<script> and <script setup> must have same lang', () => {
|
||||
expect(() =>
|
||||
compile(`<script>foo()</script><script setup lang="ts">bar()</script>`)
|
||||
).toThrow(`<script> and <script setup> must have the same language type`)
|
||||
expect(
|
||||
parse(`<script>foo()</script><script setup lang="ts">bar()</script>`)
|
||||
.errors[0].message
|
||||
).toMatch(`<script> and <script setup> must have the same language type`)
|
||||
})
|
||||
|
||||
test('export local as default', () => {
|
||||
expect(() =>
|
||||
compile(`<script setup>
|
||||
expect(
|
||||
parse(`<script setup>
|
||||
const bar = 1
|
||||
export { bar as default }
|
||||
</script>`)
|
||||
).toThrow(`Cannot export locally defined variable as default`)
|
||||
</script>`).errors[0].message
|
||||
).toMatch(`Cannot export locally defined variable as default`)
|
||||
})
|
||||
|
||||
test('export default referencing local var', () => {
|
||||
expect(() =>
|
||||
compile(`<script setup>
|
||||
expect(
|
||||
parse(`<script setup>
|
||||
const bar = 1
|
||||
export default {
|
||||
props: {
|
||||
@ -315,19 +316,19 @@ describe('SFC compile <script setup>', () => {
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>`)
|
||||
).toThrow(`cannot reference locally declared variables`)
|
||||
</script>`).errors[0].message
|
||||
).toMatch(`cannot reference locally declared variables`)
|
||||
})
|
||||
|
||||
test('export default referencing exports', () => {
|
||||
expect(() =>
|
||||
compile(`<script setup>
|
||||
expect(
|
||||
parse(`<script setup>
|
||||
export const bar = 1
|
||||
export default {
|
||||
props: bar
|
||||
}
|
||||
</script>`)
|
||||
).toThrow(`cannot reference locally declared variables`)
|
||||
</script>`).errors[0].message
|
||||
).toMatch(`cannot reference locally declared variables`)
|
||||
})
|
||||
|
||||
test('should allow export default referencing scope var', () => {
|
||||
@ -377,19 +378,19 @@ describe('SFC compile <script setup>', () => {
|
||||
})
|
||||
|
||||
test('error on duplicated defalut export', () => {
|
||||
expect(() =>
|
||||
compile(`
|
||||
expect(
|
||||
parse(`
|
||||
<script>
|
||||
export default {}
|
||||
</script>
|
||||
<script setup>
|
||||
export default {}
|
||||
</script>
|
||||
`)
|
||||
).toThrow(`Default export is already declared`)
|
||||
`).errors[0].message
|
||||
).toMatch(`Default export is already declared`)
|
||||
|
||||
expect(() =>
|
||||
compile(`
|
||||
expect(
|
||||
parse(`
|
||||
<script>
|
||||
export default {}
|
||||
</script>
|
||||
@ -397,33 +398,33 @@ describe('SFC compile <script setup>', () => {
|
||||
const x = {}
|
||||
export { x as default }
|
||||
</script>
|
||||
`)
|
||||
).toThrow(`Default export is already declared`)
|
||||
`).errors[0].message
|
||||
).toMatch(`Default export is already declared`)
|
||||
|
||||
expect(() =>
|
||||
compile(`
|
||||
expect(
|
||||
parse(`
|
||||
<script>
|
||||
export default {}
|
||||
</script>
|
||||
<script setup>
|
||||
export { x as default } from './y'
|
||||
</script>
|
||||
`)
|
||||
).toThrow(`Default export is already declared`)
|
||||
`).errors[0].message
|
||||
).toMatch(`Default export is already declared`)
|
||||
|
||||
expect(() =>
|
||||
compile(`
|
||||
expect(
|
||||
parse(`
|
||||
<script>
|
||||
export { x as default } from './y'
|
||||
</script>
|
||||
<script setup>
|
||||
export default {}
|
||||
</script>
|
||||
`)
|
||||
).toThrow(`Default export is already declared`)
|
||||
`).errors[0].message
|
||||
).toMatch(`Default export is already declared`)
|
||||
|
||||
expect(() =>
|
||||
compile(`
|
||||
expect(
|
||||
parse(`
|
||||
<script>
|
||||
const x = {}
|
||||
export { x as default }
|
||||
@ -431,8 +432,8 @@ describe('SFC compile <script setup>', () => {
|
||||
<script setup>
|
||||
export default {}
|
||||
</script>
|
||||
`)
|
||||
).toThrow(`Default export is already declared`)
|
||||
`).errors[0].message
|
||||
).toMatch(`Default export is already declared`)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -20,6 +20,9 @@ import { walk } from 'estree-walker'
|
||||
import { RawSourceMap } from 'source-map'
|
||||
|
||||
export interface SFCScriptCompileOptions {
|
||||
/**
|
||||
* https://babeljs.io/docs/en/babel-parser#plugins
|
||||
*/
|
||||
babelParserPlugins?: ParserPlugin[]
|
||||
}
|
||||
|
||||
@ -63,6 +66,7 @@ export function compileScript(
|
||||
)
|
||||
}
|
||||
|
||||
const defaultTempVar = `__default__`
|
||||
const bindings: BindingMetadata = {}
|
||||
const imports: Record<string, string> = {}
|
||||
const setupScopeVars: Record<string, boolean> = {}
|
||||
@ -121,7 +125,7 @@ export function compileScript(
|
||||
s.overwrite(
|
||||
start,
|
||||
start + `export default`.length,
|
||||
`const __default__ =`
|
||||
`const ${defaultTempVar} =`
|
||||
)
|
||||
} else if (node.type === 'ExportNamedDeclaration' && node.specifiers) {
|
||||
const defaultSpecifier = node.specifiers.find(
|
||||
@ -146,14 +150,16 @@ export function compileScript(
|
||||
// rewrite to `import { x as __default__ } from './x'` and
|
||||
// add to top
|
||||
s.prepend(
|
||||
`import { ${defaultSpecifier.local.name} as __default__ } from '${
|
||||
node.source.value
|
||||
}'\n`
|
||||
`import { ${
|
||||
defaultSpecifier.local.name
|
||||
} as ${defaultTempVar} } from '${node.source.value}'\n`
|
||||
)
|
||||
} else {
|
||||
// export { x as default }
|
||||
// rewrite to `const __default__ = x` and move to end
|
||||
s.append(`\nconst __default__ = ${defaultSpecifier.local.name}\n`)
|
||||
s.append(
|
||||
`\nconst ${defaultTempVar} = ${defaultSpecifier.local.name}\n`
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -283,7 +289,7 @@ export function compileScript(
|
||||
s.overwrite(
|
||||
specifier.exported.start! + startOffset,
|
||||
specifier.exported.start! + startOffset + 7,
|
||||
'__default__'
|
||||
defaultTempVar
|
||||
)
|
||||
} else if (specifier.type === 'ExportSpecifier') {
|
||||
if (specifier.exported.name === 'default') {
|
||||
@ -319,15 +325,15 @@ export function compileScript(
|
||||
)
|
||||
}
|
||||
// rewrite to `const __default__ = x` and move to end
|
||||
s.append(`\nconst __default__ = ${local}\n`)
|
||||
s.append(`\nconst ${defaultTempVar} = ${local}\n`)
|
||||
} else {
|
||||
// export { x as default } from './x'
|
||||
// rewrite to `import { x as __default__ } from './x'` and
|
||||
// add to top
|
||||
s.prepend(
|
||||
`import { ${specifier.local.name} as __default__ } from '${
|
||||
node.source.value
|
||||
}'\n`
|
||||
`import { ${
|
||||
specifier.local.name
|
||||
} as ${defaultTempVar} } from '${node.source.value}'\n`
|
||||
)
|
||||
}
|
||||
} else {
|
||||
@ -514,7 +520,7 @@ export function compileScript(
|
||||
s.prepend(`import { defineComponent as __define__ } from 'vue'\n`)
|
||||
// we have to use object spread for types to be merged properly
|
||||
// user's TS setting should compile it down to proper targets
|
||||
const def = defaultExport ? `\n ...__default__,` : ``
|
||||
const def = defaultExport ? `\n ...${defaultTempVar},` : ``
|
||||
const runtimeProps = genRuntimeProps(typeDeclaredProps)
|
||||
const runtimeEmits = genRuntimeEmits(typeDeclaredEmits)
|
||||
s.append(
|
||||
@ -522,7 +528,9 @@ export function compileScript(
|
||||
)
|
||||
} else {
|
||||
if (defaultExport) {
|
||||
s.append(`__default__.setup = setup\nexport default __default__`)
|
||||
s.append(
|
||||
`${defaultTempVar}.setup = setup\nexport default ${defaultTempVar}`
|
||||
)
|
||||
} else {
|
||||
s.append(`export default { setup }`)
|
||||
}
|
||||
|
@ -9,16 +9,18 @@ import * as CompilerDOM from '@vue/compiler-dom'
|
||||
import { RawSourceMap, SourceMapGenerator } from 'source-map'
|
||||
import { generateCodeFrame } from '@vue/shared'
|
||||
import { TemplateCompiler } from './compileTemplate'
|
||||
import { compileScript, BindingMetadata } from './compileScript'
|
||||
import { ParserPlugin } from '@babel/parser'
|
||||
import {
|
||||
compileScript,
|
||||
BindingMetadata,
|
||||
SFCScriptCompileOptions
|
||||
} from './compileScript'
|
||||
|
||||
export interface SFCParseOptions {
|
||||
export interface SFCParseOptions extends SFCScriptCompileOptions {
|
||||
filename?: string
|
||||
sourceMap?: boolean
|
||||
sourceRoot?: string
|
||||
pad?: boolean | 'line' | 'space'
|
||||
compiler?: TemplateCompiler
|
||||
babelParserPlugins?: ParserPlugin[]
|
||||
}
|
||||
|
||||
export interface SFCBlock {
|
||||
|
Loading…
x
Reference in New Issue
Block a user