Skip to content

Commit da195bd

Browse files
committed
fix: the special syntax in the script block will cause vue-eslint-parser
to fail parsing
1 parent 2e34df8 commit da195bd

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

src/runTransformation.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,42 @@ export default function runTransformation(
5050
transformation = transformationModule
5151
}
5252

53-
if (transformation.type === 'vueTransformation') {
54-
debug('TODO: Running VueTransformation')
53+
const { path, source } = fileInfo
54+
const extension = (/\.([^.]*)$/.exec(path) || [])[0]
55+
let lang = extension.slice(1)
56+
let descriptor: SFCDescriptor
5557

58+
if (transformation.type === 'vueTransformation' && extension === '.vue') {
59+
debug('Running VueTransformation')
60+
61+
descriptor = parseSFC(source, { filename: path }).descriptor
62+
63+
// skip .vue files without template block
64+
if (!descriptor.template) {
65+
return source
66+
}
67+
68+
fileInfo.source = `<template>${descriptor.template.content}</template>`
5669
const out = transformation(fileInfo, params)
70+
71+
if (!out) {
72+
return source
73+
}
74+
75+
// need to reconstruct the .vue file from descriptor blocks
76+
if (extension === '.vue') {
77+
if (out === descriptor!.template!.content) {
78+
return source // skipped, don't bother re-stringifying
79+
}
80+
// remove redundant <template> tag
81+
descriptor!.template!.content = out.slice(11, -11)
82+
return stringifySFC(descriptor!)
83+
}
84+
5785
return out
5886
} else {
5987
debug('Running jscodeshift transform')
6088

61-
const { path, source } = fileInfo
62-
const extension = (/\.([^.]*)$/.exec(path) || [])[0]
63-
let lang = extension.slice(1)
64-
65-
let descriptor: SFCDescriptor
6689
if (extension === '.vue') {
6790
descriptor = parseSFC(source, { filename: path }).descriptor
6891

0 commit comments

Comments
 (0)