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