Skip to content

Commit 5e8fbf4

Browse files
committed
Chore: improve test coverage
1 parent c26e093 commit 5e8fbf4

File tree

70 files changed

+19067
-39
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+19067
-39
lines changed

lib/parse-component.js

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,21 @@ function parseScriptNode(scriptNode, scriptParser, tokenGenerator) {
8282

8383
// Needs the tokens of start/end tags for `lines-around-*` rules to work
8484
// correctly.
85-
const startTag = tokenGenerator.createToken(
86-
"Punctuator",
87-
startLoc.startOffset,
88-
startLoc.endOffset
89-
)
90-
const endTag = tokenGenerator.createToken(
91-
"Punctuator",
92-
endLoc.startOffset,
93-
endLoc.endOffset
94-
)
95-
ast.start = startTag.end
96-
ast.tokens.unshift(startTag)
97-
ast.tokens.push(endTag)
85+
if (ast.tokens) {
86+
const startTag = tokenGenerator.createToken(
87+
"Punctuator",
88+
startLoc.startOffset,
89+
startLoc.endOffset
90+
)
91+
const endTag = tokenGenerator.createToken(
92+
"Punctuator",
93+
endLoc.startOffset,
94+
endLoc.endOffset
95+
)
96+
ast.start = startTag.end
97+
ast.tokens.unshift(startTag)
98+
ast.tokens.push(endTag)
99+
}
98100

99101
return ast
100102
}
@@ -137,33 +139,37 @@ function parse(code, options) {
137139
const templateOptions = options.template || {}
138140
const isVue = path.extname(filePath || "unknown.js") === ".vue"
139141
const scriptParser = new ScriptParser(code, scriptOptions)
140-
141-
if (!isVue) {
142-
return {
143-
ast: scriptParser._parseScript(code),
144-
services: {registerTemplateBodyVisitor},
145-
}
142+
let ast = null
143+
144+
if (isVue) {
145+
const tokenGenerator = new TokenGenerator(code)
146+
const info = parseComponent(code)
147+
148+
// <script>
149+
ast = (info.script != null)
150+
? parseScriptNode(info.script, scriptParser, tokenGenerator)
151+
: scriptParser._parseScript("")
152+
153+
// <template>
154+
ast.templateBody = info.template && parseTemplateNode(
155+
info.template,
156+
scriptParser,
157+
tokenGenerator,
158+
templateOptions
159+
)
146160
}
147-
const info = parseComponent(code)
148-
if (!info.script) {
149-
return {
150-
ast: scriptParser._parseScript(""),
151-
services: {registerTemplateBodyVisitor},
152-
}
161+
else {
162+
ast = scriptParser._parseScript(code)
163+
ast.templateBody = null
153164
}
154165

155-
const tokenGenerator = new TokenGenerator(code)
156-
const ast = parseScriptNode(
157-
info.script,
158-
scriptParser,
159-
tokenGenerator
160-
)
161-
ast.templateBody = info.template && parseTemplateNode(
162-
info.template,
163-
scriptParser,
164-
tokenGenerator,
165-
templateOptions
166-
)
166+
// Ensure ast.comments and ast.tokens.
167+
if (ast.comments == null) {
168+
ast.comments = []
169+
}
170+
if (ast.tokens == null) {
171+
ast.tokens = []
172+
}
167173

168174
return {
169175
ast,

lib/register-template-body-visitor.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,4 @@ module.exports = (context, visitor) => {
180180
emitter.on(selector, visitor[selector])
181181
}
182182
}
183+
module.exports.traverse = traverse

lib/transform-html.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ class HTMLTransformer {
292292
*/
293293
createVAttributeValue(start, end, value) {
294294
const literal = this.appendToken("VAttributeValue", start, end)
295-
return {
295+
return literal && {
296296
parent: null,
297297
type: literal.type,
298298
range: literal.range,
@@ -349,7 +349,10 @@ class HTMLTransformer {
349349
result.value = directive
350350
? this.createVExpressionContainer(i, end, quoteSize)
351351
: this.createVAttributeValue(i, end, value)
352-
result.value.parent = result
352+
353+
if (result.value != null) {
354+
result.value.parent = result
355+
}
353356
}
354357

355358
return result

0 commit comments

Comments
 (0)