diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 92b5b534293..b8afcf9843f 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -1,12 +1,11 @@ -/* eslint-disable no-restricted-globals */ - +const { builtinModules } = require('node:module') const DOMGlobals = ['window', 'document'] const NodeGlobals = ['module', 'require'] const banConstEnum = { selector: 'TSEnumDeclaration[const=true]', message: - 'Please use non-const enums. This project automatically inlines enums.' + 'Please use non-const enums. This project automatically inlines enums.', } /** @@ -15,9 +14,9 @@ const banConstEnum = { module.exports = { parser: '@typescript-eslint/parser', parserOptions: { - sourceType: 'module' + sourceType: 'module', }, - plugins: ['jest'], + plugins: ['jest', 'import', '@typescript-eslint'], rules: { 'no-debugger': 'error', // most of the codebase are expected to be env agnostic @@ -32,8 +31,27 @@ module.exports = { // tsc compiles assignment spread into Object.assign() calls, but esbuild // still generates verbose helpers, so spread assignment is also prohiboted 'ObjectExpression > SpreadElement', - 'AwaitExpression' - ] + 'AwaitExpression', + ], + 'sort-imports': ['error', { ignoreDeclarationSort: true }], + + 'import/no-nodejs-modules': [ + 'error', + { allow: builtinModules.map(mod => `node:${mod}`) }, + ], + // This rule enforces the preference for using '@ts-expect-error' comments in TypeScript + // code to indicate intentional type errors, improving code clarity and maintainability. + '@typescript-eslint/prefer-ts-expect-error': 'error', + // Enforce the use of 'import type' for importing types + '@typescript-eslint/consistent-type-imports': [ + 'error', + { + fixStyle: 'inline-type-imports', + disallowTypeAnnotations: false, + }, + ], + // Enforce the use of top-level import type qualifier when an import only has specifiers with inline type qualifiers + '@typescript-eslint/no-import-type-side-effects': 'error', }, overrides: [ // tests, no restrictions (runs in Node / jest with jsdom) @@ -43,54 +61,66 @@ module.exports = { 'no-restricted-globals': 'off', 'no-restricted-syntax': 'off', 'jest/no-disabled-tests': 'error', - 'jest/no-focused-tests': 'error' - } + 'jest/no-focused-tests': 'error', + }, }, // shared, may be used in any env { - files: ['packages/shared/**'], + files: ['packages/shared/**', '.eslintrc.cjs'], rules: { - 'no-restricted-globals': 'off' - } + 'no-restricted-globals': 'off', + }, }, // Packages targeting DOM { files: ['packages/{vue,vue-compat,runtime-dom}/**'], rules: { - 'no-restricted-globals': ['error', ...NodeGlobals] - } + 'no-restricted-globals': ['error', ...NodeGlobals], + }, }, // Packages targeting Node { files: ['packages/{compiler-sfc,compiler-ssr,server-renderer}/**'], rules: { 'no-restricted-globals': ['error', ...DOMGlobals], - 'no-restricted-syntax': ['error', banConstEnum] - } + 'no-restricted-syntax': ['error', banConstEnum], + }, }, // Private package, browser only + no syntax restrictions { files: ['packages/template-explorer/**', 'packages/sfc-playground/**'], rules: { 'no-restricted-globals': ['error', ...NodeGlobals], - 'no-restricted-syntax': ['error', banConstEnum] - } + 'no-restricted-syntax': ['error', banConstEnum], + }, }, // JavaScript files { files: ['*.js', '*.cjs'], rules: { // We only do `no-unused-vars` checks for js files, TS files are checked by TypeScript itself. - 'no-unused-vars': ['error', { vars: 'all', args: 'none' }] - } + 'no-unused-vars': ['error', { vars: 'all', args: 'none' }], + }, }, // Node scripts { - files: ['scripts/**', '*.{js,ts}', 'packages/**/index.js'], + files: [ + 'scripts/**', + './*.{js,ts}', + 'packages/*/*.js', + 'packages/vue/*/*.js', + ], rules: { 'no-restricted-globals': 'off', - 'no-restricted-syntax': ['error', banConstEnum] - } - } - ] + 'no-restricted-syntax': ['error', banConstEnum], + }, + }, + // Import nodejs modules in compiler-sfc + { + files: ['packages/compiler-sfc/src/**'], + rules: { + 'import/no-nodejs-modules': ['error', { allow: builtinModules }], + }, + }, + ], } diff --git a/.github/renovate.json5 b/.github/renovate.json5 index 088913317c7..a43f4ae30cf 100644 --- a/.github/renovate.json5 +++ b/.github/renovate.json5 @@ -7,35 +7,35 @@ packageRules: [ { depTypeList: ['peerDependencies'], - enabled: false + enabled: false, }, { groupName: 'test', matchPackageNames: ['vitest', 'jsdom', 'puppeteer'], - matchPackagePrefixes: ['@vitest'] + matchPackagePrefixes: ['@vitest'], }, { groupName: 'playground', matchFileNames: [ 'packages/sfc-playground/package.json', - 'packages/template-explorer/package.json' - ] + 'packages/template-explorer/package.json', + ], }, { groupName: 'compiler', matchPackageNames: ['magic-string'], - matchPackagePrefixes: ['@babel', 'postcss'] + matchPackagePrefixes: ['@babel', 'postcss'], }, { groupName: 'build', matchPackageNames: ['vite', 'terser'], - matchPackagePrefixes: ['rollup', 'esbuild', '@rollup', '@vitejs'] + matchPackagePrefixes: ['rollup', 'esbuild', '@rollup', '@vitejs'], }, { groupName: 'lint', matchPackageNames: ['simple-git-hooks', 'lint-staged'], - matchPackagePrefixes: ['@typescript-eslint', 'eslint', 'prettier'] - } + matchPackagePrefixes: ['@typescript-eslint', 'eslint', 'prettier'], + }, ], ignoreDeps: [ 'vue', @@ -45,6 +45,6 @@ 'typescript', // ESM only - 'estree-walker' - ] + 'estree-walker', + ], } diff --git a/.prettierignore b/.prettierignore index 1521c8b7652..fbd3dca8ca3 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1 +1,4 @@ dist +*.md +*.html +pnpm-lock.yaml diff --git a/.prettierrc b/.prettierrc index ef93d94821a..759232e7cf6 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,5 +1,5 @@ -semi: false -singleQuote: true -printWidth: 80 -trailingComma: 'none' -arrowParens: 'avoid' +{ + "semi": false, + "singleQuote": true, + "arrowParens": "avoid" +} diff --git a/.vscode/launch.json b/.vscode/launch.json index b63ffc79b80..b616400b48e 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -21,7 +21,7 @@ "console": "integratedTerminal", "sourceMaps": true, "windows": { - "program": "${workspaceFolder}/node_modules/jest/bin/jest", + "program": "${workspaceFolder}/node_modules/jest/bin/jest" } } ] diff --git a/package.json b/package.json index 0030ef94429..f1f3bee34aa 100644 --- a/package.json +++ b/package.json @@ -13,9 +13,9 @@ "size-esm-runtime": "node scripts/build.js vue -f esm-bundler-runtime", "size-esm": "node scripts/build.js runtime-dom runtime-core reactivity shared -f esm-bundler", "check": "tsc --incremental --noEmit", - "lint": "eslint --cache --ext .ts packages/*/{src,__tests__}/**.ts", - "format": "prettier --write --cache \"**/*.[tj]s?(x)\"", - "format-check": "prettier --check --cache \"**/*.[tj]s?(x)\"", + "lint": "eslint --cache --ext .js,.ts,.tsx .", + "format": "prettier --write --cache .", + "format-check": "prettier --check --cache .", "test": "vitest", "test-unit": "vitest -c vitest.unit.config.ts", "test-e2e": "node scripts/build.js vue -f global -d && vitest -c vitest.e2e.config.ts", @@ -72,6 +72,7 @@ "@types/minimist": "^1.2.5", "@types/node": "^20.10.5", "@types/semver": "^7.5.5", + "@typescript-eslint/eslint-plugin": "^6.16.0", "@typescript-eslint/parser": "^6.15.0", "@vitest/coverage-istanbul": "^1.1.0", "@vue/consolidate": "0.17.3", @@ -81,6 +82,7 @@ "esbuild-plugin-polyfill-node": "^0.3.0", "eslint": "^8.56.0", "eslint-define-config": "^1.24.1", + "eslint-plugin-import": "npm:eslint-plugin-i@^2.29.1", "eslint-plugin-jest": "^27.6.0", "estree-walker": "^2.0.2", "execa": "^8.0.1", diff --git a/packages/compiler-core/__tests__/codegen.spec.ts b/packages/compiler-core/__tests__/codegen.spec.ts index 4938e758546..9c923075634 100644 --- a/packages/compiler-core/__tests__/codegen.spec.ts +++ b/packages/compiler-core/__tests__/codegen.spec.ts @@ -1,38 +1,38 @@ import { - locStub, - generate, + ConstantTypes, + type DirectiveArguments, + type ForCodegenNode, + type IfConditionalExpression, NodeTypes, - RootNode, - createSimpleExpression, - createObjectExpression, - createObjectProperty, + type RootNode, + type VNodeCall, createArrayExpression, - createCompoundExpression, - createInterpolation, + createAssignmentExpression, + createBlockStatement, + createCacheExpression, createCallExpression, + createCompoundExpression, createConditionalExpression, - ForCodegenNode, - createCacheExpression, - createTemplateLiteral, - createBlockStatement, createIfStatement, - createAssignmentExpression, - IfConditionalExpression, + createInterpolation, + createObjectExpression, + createObjectProperty, + createSimpleExpression, + createTemplateLiteral, createVNodeCall, - VNodeCall, - DirectiveArguments, - ConstantTypes + generate, + locStub, } from '../src' import { - CREATE_VNODE, - TO_DISPLAY_STRING, - RESOLVE_DIRECTIVE, - helperNameMap, - RESOLVE_COMPONENT, CREATE_COMMENT, + CREATE_ELEMENT_VNODE, + CREATE_VNODE, FRAGMENT, RENDER_LIST, - CREATE_ELEMENT_VNODE + RESOLVE_COMPONENT, + RESOLVE_DIRECTIVE, + TO_DISPLAY_STRING, + helperNameMap, } from '../src/runtimeHelpers' import { createElementWithCodegen, genFlagText } from './testUtils' import { PatchFlags } from '@vue/shared' @@ -51,59 +51,59 @@ function createRoot(options: Partial = {}): RootNode { temps: 0, codegenNode: createSimpleExpression(`null`, false), loc: locStub, - ...options + ...options, } } describe('compiler: codegen', () => { test('module mode preamble', () => { const root = createRoot({ - helpers: new Set([CREATE_VNODE, RESOLVE_DIRECTIVE]) + helpers: new Set([CREATE_VNODE, RESOLVE_DIRECTIVE]), }) const { code } = generate(root, { mode: 'module' }) expect(code).toMatch( - `import { ${helperNameMap[CREATE_VNODE]} as _${helperNameMap[CREATE_VNODE]}, ${helperNameMap[RESOLVE_DIRECTIVE]} as _${helperNameMap[RESOLVE_DIRECTIVE]} } from "vue"` + `import { ${helperNameMap[CREATE_VNODE]} as _${helperNameMap[CREATE_VNODE]}, ${helperNameMap[RESOLVE_DIRECTIVE]} as _${helperNameMap[RESOLVE_DIRECTIVE]} } from "vue"`, ) expect(code).toMatchSnapshot() }) test('module mode preamble w/ optimizeImports: true', () => { const root = createRoot({ - helpers: new Set([CREATE_VNODE, RESOLVE_DIRECTIVE]) + helpers: new Set([CREATE_VNODE, RESOLVE_DIRECTIVE]), }) const { code } = generate(root, { mode: 'module', optimizeImports: true }) expect(code).toMatch( - `import { ${helperNameMap[CREATE_VNODE]}, ${helperNameMap[RESOLVE_DIRECTIVE]} } from "vue"` + `import { ${helperNameMap[CREATE_VNODE]}, ${helperNameMap[RESOLVE_DIRECTIVE]} } from "vue"`, ) expect(code).toMatch( - `const _${helperNameMap[CREATE_VNODE]} = ${helperNameMap[CREATE_VNODE]}, _${helperNameMap[RESOLVE_DIRECTIVE]} = ${helperNameMap[RESOLVE_DIRECTIVE]}` + `const _${helperNameMap[CREATE_VNODE]} = ${helperNameMap[CREATE_VNODE]}, _${helperNameMap[RESOLVE_DIRECTIVE]} = ${helperNameMap[RESOLVE_DIRECTIVE]}`, ) expect(code).toMatchSnapshot() }) test('function mode preamble', () => { const root = createRoot({ - helpers: new Set([CREATE_VNODE, RESOLVE_DIRECTIVE]) + helpers: new Set([CREATE_VNODE, RESOLVE_DIRECTIVE]), }) const { code } = generate(root, { mode: 'function' }) expect(code).toMatch(`const _Vue = Vue`) expect(code).toMatch( - `const { ${helperNameMap[CREATE_VNODE]}: _${helperNameMap[CREATE_VNODE]}, ${helperNameMap[RESOLVE_DIRECTIVE]}: _${helperNameMap[RESOLVE_DIRECTIVE]} } = _Vue` + `const { ${helperNameMap[CREATE_VNODE]}: _${helperNameMap[CREATE_VNODE]}, ${helperNameMap[RESOLVE_DIRECTIVE]}: _${helperNameMap[RESOLVE_DIRECTIVE]} } = _Vue`, ) expect(code).toMatchSnapshot() }) test('function mode preamble w/ prefixIdentifiers: true', () => { const root = createRoot({ - helpers: new Set([CREATE_VNODE, RESOLVE_DIRECTIVE]) + helpers: new Set([CREATE_VNODE, RESOLVE_DIRECTIVE]), }) const { code } = generate(root, { mode: 'function', - prefixIdentifiers: true + prefixIdentifiers: true, }) expect(code).not.toMatch(`const _Vue = Vue`) expect(code).toMatch( - `const { ${helperNameMap[CREATE_VNODE]}: _${helperNameMap[CREATE_VNODE]}, ${helperNameMap[RESOLVE_DIRECTIVE]}: _${helperNameMap[RESOLVE_DIRECTIVE]} } = Vue` + `const { ${helperNameMap[CREATE_VNODE]}: _${helperNameMap[CREATE_VNODE]}, ${helperNameMap[RESOLVE_DIRECTIVE]}: _${helperNameMap[RESOLVE_DIRECTIVE]} } = Vue`, ) expect(code).toMatchSnapshot() }) @@ -112,27 +112,27 @@ describe('compiler: codegen', () => { const root = createRoot({ components: [`Foo`, `bar-baz`, `barbaz`, `Qux__self`], directives: [`my_dir_0`, `my_dir_1`], - temps: 3 + temps: 3, }) const { code } = generate(root, { mode: 'function' }) expect(code).toMatch( - `const _component_Foo = _${helperNameMap[RESOLVE_COMPONENT]}("Foo")\n` + `const _component_Foo = _${helperNameMap[RESOLVE_COMPONENT]}("Foo")\n`, ) expect(code).toMatch( - `const _component_bar_baz = _${helperNameMap[RESOLVE_COMPONENT]}("bar-baz")\n` + `const _component_bar_baz = _${helperNameMap[RESOLVE_COMPONENT]}("bar-baz")\n`, ) expect(code).toMatch( - `const _component_barbaz = _${helperNameMap[RESOLVE_COMPONENT]}("barbaz")\n` + `const _component_barbaz = _${helperNameMap[RESOLVE_COMPONENT]}("barbaz")\n`, ) // implicit self reference from SFC filename expect(code).toMatch( - `const _component_Qux = _${helperNameMap[RESOLVE_COMPONENT]}("Qux", true)\n` + `const _component_Qux = _${helperNameMap[RESOLVE_COMPONENT]}("Qux", true)\n`, ) expect(code).toMatch( - `const _directive_my_dir_0 = _${helperNameMap[RESOLVE_DIRECTIVE]}("my_dir_0")\n` + `const _directive_my_dir_0 = _${helperNameMap[RESOLVE_DIRECTIVE]}("my_dir_0")\n`, ) expect(code).toMatch( - `const _directive_my_dir_1 = _${helperNameMap[RESOLVE_DIRECTIVE]}("my_dir_1")\n` + `const _directive_my_dir_1 = _${helperNameMap[RESOLVE_DIRECTIVE]}("my_dir_1")\n`, ) expect(code).toMatch(`let _temp0, _temp1, _temp2`) expect(code).toMatchSnapshot() @@ -146,12 +146,12 @@ describe('compiler: codegen', () => { [ createObjectProperty( createSimpleExpression(`id`, true, locStub), - createSimpleExpression(`foo`, true, locStub) - ) + createSimpleExpression(`foo`, true, locStub), + ), ], - locStub - ) - ] + locStub, + ), + ], }) const { code } = generate(root) expect(code).toMatch(`const _hoisted_1 = hello`) @@ -161,7 +161,7 @@ describe('compiler: codegen', () => { test('temps', () => { const root = createRoot({ - temps: 3 + temps: 3, }) const { code } = generate(root) expect(code).toMatch(`let _temp0, _temp1, _temp2`) @@ -174,9 +174,9 @@ describe('compiler: codegen', () => { codegenNode: { type: NodeTypes.TEXT, content: 'hello', - loc: locStub - } - }) + loc: locStub, + }, + }), ) expect(code).toMatch(`return "hello"`) expect(code).toMatchSnapshot() @@ -185,8 +185,8 @@ describe('compiler: codegen', () => { test('interpolation', () => { const { code } = generate( createRoot({ - codegenNode: createInterpolation(`hello`, locStub) - }) + codegenNode: createInterpolation(`hello`, locStub), + }), ) expect(code).toMatch(`return _${helperNameMap[TO_DISPLAY_STRING]}(hello)`) expect(code).toMatchSnapshot() @@ -198,9 +198,9 @@ describe('compiler: codegen', () => { codegenNode: { type: NodeTypes.COMMENT, content: 'foo', - loc: locStub - } - }) + loc: locStub, + }, + }), ) expect(code).toMatch(`return _${helperNameMap[CREATE_COMMENT]}("foo")`) expect(code).toMatchSnapshot() @@ -216,15 +216,15 @@ describe('compiler: codegen', () => { { type: NodeTypes.INTERPOLATION, loc: locStub, - content: createSimpleExpression(`bar`, false, locStub) + content: createSimpleExpression(`bar`, false, locStub), }, // nested compound - createCompoundExpression([` + `, `nested`]) - ]) - }) + createCompoundExpression([` + `, `nested`]), + ]), + }), ) expect(code).toMatch( - `return _ctx.foo + _${helperNameMap[TO_DISPLAY_STRING]}(bar) + nested` + `return _ctx.foo + _${helperNameMap[TO_DISPLAY_STRING]}(bar) + nested`, ) expect(code).toMatchSnapshot() }) @@ -239,10 +239,10 @@ describe('compiler: codegen', () => { codegenNode: createConditionalExpression( createSimpleExpression('foo', false), createSimpleExpression('bar', false), - createSimpleExpression('baz', false) - ) as IfConditionalExpression - } - }) + createSimpleExpression('baz', false), + ) as IfConditionalExpression, + }, + }), ) expect(code).toMatch(/return foo\s+\? bar\s+: baz/) expect(code).toMatchSnapshot() @@ -270,10 +270,10 @@ describe('compiler: codegen', () => { patchFlag: '1', dynamicProps: undefined, directives: undefined, - loc: locStub - } as ForCodegenNode - } - }) + loc: locStub, + } as ForCodegenNode, + }, + }), ) expect(code).toMatch(`openBlock(true)`) expect(code).toMatchSnapshot() @@ -289,7 +289,7 @@ describe('compiler: codegen', () => { '1 + 2', false, locStub, - ConstantTypes.CAN_STRINGIFY + ConstantTypes.CAN_STRINGIFY, ), valueAlias: undefined, keyAlias: undefined, @@ -306,10 +306,10 @@ describe('compiler: codegen', () => { patchFlag: genFlagText(PatchFlags.STABLE_FRAGMENT), dynamicProps: undefined, directives: undefined, - loc: locStub - } as ForCodegenNode - } - }) + loc: locStub, + } as ForCodegenNode, + }, + }), ) expect(code).toMatch(`openBlock()`) expect(code).toMatchSnapshot() @@ -326,11 +326,11 @@ describe('compiler: codegen', () => { [ createObjectProperty( createSimpleExpression(`id`, true, locStub), - createSimpleExpression(`foo`, true, locStub) + createSimpleExpression(`foo`, true, locStub), ), createObjectProperty( createSimpleExpression(`prop`, false, locStub), - createSimpleExpression(`bar`, false, locStub) + createSimpleExpression(`bar`, false, locStub), ), // compound expression as computed key createObjectProperty( @@ -339,13 +339,13 @@ describe('compiler: codegen', () => { loc: locStub, children: [ `foo + `, - createSimpleExpression(`bar`, false, locStub) - ] + createSimpleExpression(`bar`, false, locStub), + ], }, - createSimpleExpression(`bar`, false, locStub) - ) + createSimpleExpression(`bar`, false, locStub), + ), ], - locStub + locStub, ), // ChildNode[] [ @@ -356,17 +356,17 @@ describe('compiler: codegen', () => { createObjectProperty( // should quote the key! createSimpleExpression(`some-key`, true, locStub), - createSimpleExpression(`foo`, true, locStub) - ) + createSimpleExpression(`foo`, true, locStub), + ), ], - locStub - ) - ) + locStub, + ), + ), ], // flag - PatchFlags.FULL_PROPS + '' - ) - }) + PatchFlags.FULL_PROPS + '', + ), + }), ) expect(code).toMatch(` return _${helperNameMap[CREATE_ELEMENT_VNODE]}("div", { @@ -384,9 +384,9 @@ describe('compiler: codegen', () => { createRoot({ codegenNode: createArrayExpression([ createSimpleExpression(`foo`, false), - createCallExpression(`bar`, [`baz`]) - ]) - }) + createCallExpression(`bar`, [`baz`]), + ]), + }), ) expect(code).toMatch(`return [ foo, @@ -404,17 +404,17 @@ describe('compiler: codegen', () => { createConditionalExpression( createSimpleExpression(`orNot`, false), createCallExpression(`bar`), - createCallExpression(`baz`) - ) - ) - }) + createCallExpression(`baz`), + ), + ), + }), ) expect(code).toMatch( `return ok ? foo() : orNot ? bar() - : baz()` + : baz()`, ) expect(code).toMatchSnapshot() }) @@ -425,13 +425,13 @@ describe('compiler: codegen', () => { cached: 1, codegenNode: createCacheExpression( 1, - createSimpleExpression(`foo`, false) - ) + createSimpleExpression(`foo`, false), + ), }), { mode: 'module', - prefixIdentifiers: true - } + prefixIdentifiers: true, + }, ) expect(code).toMatch(`_cache[1] || (_cache[1] = foo)`) expect(code).toMatchSnapshot() @@ -444,13 +444,13 @@ describe('compiler: codegen', () => { codegenNode: createCacheExpression( 1, createSimpleExpression(`foo`, false), - true - ) + true, + ), }), { mode: 'module', - prefixIdentifiers: true - } + prefixIdentifiers: true, + }, ) expect(code).toMatch( ` @@ -460,7 +460,7 @@ describe('compiler: codegen', () => { _setBlockTracking(1), _cache[1] ) - `.trim() + `.trim(), ) expect(code).toMatchSnapshot() }) @@ -472,11 +472,11 @@ describe('compiler: codegen', () => { createTemplateLiteral([ `foo`, createCallExpression(`_renderAttr`, ['id', 'foo']), - `bar` - ]) - ]) + `bar`, + ]), + ]), }), - { ssr: true, mode: 'module' } + { ssr: true, mode: 'module' }, ) expect(code).toMatchInlineSnapshot(` " @@ -493,11 +493,11 @@ describe('compiler: codegen', () => { codegenNode: createBlockStatement([ createIfStatement( createSimpleExpression('foo', false), - createBlockStatement([createCallExpression(`ok`)]) - ) - ]) + createBlockStatement([createCallExpression(`ok`)]), + ), + ]), }), - { ssr: true, mode: 'module' } + { ssr: true, mode: 'module' }, ) expect(code).toMatchInlineSnapshot(` " @@ -516,11 +516,11 @@ describe('compiler: codegen', () => { createIfStatement( createSimpleExpression('foo', false), createBlockStatement([createCallExpression(`foo`)]), - createBlockStatement([createCallExpression('bar')]) - ) - ]) + createBlockStatement([createCallExpression('bar')]), + ), + ]), }), - { ssr: true, mode: 'module' } + { ssr: true, mode: 'module' }, ) expect(code).toMatchInlineSnapshot(` " @@ -543,12 +543,12 @@ describe('compiler: codegen', () => { createBlockStatement([createCallExpression(`foo`)]), createIfStatement( createSimpleExpression('bar', false), - createBlockStatement([createCallExpression(`bar`)]) - ) - ) - ]) + createBlockStatement([createCallExpression(`bar`)]), + ), + ), + ]), }), - { ssr: true, mode: 'module' } + { ssr: true, mode: 'module' }, ) expect(code).toMatchInlineSnapshot(` " @@ -572,12 +572,12 @@ describe('compiler: codegen', () => { createIfStatement( createSimpleExpression('bar', false), createBlockStatement([createCallExpression(`bar`)]), - createBlockStatement([createCallExpression('baz')]) - ) - ) - ]) + createBlockStatement([createCallExpression('baz')]), + ), + ), + ]), }), - { ssr: true, mode: 'module' } + { ssr: true, mode: 'module' }, ) expect(code).toMatchInlineSnapshot(` " @@ -599,9 +599,9 @@ describe('compiler: codegen', () => { createRoot({ codegenNode: createAssignmentExpression( createSimpleExpression(`foo`, false), - createSimpleExpression(`bar`, false) - ) - }) + createSimpleExpression(`bar`, false), + ), + }), ) expect(code).toMatchInlineSnapshot(` " @@ -617,17 +617,17 @@ describe('compiler: codegen', () => { function genCode(node: VNodeCall) { return generate( createRoot({ - codegenNode: node - }) + codegenNode: node, + }), ).code.match(/with \(_ctx\) \{\s+([^]+)\s+\}\s+\}$/)![1] } const mockProps = createObjectExpression([ - createObjectProperty(`foo`, createSimpleExpression(`bar`, true)) + createObjectProperty(`foo`, createSimpleExpression(`bar`, true)), ]) const mockChildren = createCompoundExpression(['children']) const mockDirs = createArrayExpression([ - createArrayExpression([`foo`, createSimpleExpression(`bar`, false)]) + createArrayExpression([`foo`, createSimpleExpression(`bar`, false)]), ]) as DirectiveArguments test('tag only', () => { @@ -684,9 +684,9 @@ describe('compiler: codegen', () => { undefined, undefined, undefined, - true - ) - ) + true, + ), + ), ).toMatchInlineSnapshot(` "return (_openBlock(), _createElementBlock("div", { foo: "bar" }, children)) " @@ -705,9 +705,9 @@ describe('compiler: codegen', () => { undefined, undefined, true, - true - ) - ) + true, + ), + ), ).toMatchInlineSnapshot(` "return (_openBlock(true), _createElementBlock("div", { foo: "bar" }, children)) " @@ -724,9 +724,9 @@ describe('compiler: codegen', () => { mockChildren, undefined, undefined, - mockDirs - ) - ) + mockDirs, + ), + ), ).toMatchInlineSnapshot(` "return _withDirectives(_createElementVNode("div", { foo: "bar" }, children), [ [foo, bar] @@ -746,9 +746,9 @@ describe('compiler: codegen', () => { undefined, undefined, mockDirs, - true - ) - ) + true, + ), + ), ).toMatchInlineSnapshot(` "return _withDirectives((_openBlock(), _createElementBlock("div", { foo: "bar" }, children)), [ [foo, bar] diff --git a/packages/compiler-core/__tests__/compile.spec.ts b/packages/compiler-core/__tests__/compile.spec.ts index dc4f57ad3d2..995741091df 100644 --- a/packages/compiler-core/__tests__/compile.spec.ts +++ b/packages/compiler-core/__tests__/compile.spec.ts @@ -1,5 +1,5 @@ import { baseCompile as compile } from '../src' -import { SourceMapConsumer, RawSourceMap } from 'source-map-js' +import { type RawSourceMap, SourceMapConsumer } from 'source-map-js' describe('compiler: integration tests', () => { const source = ` @@ -20,7 +20,7 @@ describe('compiler: integration tests', () => { function getPositionInCode( code: string, token: string, - expectName: string | boolean = false + expectName: string | boolean = false, ): Pos { const generatedOffset = code.indexOf(token) let line = 1 @@ -36,7 +36,7 @@ describe('compiler: integration tests', () => { column: lastNewLinePos === -1 ? generatedOffset - : generatedOffset - lastNewLinePos - 1 + : generatedOffset - lastNewLinePos - 1, } if (expectName) { res.name = typeof expectName === 'string' ? expectName : token @@ -47,7 +47,7 @@ describe('compiler: integration tests', () => { test('function mode', () => { const { code, map } = compile(source, { sourceMap: true, - filename: `foo.vue` + filename: `foo.vue`, }) expect(code).toMatchSnapshot() @@ -57,55 +57,55 @@ describe('compiler: integration tests', () => { const consumer = new SourceMapConsumer(map as RawSourceMap) expect( - consumer.originalPositionFor(getPositionInCode(code, `id`)) + consumer.originalPositionFor(getPositionInCode(code, `id`)), ).toMatchObject(getPositionInCode(source, `id`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `"foo"`)) + consumer.originalPositionFor(getPositionInCode(code, `"foo"`)), ).toMatchObject(getPositionInCode(source, `"foo"`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `class:`)) + consumer.originalPositionFor(getPositionInCode(code, `class:`)), ).toMatchObject(getPositionInCode(source, `class=`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `bar`)) + consumer.originalPositionFor(getPositionInCode(code, `bar`)), ).toMatchObject(getPositionInCode(source, `bar`)) // without prefixIdentifiers: true, identifiers inside compound expressions // are mapped to closest parent expression. expect( - consumer.originalPositionFor(getPositionInCode(code, `baz`)) + consumer.originalPositionFor(getPositionInCode(code, `baz`)), ).toMatchObject(getPositionInCode(source, `bar`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `world`)) + consumer.originalPositionFor(getPositionInCode(code, `world`)), ).toMatchObject(getPositionInCode(source, `world`)) // without prefixIdentifiers: true, identifiers inside compound expressions // are mapped to closest parent expression. expect( - consumer.originalPositionFor(getPositionInCode(code, `burn()`)) + consumer.originalPositionFor(getPositionInCode(code, `burn()`)), ).toMatchObject(getPositionInCode(source, `world`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `ok`)) + consumer.originalPositionFor(getPositionInCode(code, `ok`)), ).toMatchObject(getPositionInCode(source, `ok`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `list`)) + consumer.originalPositionFor(getPositionInCode(code, `list`)), ).toMatchObject(getPositionInCode(source, `list`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `value`)) + consumer.originalPositionFor(getPositionInCode(code, `value`)), ).toMatchObject(getPositionInCode(source, `value`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `index`)) + consumer.originalPositionFor(getPositionInCode(code, `index`)), ).toMatchObject(getPositionInCode(source, `index`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `value + index`)) + consumer.originalPositionFor(getPositionInCode(code, `value + index`)), ).toMatchObject(getPositionInCode(source, `value + index`)) }) @@ -113,7 +113,7 @@ describe('compiler: integration tests', () => { const { code, map } = compile(source, { sourceMap: true, filename: `foo.vue`, - prefixIdentifiers: true + prefixIdentifiers: true, }) expect(code).toMatchSnapshot() @@ -123,64 +123,66 @@ describe('compiler: integration tests', () => { const consumer = new SourceMapConsumer(map as RawSourceMap) expect( - consumer.originalPositionFor(getPositionInCode(code, `id`)) + consumer.originalPositionFor(getPositionInCode(code, `id`)), ).toMatchObject(getPositionInCode(source, `id`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `"foo"`)) + consumer.originalPositionFor(getPositionInCode(code, `"foo"`)), ).toMatchObject(getPositionInCode(source, `"foo"`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `class:`)) + consumer.originalPositionFor(getPositionInCode(code, `class:`)), ).toMatchObject(getPositionInCode(source, `class=`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `bar`)) + consumer.originalPositionFor(getPositionInCode(code, `bar`)), ).toMatchObject(getPositionInCode(source, `bar`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `_ctx.bar`, `bar`)) + consumer.originalPositionFor(getPositionInCode(code, `_ctx.bar`, `bar`)), ).toMatchObject(getPositionInCode(source, `bar`, true)) expect( - consumer.originalPositionFor(getPositionInCode(code, `baz`)) + consumer.originalPositionFor(getPositionInCode(code, `baz`)), ).toMatchObject(getPositionInCode(source, `baz`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `world`, true)) + consumer.originalPositionFor(getPositionInCode(code, `world`, true)), ).toMatchObject(getPositionInCode(source, `world`, `world`)) expect( consumer.originalPositionFor( - getPositionInCode(code, `_ctx.world`, `world`) - ) + getPositionInCode(code, `_ctx.world`, `world`), + ), ).toMatchObject(getPositionInCode(source, `world`, `world`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `burn()`)) + consumer.originalPositionFor(getPositionInCode(code, `burn()`)), ).toMatchObject(getPositionInCode(source, `burn()`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `ok`)) + consumer.originalPositionFor(getPositionInCode(code, `ok`)), ).toMatchObject(getPositionInCode(source, `ok`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `_ctx.ok`, `ok`)) + consumer.originalPositionFor(getPositionInCode(code, `_ctx.ok`, `ok`)), ).toMatchObject(getPositionInCode(source, `ok`, true)) expect( - consumer.originalPositionFor(getPositionInCode(code, `list`)) + consumer.originalPositionFor(getPositionInCode(code, `list`)), ).toMatchObject(getPositionInCode(source, `list`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `_ctx.list`, `list`)) + consumer.originalPositionFor( + getPositionInCode(code, `_ctx.list`, `list`), + ), ).toMatchObject(getPositionInCode(source, `list`, true)) expect( - consumer.originalPositionFor(getPositionInCode(code, `value`)) + consumer.originalPositionFor(getPositionInCode(code, `value`)), ).toMatchObject(getPositionInCode(source, `value`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `index`)) + consumer.originalPositionFor(getPositionInCode(code, `index`)), ).toMatchObject(getPositionInCode(source, `index`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `value + index`)) + consumer.originalPositionFor(getPositionInCode(code, `value + index`)), ).toMatchObject(getPositionInCode(source, `value + index`)) }) @@ -188,7 +190,7 @@ describe('compiler: integration tests', () => { const { code, map } = compile(source, { mode: 'module', sourceMap: true, - filename: `foo.vue` + filename: `foo.vue`, }) expect(code).toMatchSnapshot() @@ -198,64 +200,66 @@ describe('compiler: integration tests', () => { const consumer = new SourceMapConsumer(map as RawSourceMap) expect( - consumer.originalPositionFor(getPositionInCode(code, `id`)) + consumer.originalPositionFor(getPositionInCode(code, `id`)), ).toMatchObject(getPositionInCode(source, `id`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `"foo"`)) + consumer.originalPositionFor(getPositionInCode(code, `"foo"`)), ).toMatchObject(getPositionInCode(source, `"foo"`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `class:`)) + consumer.originalPositionFor(getPositionInCode(code, `class:`)), ).toMatchObject(getPositionInCode(source, `class=`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `bar`)) + consumer.originalPositionFor(getPositionInCode(code, `bar`)), ).toMatchObject(getPositionInCode(source, `bar`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `_ctx.bar`, `bar`)) + consumer.originalPositionFor(getPositionInCode(code, `_ctx.bar`, `bar`)), ).toMatchObject(getPositionInCode(source, `bar`, true)) expect( - consumer.originalPositionFor(getPositionInCode(code, `baz`)) + consumer.originalPositionFor(getPositionInCode(code, `baz`)), ).toMatchObject(getPositionInCode(source, `baz`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `world`, true)) + consumer.originalPositionFor(getPositionInCode(code, `world`, true)), ).toMatchObject(getPositionInCode(source, `world`, `world`)) expect( consumer.originalPositionFor( - getPositionInCode(code, `_ctx.world`, `world`) - ) + getPositionInCode(code, `_ctx.world`, `world`), + ), ).toMatchObject(getPositionInCode(source, `world`, `world`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `burn()`)) + consumer.originalPositionFor(getPositionInCode(code, `burn()`)), ).toMatchObject(getPositionInCode(source, `burn()`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `ok`)) + consumer.originalPositionFor(getPositionInCode(code, `ok`)), ).toMatchObject(getPositionInCode(source, `ok`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `_ctx.ok`, `ok`)) + consumer.originalPositionFor(getPositionInCode(code, `_ctx.ok`, `ok`)), ).toMatchObject(getPositionInCode(source, `ok`, true)) expect( - consumer.originalPositionFor(getPositionInCode(code, `list`)) + consumer.originalPositionFor(getPositionInCode(code, `list`)), ).toMatchObject(getPositionInCode(source, `list`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `_ctx.list`, `list`)) + consumer.originalPositionFor( + getPositionInCode(code, `_ctx.list`, `list`), + ), ).toMatchObject(getPositionInCode(source, `list`, true)) expect( - consumer.originalPositionFor(getPositionInCode(code, `value`)) + consumer.originalPositionFor(getPositionInCode(code, `value`)), ).toMatchObject(getPositionInCode(source, `value`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `index`)) + consumer.originalPositionFor(getPositionInCode(code, `index`)), ).toMatchObject(getPositionInCode(source, `index`)) expect( - consumer.originalPositionFor(getPositionInCode(code, `value + index`)) + consumer.originalPositionFor(getPositionInCode(code, `value + index`)), ).toMatchObject(getPositionInCode(source, `value + index`)) }) }) diff --git a/packages/compiler-core/__tests__/parse.spec.ts b/packages/compiler-core/__tests__/parse.spec.ts index e49ce2bd4ce..d5bdb4bc5fd 100644 --- a/packages/compiler-core/__tests__/parse.spec.ts +++ b/packages/compiler-core/__tests__/parse.spec.ts @@ -1,20 +1,20 @@ -import { ParserOptions } from '../src/options' +import type { ParserOptions } from '../src/options' import { ErrorCodes } from '../src/errors' import { - CommentNode, - ElementNode, + type CommentNode, + ConstantTypes, + type DirectiveNode, + type ElementNode, ElementTypes, + type InterpolationNode, Namespaces, NodeTypes, - Position, - TextNode, - InterpolationNode, - ConstantTypes, - DirectiveNode + type Position, + type TextNode, } from '../src/ast' import { baseParse } from '../src/parser' -import { Program } from '@babel/types' +import type { Program } from '@babel/types' /* eslint jest/no-disabled-tests: "off" */ @@ -30,8 +30,8 @@ describe('compiler: parse', () => { loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 9, line: 1, column: 10 }, - source: 'some text' - } + source: 'some text', + }, }) }) @@ -46,10 +46,10 @@ describe('compiler: parse', () => { code: ErrorCodes.X_INVALID_END_TAG, loc: { start: { column: 10, line: 1, offset: 9 }, - end: { column: 10, line: 1, offset: 9 } - } - } - ] + end: { column: 10, line: 1, offset: 9 }, + }, + }, + ], ]) expect(text).toStrictEqual({ @@ -58,8 +58,8 @@ describe('compiler: parse', () => { loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 9, line: 1, column: 10 }, - source: 'some text' - } + source: 'some text', + }, }) }) @@ -74,8 +74,8 @@ describe('compiler: parse', () => { loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 5, line: 1, column: 6 }, - source: 'some ' - } + source: 'some ', + }, }) expect(text2).toStrictEqual({ type: NodeTypes.TEXT, @@ -83,8 +83,8 @@ describe('compiler: parse', () => { loc: { start: { offset: 20, line: 1, column: 21 }, end: { offset: 25, line: 1, column: 26 }, - source: ' text' - } + source: ' text', + }, }) }) @@ -99,8 +99,8 @@ describe('compiler: parse', () => { loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 5, line: 1, column: 6 }, - source: 'some ' - } + source: 'some ', + }, }) expect(text2).toStrictEqual({ type: NodeTypes.TEXT, @@ -108,8 +108,8 @@ describe('compiler: parse', () => { loc: { start: { offset: 21, line: 1, column: 22 }, end: { offset: 26, line: 1, column: 27 }, - source: ' text' - } + source: ' text', + }, }) }) @@ -124,8 +124,8 @@ describe('compiler: parse', () => { loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 5, line: 1, column: 6 }, - source: 'some ' - } + source: 'some ', + }, }) expect(text2).toStrictEqual({ type: NodeTypes.TEXT, @@ -133,8 +133,8 @@ describe('compiler: parse', () => { loc: { start: { offset: 32, line: 1, column: 33 }, end: { offset: 37, line: 1, column: 38 }, - source: ' text' - } + source: ' text', + }, }) }) @@ -144,7 +144,7 @@ describe('compiler: parse', () => { if (err.code !== ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME) { throw err } - } + }, }) const text = ast.children[0] as TextNode @@ -154,8 +154,8 @@ describe('compiler: parse', () => { loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 5, line: 1, column: 6 }, - source: 'a < b' - } + source: 'a < b', + }, }) }) @@ -165,7 +165,7 @@ describe('compiler: parse', () => { if (error.code !== ErrorCodes.X_MISSING_INTERPOLATION_END) { throw error } - } + }, }) const text = ast.children[0] as TextNode @@ -175,8 +175,8 @@ describe('compiler: parse', () => { loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 6, line: 1, column: 7 }, - source: 'a {{ b' - } + source: 'a {{ b', + }, }) }) }) @@ -196,14 +196,14 @@ describe('compiler: parse', () => { loc: { start: { offset: 2, line: 1, column: 3 }, end: { offset: 9, line: 1, column: 10 }, - source: 'message' - } + source: 'message', + }, }, loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 11, line: 1, column: 12 }, - source: '{{message}}' - } + source: '{{message}}', + }, }) }) @@ -221,14 +221,14 @@ describe('compiler: parse', () => { loc: { start: { offset: 3, line: 1, column: 4 }, end: { offset: 6, line: 1, column: 7 }, - source: 'a { loc: { start: { offset: 3, line: 1, column: 4 }, end: { offset: 6, line: 1, column: 7 }, - source: 'a { loc: { start: { offset: 12, line: 1, column: 13 }, end: { offset: 15, line: 1, column: 16 }, - source: 'c>d' - } + source: 'c>d', + }, }, loc: { start: { offset: 9, line: 1, column: 10 }, end: { offset: 18, line: 1, column: 19 }, - source: '{{ c>d }}' - } + source: '{{ c>d }}', + }, }) }) @@ -294,20 +294,20 @@ describe('compiler: parse', () => { loc: { start: { offset: 8, line: 1, column: 9 }, end: { offset: 16, line: 1, column: 17 }, - source: '""' - } + source: '""', + }, }, loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 19, line: 1, column: 20 }, - source: '{{ "" }}' - } + source: '{{ "" }}', + }, }) }) test('custom delimiters', () => { const ast = baseParse('

{msg}

', { - delimiters: ['{', '}'] + delimiters: ['{', '}'], }) const element = ast.children[0] as ElementNode const interpolation = element.children[0] as InterpolationNode @@ -322,14 +322,14 @@ describe('compiler: parse', () => { loc: { start: { offset: 4, line: 1, column: 5 }, end: { offset: 7, line: 1, column: 8 }, - source: 'msg' - } + source: 'msg', + }, }, loc: { start: { offset: 3, line: 1, column: 4 }, end: { offset: 8, line: 1, column: 9 }, - source: '{msg}' - } + source: '{msg}', + }, }) }) }) @@ -345,8 +345,8 @@ describe('compiler: parse', () => { loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 7, line: 1, column: 8 }, - source: '' - } + source: '', + }, }) }) @@ -360,8 +360,8 @@ describe('compiler: parse', () => { loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 10, line: 1, column: 11 }, - source: '' - } + source: '', + }, }) }) @@ -376,8 +376,8 @@ describe('compiler: parse', () => { loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 10, line: 1, column: 11 }, - source: '' - } + source: '', + }, }) expect(comment2).toStrictEqual({ type: NodeTypes.COMMENT, @@ -385,8 +385,8 @@ describe('compiler: parse', () => { loc: { start: { offset: 10, line: 1, column: 11 }, end: { offset: 20, line: 1, column: 21 }, - source: '' - } + source: '', + }, }) }) @@ -403,38 +403,38 @@ describe('compiler: parse', () => { const rawText = `

` const astWithComments = baseParse(`

${rawText}
`, { - comments: true + comments: true, }) expect( - (astWithComments.children[0] as ElementNode).children + (astWithComments.children[0] as ElementNode).children, ).toMatchObject([ { type: NodeTypes.ELEMENT, - tag: 'p' + tag: 'p', }, { - type: NodeTypes.COMMENT + type: NodeTypes.COMMENT, }, { type: NodeTypes.ELEMENT, - tag: 'p' - } + tag: 'p', + }, ]) const astWithoutComments = baseParse(`
${rawText}
`, { - comments: false + comments: false, }) expect( - (astWithoutComments.children[0] as ElementNode).children + (astWithoutComments.children[0] as ElementNode).children, ).toMatchObject([ { type: NodeTypes.ELEMENT, - tag: 'p' + tag: 'p', }, { type: NodeTypes.ELEMENT, - tag: 'p' - } + tag: 'p', + }, ]) }) }) @@ -458,15 +458,15 @@ describe('compiler: parse', () => { loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 10, line: 1, column: 11 }, - source: 'hello' - } - } + source: 'hello', + }, + }, ], loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 16, line: 1, column: 17 }, - source: '
hello
' - } + source: '
hello
', + }, }) }) @@ -485,8 +485,8 @@ describe('compiler: parse', () => { loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 11, line: 1, column: 12 }, - source: '
' - } + source: '
', + }, }) }) @@ -506,14 +506,14 @@ describe('compiler: parse', () => { loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 6, line: 1, column: 7 }, - source: '
' - } + source: '
', + }, }) }) test('void element', () => { const ast = baseParse('after', { - isVoidTag: tag => tag === 'img' + isVoidTag: tag => tag === 'img', }) const element = ast.children[0] as ElementNode @@ -528,14 +528,14 @@ describe('compiler: parse', () => { loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 5, line: 1, column: 6 }, - source: '' - } + source: '', + }, }) }) test('self-closing void element', () => { const ast = baseParse('after', { - isVoidTag: tag => tag === 'img' + isVoidTag: tag => tag === 'img', }) const element = ast.children[0] as ElementNode @@ -551,8 +551,8 @@ describe('compiler: parse', () => { loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 6, line: 1, column: 7 }, - source: '' - } + source: '', + }, }) }) @@ -561,7 +561,7 @@ describe('compiler: parse', () => { const element = ast.children[0] expect(element).toMatchObject({ type: NodeTypes.ELEMENT, - tagType: ElementTypes.TEMPLATE + tagType: ElementTypes.TEMPLATE, }) }) @@ -570,31 +570,31 @@ describe('compiler: parse', () => { const element = ast.children[0] expect(element).toMatchObject({ type: NodeTypes.ELEMENT, - tagType: ElementTypes.ELEMENT + tagType: ElementTypes.ELEMENT, }) }) test('native element with `isNativeTag`', () => { const ast = baseParse('
', { - isNativeTag: tag => tag === 'div' + isNativeTag: tag => tag === 'div', }) expect(ast.children[0]).toMatchObject({ type: NodeTypes.ELEMENT, tag: 'div', - tagType: ElementTypes.ELEMENT + tagType: ElementTypes.ELEMENT, }) expect(ast.children[1]).toMatchObject({ type: NodeTypes.ELEMENT, tag: 'comp', - tagType: ElementTypes.COMPONENT + tagType: ElementTypes.COMPONENT, }) expect(ast.children[2]).toMatchObject({ type: NodeTypes.ELEMENT, tag: 'Comp', - tagType: ElementTypes.COMPONENT + tagType: ElementTypes.COMPONENT, }) }) @@ -604,19 +604,19 @@ describe('compiler: parse', () => { expect(ast.children[0]).toMatchObject({ type: NodeTypes.ELEMENT, tag: 'div', - tagType: ElementTypes.ELEMENT + tagType: ElementTypes.ELEMENT, }) expect(ast.children[1]).toMatchObject({ type: NodeTypes.ELEMENT, tag: 'comp', - tagType: ElementTypes.ELEMENT + tagType: ElementTypes.ELEMENT, }) expect(ast.children[2]).toMatchObject({ type: NodeTypes.ELEMENT, tag: 'Comp', - tagType: ElementTypes.COMPONENT + tagType: ElementTypes.COMPONENT, }) }) @@ -624,26 +624,26 @@ describe('compiler: parse', () => { const ast = baseParse( `
`, { - isNativeTag: tag => tag === 'div' - } + isNativeTag: tag => tag === 'div', + }, ) expect(ast.children[0]).toMatchObject({ type: NodeTypes.ELEMENT, tag: 'div', - tagType: ElementTypes.ELEMENT + tagType: ElementTypes.ELEMENT, }) expect(ast.children[1]).toMatchObject({ type: NodeTypes.ELEMENT, tag: 'div', - tagType: ElementTypes.COMPONENT + tagType: ElementTypes.COMPONENT, }) expect(ast.children[2]).toMatchObject({ type: NodeTypes.ELEMENT, tag: 'Comp', - tagType: ElementTypes.COMPONENT + tagType: ElementTypes.COMPONENT, }) }) @@ -653,56 +653,56 @@ describe('compiler: parse', () => { expect(ast.children[0]).toMatchObject({ type: NodeTypes.ELEMENT, tag: 'div', - tagType: ElementTypes.ELEMENT + tagType: ElementTypes.ELEMENT, }) expect(ast.children[1]).toMatchObject({ type: NodeTypes.ELEMENT, tag: 'div', - tagType: ElementTypes.COMPONENT + tagType: ElementTypes.COMPONENT, }) expect(ast.children[2]).toMatchObject({ type: NodeTypes.ELEMENT, tag: 'Comp', - tagType: ElementTypes.COMPONENT + tagType: ElementTypes.COMPONENT, }) }) test('custom element', () => { const ast = baseParse('
', { isNativeTag: tag => tag === 'div', - isCustomElement: tag => tag === 'comp' + isCustomElement: tag => tag === 'comp', }) expect(ast.children[0]).toMatchObject({ type: NodeTypes.ELEMENT, tag: 'div', - tagType: ElementTypes.ELEMENT + tagType: ElementTypes.ELEMENT, }) expect(ast.children[1]).toMatchObject({ type: NodeTypes.ELEMENT, tag: 'comp', - tagType: ElementTypes.ELEMENT + tagType: ElementTypes.ELEMENT, }) }) test('built-in component', () => { const ast = baseParse('
', { - isBuiltInComponent: tag => (tag === 'comp' ? Symbol() : void 0) + isBuiltInComponent: tag => (tag === 'comp' ? Symbol() : void 0), }) expect(ast.children[0]).toMatchObject({ type: NodeTypes.ELEMENT, tag: 'div', - tagType: ElementTypes.ELEMENT + tagType: ElementTypes.ELEMENT, }) expect(ast.children[1]).toMatchObject({ type: NodeTypes.ELEMENT, tag: 'comp', - tagType: ElementTypes.COMPONENT + tagType: ElementTypes.COMPONENT, }) }) @@ -712,13 +712,13 @@ describe('compiler: parse', () => { expect(ast.children[0]).toMatchObject({ type: NodeTypes.ELEMENT, tag: 'slot', - tagType: ElementTypes.SLOT + tagType: ElementTypes.SLOT, }) expect(ast.children[1]).toMatchObject({ type: NodeTypes.ELEMENT, tag: 'Comp', - tagType: ElementTypes.COMPONENT + tagType: ElementTypes.COMPONENT, }) }) @@ -739,23 +739,23 @@ describe('compiler: parse', () => { nameLoc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 7, line: 1, column: 8 }, - source: 'id' + source: 'id', }, value: undefined, loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 7, line: 1, column: 8 }, - source: 'id' - } - } + source: 'id', + }, + }, ], children: [], loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 14, line: 1, column: 15 }, - source: '
' - } + source: '
', + }, }) }) @@ -776,7 +776,7 @@ describe('compiler: parse', () => { nameLoc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 7, line: 1, column: 8 }, - source: 'id' + source: 'id', }, value: { type: NodeTypes.TEXT, @@ -784,23 +784,23 @@ describe('compiler: parse', () => { loc: { start: { offset: 8, line: 1, column: 9 }, end: { offset: 10, line: 1, column: 11 }, - source: '""' - } + source: '""', + }, }, loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 10, line: 1, column: 11 }, - source: 'id=""' - } - } + source: 'id=""', + }, + }, ], children: [], loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 17, line: 1, column: 18 }, - source: '
' - } + source: '
', + }, }) }) @@ -821,7 +821,7 @@ describe('compiler: parse', () => { nameLoc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 7, line: 1, column: 8 }, - source: 'id' + source: 'id', }, value: { type: NodeTypes.TEXT, @@ -829,23 +829,23 @@ describe('compiler: parse', () => { loc: { start: { offset: 8, line: 1, column: 9 }, end: { offset: 10, line: 1, column: 11 }, - source: "''" - } + source: "''", + }, }, loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 10, line: 1, column: 11 }, - source: "id=''" - } - } + source: "id=''", + }, + }, ], children: [], loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 17, line: 1, column: 18 }, - source: "
" - } + source: "
", + }, }) }) @@ -866,7 +866,7 @@ describe('compiler: parse', () => { nameLoc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 7, line: 1, column: 8 }, - source: 'id' + source: 'id', }, value: { type: NodeTypes.TEXT, @@ -874,23 +874,23 @@ describe('compiler: parse', () => { loc: { start: { offset: 8, line: 1, column: 9 }, end: { offset: 12, line: 1, column: 13 }, - source: '">\'"' - } + source: '">\'"', + }, }, loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 12, line: 1, column: 13 }, - source: 'id=">\'"' - } - } + source: 'id=">\'"', + }, + }, ], children: [], loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 19, line: 1, column: 20 }, - source: '
' - } + source: '
', + }, }) }) @@ -911,7 +911,7 @@ describe('compiler: parse', () => { nameLoc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 7, line: 1, column: 8 }, - source: 'id' + source: 'id', }, value: { type: NodeTypes.TEXT, @@ -919,23 +919,23 @@ describe('compiler: parse', () => { loc: { start: { offset: 8, line: 1, column: 9 }, end: { offset: 12, line: 1, column: 13 }, - source: "'>\"'" - } + source: "'>\"'", + }, }, loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 12, line: 1, column: 13 }, - source: "id='>\"'" - } - } + source: "id='>\"'", + }, + }, ], children: [], loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 19, line: 1, column: 20 }, - source: "
" - } + source: "
", + }, }) }) @@ -956,7 +956,7 @@ describe('compiler: parse', () => { nameLoc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 7, line: 1, column: 8 }, - source: 'id' + source: 'id', }, value: { type: NodeTypes.TEXT, @@ -964,30 +964,30 @@ describe('compiler: parse', () => { loc: { start: { offset: 8, line: 1, column: 9 }, end: { offset: 10, line: 1, column: 11 }, - source: 'a/' - } + source: 'a/', + }, }, loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 10, line: 1, column: 11 }, - source: 'id=a/' - } - } + source: 'id=a/', + }, + }, ], children: [], loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 17, line: 1, column: 18 }, - source: '
' - } + source: '
', + }, }) }) test('attribute value with >', () => { const ast = baseParse( '', - { parseMode: 'sfc' } + { parseMode: 'sfc' }, ) const element = ast.children[0] as ElementNode expect(element).toMatchObject({ @@ -999,35 +999,35 @@ describe('compiler: parse', () => { children: [], innerLoc: { start: { column: 67, line: 1, offset: 66 }, - end: { column: 67, line: 1, offset: 66 } + end: { column: 67, line: 1, offset: 66 }, }, props: [ { loc: { source: 'setup', end: { column: 14, line: 1, offset: 13 }, - start: { column: 9, line: 1, offset: 8 } + start: { column: 9, line: 1, offset: 8 }, }, name: 'setup', nameLoc: { source: 'setup', end: { column: 14, line: 1, offset: 13 }, - start: { column: 9, line: 1, offset: 8 } + start: { column: 9, line: 1, offset: 8 }, }, type: NodeTypes.ATTRIBUTE, - value: undefined + value: undefined, }, { loc: { source: 'lang="ts"', end: { column: 24, line: 1, offset: 23 }, - start: { column: 15, line: 1, offset: 14 } + start: { column: 15, line: 1, offset: 14 }, }, name: 'lang', nameLoc: { source: 'lang', end: { column: 19, line: 1, offset: 18 }, - start: { column: 15, line: 1, offset: 14 } + start: { column: 15, line: 1, offset: 14 }, }, type: NodeTypes.ATTRIBUTE, value: { @@ -1035,22 +1035,22 @@ describe('compiler: parse', () => { loc: { source: '"ts"', end: { column: 24, line: 1, offset: 23 }, - start: { column: 20, line: 1, offset: 19 } + start: { column: 20, line: 1, offset: 19 }, }, - type: NodeTypes.TEXT - } + type: NodeTypes.TEXT, + }, }, { loc: { source: 'generic="T extends Record"', end: { column: 66, line: 1, offset: 65 }, - start: { column: 25, line: 1, offset: 24 } + start: { column: 25, line: 1, offset: 24 }, }, name: 'generic', nameLoc: { source: 'generic', end: { column: 32, line: 1, offset: 31 }, - start: { column: 25, line: 1, offset: 24 } + start: { column: 25, line: 1, offset: 24 }, }, type: NodeTypes.ATTRIBUTE, value: { @@ -1058,12 +1058,12 @@ describe('compiler: parse', () => { loc: { source: '"T extends Record"', end: { column: 66, line: 1, offset: 65 }, - start: { column: 33, line: 1, offset: 32 } + start: { column: 33, line: 1, offset: 32 }, }, - type: NodeTypes.TEXT - } - } - ] + type: NodeTypes.TEXT, + }, + }, + ], }) }) @@ -1084,7 +1084,7 @@ describe('compiler: parse', () => { nameLoc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 7, line: 1, column: 8 }, - source: 'id' + source: 'id', }, value: { type: NodeTypes.TEXT, @@ -1092,14 +1092,14 @@ describe('compiler: parse', () => { loc: { start: { offset: 8, line: 1, column: 9 }, end: { offset: 9, line: 1, column: 10 }, - source: 'a' - } + source: 'a', + }, }, loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 9, line: 1, column: 10 }, - source: 'id=a' - } + source: 'id=a', + }, }, { type: NodeTypes.ATTRIBUTE, @@ -1107,7 +1107,7 @@ describe('compiler: parse', () => { nameLoc: { start: { offset: 10, line: 1, column: 11 }, end: { offset: 15, line: 1, column: 16 }, - source: 'class' + source: 'class', }, value: { type: NodeTypes.TEXT, @@ -1115,14 +1115,14 @@ describe('compiler: parse', () => { loc: { start: { offset: 16, line: 1, column: 17 }, end: { offset: 19, line: 1, column: 20 }, - source: '"c"' - } + source: '"c"', + }, }, loc: { start: { offset: 10, line: 1, column: 11 }, end: { offset: 19, line: 1, column: 20 }, - source: 'class="c"' - } + source: 'class="c"', + }, }, { type: NodeTypes.ATTRIBUTE, @@ -1130,14 +1130,14 @@ describe('compiler: parse', () => { nameLoc: { start: { offset: 20, line: 1, column: 21 }, end: { offset: 25, line: 1, column: 26 }, - source: 'inert' + source: 'inert', }, value: undefined, loc: { start: { offset: 20, line: 1, column: 21 }, end: { offset: 25, line: 1, column: 26 }, - source: 'inert' - } + source: 'inert', + }, }, { type: NodeTypes.ATTRIBUTE, @@ -1145,7 +1145,7 @@ describe('compiler: parse', () => { nameLoc: { start: { offset: 26, line: 1, column: 27 }, end: { offset: 31, line: 1, column: 32 }, - source: 'style' + source: 'style', }, value: { type: NodeTypes.TEXT, @@ -1153,23 +1153,23 @@ describe('compiler: parse', () => { loc: { start: { offset: 32, line: 1, column: 33 }, end: { offset: 34, line: 1, column: 35 }, - source: "''" - } + source: "''", + }, }, loc: { start: { offset: 26, line: 1, column: 27 }, end: { offset: 34, line: 1, column: 35 }, - source: "style=''" - } - } + source: "style=''", + }, + }, ], children: [], loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 41, line: 1, column: 42 }, - source: '
' - } + source: '
', + }, }) }) @@ -1184,7 +1184,7 @@ describe('compiler: parse', () => { loc: { start: { column: 1, line: 1, offset: 0 }, end: { column: 10, line: 3, offset: 29 }, - source: '
' + source: '
', }, ns: Namespaces.HTML, props: [ @@ -1193,7 +1193,7 @@ describe('compiler: parse', () => { nameLoc: { start: { column: 6, line: 1, offset: 5 }, end: { column: 11, line: 1, offset: 10 }, - source: 'class' + source: 'class', }, type: NodeTypes.ATTRIBUTE, value: { @@ -1201,20 +1201,20 @@ describe('compiler: parse', () => { loc: { start: { column: 12, line: 1, offset: 11 }, end: { column: 3, line: 3, offset: 22 }, - source: '" \n\t c \t\n "' + source: '" \n\t c \t\n "', }, - type: NodeTypes.TEXT + type: NodeTypes.TEXT, }, loc: { start: { column: 6, line: 1, offset: 5 }, end: { column: 3, line: 3, offset: 22 }, - source: 'class=" \n\t c \t\n "' - } - } + source: 'class=" \n\t c \t\n "', + }, + }, ], tag: 'div', tagType: ElementTypes.ELEMENT, - type: NodeTypes.ELEMENT + type: NodeTypes.ELEMENT, }) }) @@ -1232,8 +1232,8 @@ describe('compiler: parse', () => { loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 9, line: 1, column: 10 }, - source: 'v-if' - } + source: 'v-if', + }, }) }) @@ -1255,14 +1255,14 @@ describe('compiler: parse', () => { loc: { start: { offset: 11, line: 1, column: 12 }, end: { offset: 12, line: 1, column: 13 }, - source: 'a' - } + source: 'a', + }, }, loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 13, line: 1, column: 14 }, - source: 'v-if="a"' - } + source: 'v-if="a"', + }, }) }) @@ -1282,16 +1282,16 @@ describe('compiler: parse', () => { loc: { start: { column: 11, line: 1, offset: 10 }, end: { column: 16, line: 1, offset: 15 }, - source: 'click' - } + source: 'click', + }, }, modifiers: [], exp: undefined, loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 15, line: 1, column: 16 }, - source: 'v-on:click' - } + source: 'v-on:click', + }, }) }) @@ -1303,8 +1303,8 @@ describe('compiler: parse', () => { expect(directive.arg).toMatchObject({ loc: { start: { offset: 12, line: 1, column: 13 }, - end: { offset: 16, line: 1, column: 17 } - } + end: { offset: 16, line: 1, column: 17 }, + }, }) }) @@ -1317,8 +1317,8 @@ describe('compiler: parse', () => { content: 'item.item', loc: { start: { offset: 6, line: 1, column: 7 }, - end: { offset: 15, line: 1, column: 16 } - } + end: { offset: 15, line: 1, column: 16 }, + }, }) }) @@ -1338,16 +1338,16 @@ describe('compiler: parse', () => { loc: { start: { column: 11, line: 1, offset: 10 }, end: { column: 18, line: 1, offset: 17 }, - source: '[event]' - } + source: '[event]', + }, }, modifiers: [], exp: undefined, loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 17, line: 1, column: 18 }, - source: 'v-on:[event]' - } + source: 'v-on:[event]', + }, }) }) @@ -1365,8 +1365,8 @@ describe('compiler: parse', () => { loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 15, line: 1, column: 16 }, - source: 'v-on.enter' - } + source: 'v-on.enter', + }, }) }) @@ -1384,8 +1384,8 @@ describe('compiler: parse', () => { loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 21, line: 1, column: 22 }, - source: 'v-on.enter.exact' - } + source: 'v-on.enter.exact', + }, }) }) @@ -1405,16 +1405,16 @@ describe('compiler: parse', () => { loc: { start: { column: 11, line: 1, offset: 10 }, end: { column: 16, line: 1, offset: 15 }, - source: 'click' - } + source: 'click', + }, }, modifiers: ['enter', 'exact'], exp: undefined, loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 27, line: 1, column: 28 }, - source: 'v-on:click.enter.exact' - } + source: 'v-on:click.enter.exact', + }, }) }) @@ -1434,16 +1434,16 @@ describe('compiler: parse', () => { loc: { start: { column: 11, line: 1, offset: 10 }, end: { column: 16, line: 1, offset: 15 }, - source: '[a.b]' - } + source: '[a.b]', + }, }, modifiers: ['camel'], exp: undefined, loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 21, line: 1, column: 22 }, - source: 'v-on:[a.b].camel' - } + source: 'v-on:[a.b].camel', + }, }) }) @@ -1452,7 +1452,7 @@ describe('compiler: parse', () => { const ast = baseParse('
', { onError: err => { errorCode = err.code as number - } + }, }) const directive = (ast.children[0] as ElementNode).props[0] @@ -1464,13 +1464,13 @@ describe('compiler: parse', () => { loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 7, line: 1, column: 8 }, - source: 'v-' + source: 'v-', }, nameLoc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 7, line: 1, column: 8 }, - source: 'v-' - } + source: 'v-', + }, }) }) @@ -1490,8 +1490,8 @@ describe('compiler: parse', () => { loc: { start: { column: 7, line: 1, offset: 6 }, end: { column: 8, line: 1, offset: 7 }, - source: 'a' - } + source: 'a', + }, }, modifiers: [], exp: { @@ -1502,14 +1502,14 @@ describe('compiler: parse', () => { loc: { start: { offset: 8, line: 1, column: 9 }, end: { offset: 9, line: 1, column: 10 }, - source: 'b' - } + source: 'b', + }, }, loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 9, line: 1, column: 10 }, - source: ':a=b' - } + source: ':a=b', + }, }) }) @@ -1529,8 +1529,8 @@ describe('compiler: parse', () => { loc: { start: { column: 7, line: 1, offset: 6 }, end: { column: 8, line: 1, offset: 7 }, - source: 'a' - } + source: 'a', + }, }, modifiers: ['prop'], exp: { @@ -1541,14 +1541,14 @@ describe('compiler: parse', () => { loc: { start: { offset: 8, line: 1, column: 9 }, end: { offset: 9, line: 1, column: 10 }, - source: 'b' - } + source: 'b', + }, }, loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 9, line: 1, column: 10 }, - source: '.a=b' - } + source: '.a=b', + }, }) }) @@ -1568,8 +1568,8 @@ describe('compiler: parse', () => { loc: { start: { column: 7, line: 1, offset: 6 }, end: { column: 8, line: 1, offset: 7 }, - source: 'a' - } + source: 'a', + }, }, modifiers: ['sync'], exp: { @@ -1581,14 +1581,14 @@ describe('compiler: parse', () => { loc: { start: { offset: 13, line: 1, column: 14 }, end: { offset: 14, line: 1, column: 15 }, - source: 'b' - } + source: 'b', + }, }, loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 14, line: 1, column: 15 }, - source: ':a.sync=b' - } + source: ':a.sync=b', + }, }) }) @@ -1608,8 +1608,8 @@ describe('compiler: parse', () => { loc: { start: { column: 7, line: 1, offset: 6 }, end: { column: 8, line: 1, offset: 7 }, - source: 'a' - } + source: 'a', + }, }, modifiers: [], exp: { @@ -1621,14 +1621,14 @@ describe('compiler: parse', () => { loc: { start: { offset: 8, line: 1, column: 9 }, end: { offset: 9, line: 1, column: 10 }, - source: 'b' - } + source: 'b', + }, }, loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 9, line: 1, column: 10 }, - source: '@a=b' - } + source: '@a=b', + }, }) }) @@ -1648,8 +1648,8 @@ describe('compiler: parse', () => { loc: { start: { column: 7, line: 1, offset: 6 }, end: { column: 8, line: 1, offset: 7 }, - source: 'a' - } + source: 'a', + }, }, modifiers: ['enter'], exp: { @@ -1661,14 +1661,14 @@ describe('compiler: parse', () => { loc: { start: { offset: 14, line: 1, column: 15 }, end: { offset: 15, line: 1, column: 16 }, - source: 'b' - } + source: 'b', + }, }, loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 15, line: 1, column: 16 }, - source: '@a.enter=b' - } + source: '@a.enter=b', + }, }) }) @@ -1688,8 +1688,8 @@ describe('compiler: parse', () => { loc: { start: { column: 8, line: 1, offset: 7 }, end: { column: 9, line: 1, offset: 8 }, - source: 'a' - } + source: 'a', + }, }, modifiers: [], exp: { @@ -1701,14 +1701,14 @@ describe('compiler: parse', () => { loc: { start: { offset: 10, line: 1, column: 11 }, end: { offset: 15, line: 1, column: 16 }, - source: '{ b }' - } + source: '{ b }', + }, }, loc: { start: { offset: 6, line: 1, column: 7 }, end: { offset: 16, line: 1, column: 17 }, - source: '#a="{ b }"' - } + source: '#a="{ b }"', + }, }) }) @@ -1730,22 +1730,22 @@ describe('compiler: parse', () => { start: { column: 14, line: 1, - offset: 13 + offset: 13, }, end: { column: 21, line: 1, - offset: 20 - } - } - } + offset: 20, + }, + }, + }, }) }) test('v-pre', () => { const ast = baseParse( `
{{ bar }}
\n` + - `
{{ bar }}
` + `
{{ bar }}
`, ) const divWithPre = ast.children[0] as ElementNode @@ -1755,22 +1755,22 @@ describe('compiler: parse', () => { name: `:id`, value: { type: NodeTypes.TEXT, - content: `foo` + content: `foo`, }, loc: { start: { line: 1, column: 12 }, - end: { line: 1, column: 21 } - } - } + end: { line: 1, column: 21 }, + }, + }, ]) expect(divWithPre.children[0]).toMatchObject({ type: NodeTypes.ELEMENT, tagType: ElementTypes.ELEMENT, - tag: `Comp` + tag: `Comp`, }) expect(divWithPre.children[1]).toMatchObject({ type: NodeTypes.TEXT, - content: `{{ bar }}` + content: `{{ bar }}`, }) // should not affect siblings after it @@ -1782,37 +1782,37 @@ describe('compiler: parse', () => { arg: { type: NodeTypes.SIMPLE_EXPRESSION, isStatic: true, - content: `id` + content: `id`, }, exp: { type: NodeTypes.SIMPLE_EXPRESSION, isStatic: false, - content: `foo` + content: `foo`, }, loc: { start: { line: 2, - column: 6 + column: 6, }, end: { line: 2, - column: 15 - } - } - } + column: 15, + }, + }, + }, ]) expect(divWithoutPre.children[0]).toMatchObject({ type: NodeTypes.ELEMENT, tagType: ElementTypes.COMPONENT, - tag: `Comp` + tag: `Comp`, }) expect(divWithoutPre.children[1]).toMatchObject({ type: NodeTypes.INTERPOLATION, content: { type: NodeTypes.SIMPLE_EXPRESSION, content: `bar`, - isStatic: false - } + isStatic: false, + }, }) }) @@ -1823,31 +1823,31 @@ describe('compiler: parse', () => { {{ number }}
- ` + `, ) expect((ast.children[0] as ElementNode).children).toMatchObject([ { type: NodeTypes.ELEMENT, - children: [{ type: NodeTypes.TEXT, content: `{{ number ` }] + children: [{ type: NodeTypes.TEXT, content: `{{ number ` }], }, { type: NodeTypes.ELEMENT, - children: [{ type: NodeTypes.TEXT, content: `}}` }] - } + children: [{ type: NodeTypes.TEXT, content: `}}` }], + }, ]) const ast2 = baseParse(`
{{ number
`) expect((ast2.children[0] as ElementNode).children).toMatchObject([ { type: NodeTypes.ELEMENT, - children: [{ type: NodeTypes.TEXT, content: `{{ number ` }] - } + children: [{ type: NodeTypes.TEXT, content: `{{ number ` }], + }, ]) }) test('self-closing v-pre', () => { const ast = baseParse( - `
\n
{{ bar }}
` + `
\n
{{ bar }}
`, ) // should not affect siblings after it const divWithoutPre = ast.children[1] as ElementNode @@ -1858,37 +1858,37 @@ describe('compiler: parse', () => { arg: { type: NodeTypes.SIMPLE_EXPRESSION, isStatic: true, - content: `id` + content: `id`, }, exp: { type: NodeTypes.SIMPLE_EXPRESSION, isStatic: false, - content: `foo` + content: `foo`, }, loc: { start: { line: 2, - column: 6 + column: 6, }, end: { line: 2, - column: 15 - } - } - } + column: 15, + }, + }, + }, ]) expect(divWithoutPre.children[0]).toMatchObject({ type: NodeTypes.ELEMENT, tagType: ElementTypes.COMPONENT, - tag: `Comp` + tag: `Comp`, }) expect(divWithoutPre.children[1]).toMatchObject({ type: NodeTypes.INTERPOLATION, content: { type: NodeTypes.SIMPLE_EXPRESSION, content: `bar`, - isStatic: false - } + isStatic: false, + }, }) }) @@ -1903,8 +1903,8 @@ describe('compiler: parse', () => { loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 10, line: 1, column: 11 }, - source: 'hello' - } + source: 'hello', + }, }) }) }) @@ -1920,7 +1920,7 @@ describe('compiler: parse', () => { test('self closing multiple tag', () => { const ast = baseParse( `
\n` + - `

` + `

`, ) expect(ast).toMatchSnapshot() @@ -1935,7 +1935,7 @@ describe('compiler: parse', () => { `

\n` + `

\n` + ` \n` + - `

` + `
`, ) expect(ast).toMatchSnapshot() @@ -1943,14 +1943,14 @@ describe('compiler: parse', () => { expect(ast.children).toHaveLength(1) const el = ast.children[0] as any expect(el).toMatchObject({ - tag: 'div' + tag: 'div', }) expect(el.children).toHaveLength(2) expect(el.children[0]).toMatchObject({ - tag: 'p' + tag: 'p', }) expect(el.children[1]).toMatchObject({ - type: NodeTypes.COMMENT + type: NodeTypes.COMMENT, }) }) @@ -1961,7 +1961,7 @@ describe('compiler: parse', () => { const spy = vi.fn() const ast = baseParse(`
\n\n
\n`, { - onError: spy + onError: spy, }) expect(spy.mock.calls).toMatchObject([ @@ -1972,10 +1972,10 @@ describe('compiler: parse', () => { start: { offset: 6, line: 2, - column: 1 - } - } - } + column: 1, + }, + }, + }, ], [ { @@ -1984,11 +1984,11 @@ describe('compiler: parse', () => { start: { offset: 20, line: 4, - column: 1 - } - } - } - ] + column: 1, + }, + }, + }, + ], ]) expect(ast).toMatchSnapshot() @@ -2000,7 +2000,7 @@ describe('compiler: parse', () => { const butSrc = ` but ` const bazSrc = `{{ baz }}` const [foo, bar, but, baz] = baseParse( - fooSrc + barSrc + butSrc + bazSrc + fooSrc + barSrc + butSrc + bazSrc, ).children let offset = 0 @@ -2043,14 +2043,14 @@ describe('compiler: parse', () => { const ast = baseParse( ` `, - { filename: 'example.vue', sourceMap: true } + { filename: 'example.vue', sourceMap: true }, ).descriptor.template as SFCTemplateBlock const result = compile({ filename: 'example.vue', source: template.content, - preprocessLang: template.lang + preprocessLang: template.lang, }) expect(result.errors.length).toBe(1) const message = result.errors[0].toString() expect(message).toMatch(`Error: example.vue:3:1`) expect(message).toMatch( - `The end of the string reached with no closing bracket ) found.` + `The end of the string reached with no closing bracket ) found.`, ) }) @@ -362,7 +362,7 @@ test('should generate the correct imports expression', () => { `, - ssr: true + ssr: true, }) expect(code).toMatch(`_ssrRenderAttr(\"src\", _imports_1)`) expect(code).toMatch(`_createVNode(\"img\", { src: _imports_1 })`) @@ -384,7 +384,7 @@ test('should not hoist srcset URLs in SSR mode', () => { `, - ssr: true + ssr: true, }) expect(code).toMatchSnapshot() }) @@ -422,7 +422,7 @@ test('prefixing edge case for reused AST', () => { id: 'xxx', filename: 'test.vue', ast: descriptor.template!.ast, - source: descriptor.template!.content + source: descriptor.template!.content, }) expect(code).not.toMatch(`_ctx.t`) }) @@ -436,7 +436,7 @@ interface Pos { function getPositionInCode( code: string, token: string, - expectName: string | boolean = false + expectName: string | boolean = false, ): Pos { const generatedOffset = code.indexOf(token) let line = 1 @@ -452,7 +452,7 @@ function getPositionInCode( column: lastNewLinePos === -1 ? generatedOffset - : generatedOffset - lastNewLinePos - 1 + : generatedOffset - lastNewLinePos - 1, } if (expectName) { res.name = typeof expectName === 'string' ? expectName : token diff --git a/packages/compiler-sfc/__tests__/cssVars.spec.ts b/packages/compiler-sfc/__tests__/cssVars.spec.ts index d57ad079d24..323c9c7a599 100644 --- a/packages/compiler-sfc/__tests__/cssVars.spec.ts +++ b/packages/compiler-sfc/__tests__/cssVars.spec.ts @@ -1,5 +1,5 @@ import { compileStyle, parse } from '../src' -import { mockId, compileSFCScript, assertCode } from './utils' +import { assertCode, compileSFCScript, mockId } from './utils' describe('CSS vars injection', () => { test('generating correct code for nested paths', () => { @@ -8,7 +8,7 @@ describe('CSS vars injection', () => { `` + }`, ) expect(content).toMatch(`_useCssVars(_ctx => ({ "${mockId}-color": (_ctx.color), @@ -32,7 +32,7 @@ describe('CSS vars injection', () => { div { font-size: v-bind(size); } - ` + `, ) expect(content).toMatch(`_useCssVars(_ctx => ({ "${mockId}-size": (_ctx.size) @@ -57,7 +57,7 @@ describe('CSS vars injection', () => { font-size: v-bind(size); border: v-bind(foo); } - ` + `, ) // should handle: // 1. local const bindings @@ -69,7 +69,7 @@ describe('CSS vars injection', () => { "${mockId}-foo": (__props.foo) })`) expect(content).toMatch( - `import { useCssVars as _useCssVars, unref as _unref } from 'vue'` + `import { useCssVars as _useCssVars, unref as _unref } from 'vue'`, ) assertCode(content) }) @@ -85,7 +85,7 @@ describe('CSS vars injection', () => { font-family: v-bind(フォント); }`, filename: 'test.css', - id: 'data-v-test' + id: 'data-v-test', }) expect(code).toMatchInlineSnapshot(` ".foo { @@ -106,7 +106,7 @@ describe('CSS vars injection', () => { color: v-bind(color); font-size: v-bind('font.size'); }`, - { isProd: true } + { isProd: true }, ) expect(content).toMatch(`_useCssVars(_ctx => ({ "4003f1a6": (_ctx.color), @@ -120,7 +120,7 @@ describe('CSS vars injection', () => { }`, filename: 'test.css', id: mockId, - isProd: true + isProd: true, }) expect(code).toMatchInlineSnapshot(` ".foo { @@ -135,8 +135,8 @@ describe('CSS vars injection', () => { assertCode( compileSFCScript( `\n` + - `` - ).content + ``, + ).content, ) }) @@ -144,8 +144,8 @@ describe('CSS vars injection', () => { assertCode( compileSFCScript( `\n` + - `` - ).content + ``, + ).content, ) }) @@ -155,8 +155,8 @@ describe('CSS vars injection', () => { `\n` + `` - ).content + \n` + ``, + ).content, ) }) @@ -164,8 +164,8 @@ describe('CSS vars injection', () => { assertCode( compileSFCScript( `\n` + - `` - ).content + ``, + ).content, ) }) @@ -178,7 +178,7 @@ describe('CSS vars injection', () => { div{ /* color: v-bind(color); */ width:20; } div{ width: v-bind(width); } /* comment */ - ` + `, ) expect(content).not.toMatch(`"${mockId}-color": (color)`) @@ -198,7 +198,7 @@ describe('CSS vars injection', () => { p { color: v-bind(color); } - ` + `, ) // color should only be injected once, even if it is twice in style expect(content).toMatch(`_useCssVars(_ctx => ({ @@ -229,7 +229,7 @@ describe('CSS vars injection', () => { p { color: v-bind(((a + b)) / (2 * a)); } - ` + `, ) expect(content).toMatch(`_useCssVars(_ctx => ({ "${mockId}-foo": (_unref(foo)), @@ -243,7 +243,7 @@ describe('CSS vars injection', () => { // #6022 test('should be able to parse incomplete expressions', () => { const { - descriptor: { cssVars } + descriptor: { cssVars }, } = parse( ` ` + `, ) expect(cssVars).toMatchObject([`count.toString(`, `xxx`]) }) @@ -266,10 +266,10 @@ describe('CSS vars injection', () => { label { background: v-bind(background); } - ` + `, ) expect(content).toMatch( - `export default {\n setup(__props, { expose: __expose }) {\n __expose();\n\n_useCssVars(_ctx => ({\n "xxxxxxxx-background": (_unref(background))\n}))` + `export default {\n setup(__props, { expose: __expose }) {\n __expose();\n\n_useCssVars(_ctx => ({\n "xxxxxxxx-background": (_unref(background))\n}))`, ) }) @@ -287,9 +287,9 @@ describe('CSS vars injection', () => { { inlineTemplate: true, templateOptions: { - ssr: true - } - } + ssr: true, + }, + }, ) expect(content).not.toMatch(`_useCssVars`) }) @@ -308,9 +308,9 @@ describe('CSS vars injection', () => { { inlineTemplate: false, templateOptions: { - ssr: true - } - } + ssr: true, + }, + }, ) expect(content).not.toMatch(`_useCssVars`) }) @@ -333,9 +333,9 @@ describe('CSS vars injection', () => { `, { templateOptions: { - ssr: true - } - } + ssr: true, + }, + }, ) expect(content).not.toMatch(`_useCssVars`) }) diff --git a/packages/compiler-sfc/__tests__/parse.spec.ts b/packages/compiler-sfc/__tests__/parse.spec.ts index 9b3e3e8a672..e650a850abd 100644 --- a/packages/compiler-sfc/__tests__/parse.spec.ts +++ b/packages/compiler-sfc/__tests__/parse.spec.ts @@ -37,7 +37,7 @@ font-weight: bold; } ` const { - descriptor: { styles } + descriptor: { styles }, } = parse(src) expect(styles[0].map).not.toBeUndefined() @@ -69,7 +69,7 @@ font-weight: bold; // Padding determines how many blank lines will there be before the style block const padding = Math.round(Math.random() * 10) const script = parse( - `${'\n'.repeat(padding)}\n` + `${'\n'.repeat(padding)}\n`, ).descriptor.script expect(script!.map).not.toBeUndefined() @@ -88,7 +88,7 @@ font-weight: bold; h1 foo div bar span baz -\n` +\n`, ).descriptor.template! expect(template.map).not.toBeUndefined() @@ -103,7 +103,7 @@ font-weight: bold; test('custom block', () => { const padding = Math.round(Math.random() * 10) const custom = parse( - `${'\n'.repeat(padding)}\n{\n "greeting": "hello"\n}\n\n` + `${'\n'.repeat(padding)}\n{\n "greeting": "hello"\n}\n\n`, ).descriptor.customBlocks[0] expect(custom!.map).not.toBeUndefined() @@ -138,42 +138,42 @@ h1 { color: red } const padTrue = parse(content.trim(), { pad: true }).descriptor expect(padTrue.script!.content).toBe( - Array(3 + 1).join('//\n') + '\nexport default {}\n' + Array(3 + 1).join('//\n') + '\nexport default {}\n', ) expect(padTrue.styles[0].content).toBe( - Array(6 + 1).join('\n') + '\nh1 { color: red }\n' + Array(6 + 1).join('\n') + '\nh1 { color: red }\n', ) expect(padTrue.customBlocks[0].content).toBe( - Array(9 + 1).join('\n') + '\n{ "greeting": "hello" }\n' + Array(9 + 1).join('\n') + '\n{ "greeting": "hello" }\n', ) const padLine = parse(content.trim(), { pad: 'line' }).descriptor expect(padLine.script!.content).toBe( - Array(3 + 1).join('//\n') + '\nexport default {}\n' + Array(3 + 1).join('//\n') + '\nexport default {}\n', ) expect(padLine.styles[0].content).toBe( - Array(6 + 1).join('\n') + '\nh1 { color: red }\n' + Array(6 + 1).join('\n') + '\nh1 { color: red }\n', ) expect(padLine.customBlocks[0].content).toBe( - Array(9 + 1).join('\n') + '\n{ "greeting": "hello" }\n' + Array(9 + 1).join('\n') + '\n{ "greeting": "hello" }\n', ) const padSpace = parse(content.trim(), { pad: 'space' }).descriptor expect(padSpace.script!.content).toBe( `\n\n\n`.replace( /./g, - ' ' - ) + '\n{ "greeting": "hello" }\n' + ' ', + ) + '\n{ "greeting": "hello" }\n', ) }) @@ -187,8 +187,8 @@ h1 { color: red } end: { line: 3, column: 1, - offset: 10 + content.length - } + offset: 10 + content.length, + }, }) }) @@ -198,7 +198,7 @@ h1 { color: red } expect(descriptor.template!.content).toBeFalsy() expect(descriptor.template!.loc).toMatchObject({ start: { line: 1, column: 12, offset: 11 }, - end: { line: 1, column: 12, offset: 11 } + end: { line: 1, column: 12, offset: 11 }, }) }) @@ -208,7 +208,7 @@ h1 { color: red } expect(descriptor.template!.content).toBeFalsy() expect(descriptor.template!.loc).toMatchObject({ start: { line: 1, column: 11, offset: 10 }, - end: { line: 1, column: 11, offset: 10 } + end: { line: 1, column: 11, offset: 10 }, }) }) @@ -219,7 +219,7 @@ h1 { color: red } expect(parse(``).descriptor.styles.length).toBe(0) expect(parse(``).descriptor.customBlocks.length).toBe(0) expect( - parse(` \n\t `).descriptor.customBlocks.length + parse(` \n\t `).descriptor.customBlocks.length, ).toBe(0) }) @@ -239,19 +239,19 @@ h1 { color: red } const { descriptor } = parse( `\n`, { - ignoreEmpty: false - } + ignoreEmpty: false, + }, ) expect(descriptor.script).toBeTruthy() expect(descriptor.script!.loc).toMatchObject({ start: { line: 1, column: 9, offset: 8 }, - end: { line: 1, column: 9, offset: 8 } + end: { line: 1, column: 9, offset: 8 }, }) expect(descriptor.scriptSetup).toBeTruthy() expect(descriptor.scriptSetup!.loc).toMatchObject({ start: { line: 2, column: 15, offset: 32 }, - end: { line: 3, column: 1, offset: 33 } + end: { line: 3, column: 1, offset: 33 }, }) }) @@ -267,7 +267,7 @@ h1 { color: red } test('treat empty lang attribute as the html', () => { const content = `
` const { descriptor, errors } = parse( - `` + ``, ) expect(descriptor.template!.content).toBe(content) expect(errors.length).toBe(0) @@ -277,7 +277,7 @@ h1 { color: red } test('template with preprocessor lang should be treated as plain text', () => { const content = `p(v-if="1 < 2") test
` const { descriptor, errors } = parse( - `` + ``, ) expect(errors.length).toBe(0) expect(descriptor.template!.content).toBe(content) @@ -301,17 +301,17 @@ h1 { color: red } expect(parse(``).descriptor.slotted).toBe(false) expect( parse(``).descriptor - .slotted + .slotted, ).toBe(false) expect( parse( - `` - ).descriptor.slotted + ``, + ).descriptor.slotted, ).toBe(true) expect( parse( - `` - ).descriptor.slotted + ``, + ).descriptor.slotted, ).toBe(true) }) @@ -332,8 +332,8 @@ h1 { color: red } options.onError!(new Error('foo') as any) return createRoot([]) }, - compile: baseCompile - } + compile: baseCompile, + }, }) expect(errors.length).toBe(2) // error thrown by the custom parse @@ -344,7 +344,7 @@ h1 { color: red } test('treat custom blocks as raw text', () => { const { errors, descriptor } = parse( - ` <-& ` + ` <-& `, ) expect(errors.length).toBe(0) expect(descriptor.customBlocks[0].content).toBe(` <-& `) @@ -358,7 +358,7 @@ h1 { color: red } test('should only allow single template element', () => { assertWarning( parse(``).errors, - `Single file component can contain only one