Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compiler-sfc): Hoist leading comment of defineOptions to export default #9013

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,51 @@ return { }

}"
`;

exports[`defineOptions() > should hoist comments even when called with no arguments 1`] = `
"/** Script docstring. */
const __default__ = {}

/** Script setup docstring 1 */
// Script setup docstring 2
/**
* Script setup docstring 3
*/
export default /*#__PURE__*/Object.assign(__default__, {
setup(__props, { expose: __expose }) {
__expose();






return { }
}

})"
`;

exports[`defineOptions() > should hoist leading comments up to export default 1`] = `
"/** Script docstring. */
const __default__ = {}

/** Script setup docstring 1 */
// Script setup docstring 2
/**
* Script setup docstring 3
*/
export default /*#__PURE__*/Object.assign(__default__, {}, {
setup(__props, { expose: __expose }) {
__expose();






return { }
}

})"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,66 @@ describe('defineOptions()', () => {
expect(content).not.toMatch('defineOptions')
})

it('should hoist leading comments up to export default', () => {
const { content } = compile(
`<script>\n` +
`/** Script docstring. */\n` +
`export default {}\n` +
`</script>\n` +
`\n` +
`<script setup>\n` +
`/** Script setup docstring 1 */\n` +
`// Script setup docstring 2\n` +
`/**\n` +
` * Script setup docstring 3\n` +
` */\n` +
`defineOptions({})\n` +
`</script>\n`
)
assertCode(content)
// export default comments are set before __default__.
expect(content).toMatch(`/** Script docstring. */\nconst __default__ = {}`)
// defineOptions comments are set before export default.
expect(content).toMatch(
`/** Script setup docstring 1 */\n` +
`// Script setup docstring 2\n` +
`/**\n` +
` * Script setup docstring 3\n` +
` */\n` +
`export default /*#__PURE__*/Object.assign(__default__, {}, {`
)
})

it('should hoist comments even when called with no arguments', () => {
const { content } = compile(
`<script>\n` +
`/** Script docstring. */\n` +
`export default {}\n` +
`</script>\n` +
`\n` +
`<script setup>\n` +
`/** Script setup docstring 1 */\n` +
`// Script setup docstring 2\n` +
`/**\n` +
` * Script setup docstring 3\n` +
` */\n` +
`defineOptions()\n` +
`</script>\n`
)
assertCode(content)
// export default comments are set before __default__.
expect(content).toMatch(`/** Script docstring. */\nconst __default__ = {}`)
// defineOptions comments are set before export default.
expect(content).toMatch(
`/** Script setup docstring 1 */\n` +
`// Script setup docstring 2\n` +
`/**\n` +
` * Script setup docstring 3\n` +
` */\n` +
`export default /*#__PURE__*/Object.assign(__default__, {`
)
})

it('should emit an error with two defineOptions', () => {
expect(() =>
compile(`
Expand Down
26 changes: 22 additions & 4 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,14 @@ export function compileScript(
if (
processDefineProps(ctx, expr) ||
processDefineEmits(ctx, expr) ||
processDefineOptions(ctx, expr) ||
processDefineSlots(ctx, expr)
) {
ctx.s.remove(node.start! + startOffset, node.end! + startOffset)
} else if (processDefineOptions(ctx, expr, node.leadingComments ?? [])) {
ctx.s.remove(node.start! + startOffset, node.end! + startOffset)
for (const comment of node.leadingComments ?? []) {
ctx.s.remove(comment.start! + startOffset, comment.end! + startOffset)
}
} else if (processDefineExpose(ctx, expr)) {
// defineExpose({}) -> expose({})
const callee = (expr as CallExpression).callee
Expand Down Expand Up @@ -948,9 +952,23 @@ export function compileScript(
}

// 11. finalize default export
const genDefaultAs = options.genDefaultAs
? `const ${options.genDefaultAs} =`
: `export default`
const defaultLeadingComments = ctx.optionsLeadingComments
.map(c => {
switch (c.type) {
case 'CommentBlock':
return `/*${c.value}*/\n`
case 'CommentLine':
return `//${c.value}\n`
default:
c satisfies never
}
})
.join('')
const genDefaultAs =
defaultLeadingComments +
(options.genDefaultAs
? `const ${options.genDefaultAs} =`
: `export default`)

let runtimeOptions = ``
if (!ctx.hasDefaultExportName && filename && filename !== DEFAULT_FILENAME) {
Expand Down
9 changes: 8 additions & 1 deletion packages/compiler-sfc/src/script/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { CallExpression, Node, ObjectPattern, Program } from '@babel/types'
import {
CallExpression,
Comment,
Node,
ObjectPattern,
Program
} from '@babel/types'
import { SFCDescriptor } from '../parse'
import { generateCodeFrame } from '@vue/shared'
import { parse as babelParse, ParserPlugin } from '@babel/parser'
Expand Down Expand Up @@ -56,6 +62,7 @@ export class ScriptCompileContext {
modelDecls: Record<string, ModelDecl> = {}

// defineOptions
optionsLeadingComments: Comment[] = []
optionsRuntimeDecl: Node | undefined

// codegen
Expand Down
9 changes: 7 additions & 2 deletions packages/compiler-sfc/src/script/defineOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Node } from '@babel/types'
import { Node, Comment } from '@babel/types'
import { ScriptCompileContext } from './context'
import { isCallOf, unwrapTSNode } from './utils'
import { DEFINE_PROPS } from './defineProps'
Expand All @@ -10,7 +10,8 @@ export const DEFINE_OPTIONS = 'defineOptions'

export function processDefineOptions(
ctx: ScriptCompileContext,
node: Node
node: Node,
leadingComments: Comment[] = []
): boolean {
if (!isCallOf(node, DEFINE_OPTIONS)) {
return false
Expand All @@ -21,6 +22,10 @@ export function processDefineOptions(
if (node.typeParameters) {
ctx.error(`${DEFINE_OPTIONS}() cannot accept type arguments`, node)
}

// Always setting leadingComments, even when called with no arguments.
ctx.optionsLeadingComments = leadingComments

if (!node.arguments[0]) return true

ctx.hasDefineOptionsCall = true
Expand Down
Loading