Skip to content

Commit 25f97a5

Browse files
committed
fix(compiler-sfc): fix rewriteDefault edge cases
close #13060 close #12892 close #12906
1 parent 10c2a87 commit 25f97a5

File tree

3 files changed

+79
-7
lines changed

3 files changed

+79
-7
lines changed

packages/compiler-sfc/src/compileScript.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1575,7 +1575,7 @@ function extractRuntimeEmits(
15751575
}
15761576

15771577
function extractEventNames(
1578-
eventName: Identifier | RestElement,
1578+
eventName: ArrayPattern | Identifier | ObjectPattern | RestElement,
15791579
emits: Set<string>
15801580
) {
15811581
if (

packages/compiler-sfc/src/rewriteDefault.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,14 @@ export function rewriteDefault(
4242
}).program.body
4343
ast.forEach(node => {
4444
if (node.type === 'ExportDefaultDeclaration') {
45-
if (node.declaration.type === 'ClassDeclaration') {
46-
s.overwrite(node.start!, node.declaration.id.start!, `class `)
45+
if (node.declaration.type === 'ClassDeclaration' && node.declaration.id) {
46+
let start: number =
47+
node.declaration.decorators && node.declaration.decorators.length > 0
48+
? node.declaration.decorators[
49+
node.declaration.decorators.length - 1
50+
].end!
51+
: node.start!
52+
s.overwrite(start, node.declaration.id.start!, ` class `)
4753
s.append(`\nconst ${as} = ${node.declaration.id.name}`)
4854
} else {
4955
s.overwrite(node.start!, node.declaration.start!, `const ${as} = `)

packages/compiler-sfc/test/rewriteDefault.spec.ts

+70-4
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ describe('compiler sfc: rewriteDefault', () => {
190190
).toMatchInlineSnapshot(`
191191
"/*
192192
export default class Foo {}*/
193-
class Bar {}
193+
class Bar {}
194194
const script = Bar"
195195
`)
196196
})
@@ -206,7 +206,10 @@ describe('compiler sfc: rewriteDefault', () => {
206206

207207
test('@Component\nexport default class w/ comments', async () => {
208208
expect(
209-
rewriteDefault(`// export default\n@Component\nexport default class Foo {}`, 'script')
209+
rewriteDefault(
210+
`// export default\n@Component\nexport default class Foo {}`,
211+
'script'
212+
)
210213
).toMatchInlineSnapshot(`
211214
"// export default
212215
@Component
@@ -231,15 +234,78 @@ describe('compiler sfc: rewriteDefault', () => {
231234
test('@Component\nexport default class w/ comments 3', async () => {
232235
expect(
233236
rewriteDefault(
234-
`/*\n@Component\nexport default class Foo {}*/\n` + `export default class Bar {}`,
237+
`/*\n@Component\nexport default class Foo {}*/\n` +
238+
`export default class Bar {}`,
235239
'script'
236240
)
237241
).toMatchInlineSnapshot(`
238242
"/*
239243
@Component
240244
export default class Foo {}*/
241-
class Bar {}
245+
class Bar {}
242246
const script = Bar"
243247
`)
244248
})
249+
250+
// #13060
251+
test('@Component\nexport default class w/ comments 4', async () => {
252+
expect(
253+
rewriteDefault(
254+
`@Component
255+
export default class App extends Vue {
256+
/* default <- This word means my component is not built correctly */
257+
@Prop({ type: String, required: true })
258+
protected someString: string;
259+
}`,
260+
'script'
261+
)
262+
).toMatchInlineSnapshot(`
263+
"@Component
264+
class App extends Vue {
265+
/* default <- This word means my component is not built correctly */
266+
@Prop({ type: String, required: true })
267+
protected someString: string;
268+
}
269+
const script = App"
270+
`)
271+
})
272+
273+
// #12892
274+
test('@Component\nexport default class w/ comments 5', async () => {
275+
expect(
276+
rewriteDefault(
277+
`@Component({})
278+
export default class HelloWorld extends Vue {
279+
test = "";
280+
mounted() {
281+
console.log("mounted!");
282+
this.test = "Hallo Welt!";
283+
}
284+
exportieren(): void {
285+
// do nothing
286+
}
287+
defaultWert(): void {
288+
// do nothing
289+
}
290+
}`,
291+
'script',
292+
['typescript', 'decorators-legacy']
293+
)
294+
).toMatchInlineSnapshot(`
295+
"@Component({}) class HelloWorld extends Vue {
296+
test = "";
297+
mounted() {
298+
console.log("mounted!");
299+
this.test = "Hallo Welt!";
300+
}
301+
exportieren(): void {
302+
// do nothing
303+
}
304+
defaultWert(): void {
305+
// do nothing
306+
}
307+
}
308+
const script = HelloWorld"
309+
`)
310+
})
245311
})

0 commit comments

Comments
 (0)