Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit c4d4753

Browse files
committed
fix: do not read comments in SFC, close #61
1 parent 7397bad commit c4d4753

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@
4343
"test:update": "jest -u"
4444
},
4545
"dependencies": {
46+
"@antfu/utils": "^0.3.0",
4647
"@babel/core": "^7.15.5",
4748
"@babel/generator": "^7.15.4",
4849
"@babel/parser": "^7.15.6",
4950
"@babel/traverse": "^7.15.4",
5051
"@babel/types": "^7.15.6",
52+
"@rollup/pluginutils": "^4.1.1",
5153
"@vue/ref-transform": "^3.2.11",
5254
"@vue/shared": "^3.2.11",
5355
"defu": "^5.0.0",
5456
"htmlparser2": "^7.1.2",
5557
"magic-string": "^0.25.7",
56-
"@rollup/pluginutils": "^4.1.1",
57-
"@antfu/utils": "^0.3.0",
5858
"unplugin": "^0.2.11"
5959
},
6060
"devDependencies": {

src/core/parseSFC.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { ParsedSFC, ScriptSetupTransformOptions, ScriptTagMeta } from '../types'
55
import { getIdentifierUsages } from './identifiers'
66
import { parse } from './babel'
77

8+
const multilineCommentsRE = /\/\*\s(.|[\r\n])*?\*\//gm
9+
const singlelineCommentsRE = /\/\/\s.*/g
10+
811
export function parseSFC(code: string, id?: string, options?: ScriptSetupTransformOptions): ParsedSFC {
912
const components = new Set<string>()
1013
const expressions = new Set<string>()
@@ -14,6 +17,10 @@ export function parseSFC(code: string, id?: string, options?: ScriptSetupTransfo
1417
let inScriptSetup = false
1518
let inScript = false
1619

20+
const striped = code
21+
.replace(multilineCommentsRE, r => ' '.repeat(r.length))
22+
.replace(singlelineCommentsRE, r => ' '.repeat(r.length))
23+
1724
const scriptSetup: ScriptTagMeta = {
1825
start: 0,
1926
end: 0,
@@ -122,7 +129,7 @@ export function parseSFC(code: string, id?: string, options?: ScriptSetupTransfo
122129
if (name === 'template') {
123130
templateLevel -= 1
124131
if (templateLevel === 0 && pugStart != null)
125-
handlePugTemplate(code.slice(pugStart, parser.startIndex), id)
132+
handlePugTemplate(striped.slice(pugStart, parser.startIndex), id)
126133
}
127134

128135
if (inScriptSetup && name === 'script') {
@@ -140,7 +147,7 @@ export function parseSFC(code: string, id?: string, options?: ScriptSetupTransfo
140147
},
141148
}, htmlParserOptions)
142149

143-
parser.write(code)
150+
parser.write(striped)
144151
parser.end()
145152

146153
expressions.forEach((exp) => {

test/__snapshots__/transform.test.ts.snap

+10-4
Original file line numberDiff line numberDiff line change
@@ -575,12 +575,18 @@ export default __sfc_main;
575575
576576
exports[`transform fixtures test/fixtures/ScriptOnly.vue 1`] = `
577577
"<script lang=\\"ts\\">
578-
import { defineComponent } from '@vue/composition-api'
578+
// <script setup> in comment should not be matched
579+
import { defineComponent } from '@vue/composition-api';
579580
580-
export default defineComponent({
581-
name: 'Hi',
582-
})
581+
const __sfc_main = defineComponent({
582+
name: 'Hi'
583+
});
584+
585+
export default __sfc_main;
583586
</script>
587+
588+
589+
<!-- <script setup> -->
584590
"
585591
`;
586592

test/fixtures/ScriptOnly.vue

+3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<script lang="ts">
2+
// <script setup> in comment should not be matched
23
import { defineComponent } from '@vue/composition-api'
34
45
export default defineComponent({
56
name: 'Hi',
67
})
78
</script>
9+
10+
<!-- <script setup> -->

0 commit comments

Comments
 (0)