Skip to content

Commit 814732c

Browse files
author
kingwl
committed
refactor comments options with ASTText type
1 parent ef9cc17 commit 814732c

File tree

6 files changed

+11
-21
lines changed

6 files changed

+11
-21
lines changed

flow/compiler.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ declare type ASTDirective = {
6464
modifiers: ?ASTModifiers;
6565
};
6666

67-
declare type ASTNode = ASTElement | ASTText | ASTExpression | ASTComment;
67+
declare type ASTNode = ASTElement | ASTText | ASTExpression;
6868

6969
declare type ASTElement = {
7070
type: 1;
@@ -154,15 +154,7 @@ declare type ASTText = {
154154
type: 3;
155155
text: string;
156156
static?: boolean;
157-
// 2.4 ssr optimization
158-
ssrOptimizability?: number;
159-
};
160-
161-
declare type ASTComment = {
162-
type: 4;
163-
text: string;
164-
static?: boolean;
165-
157+
isComment?: boolean;
166158
// 2.4 ssr optimization
167159
ssrOptimizability?: number;
168160
};

src/compiler/codegen/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ function needsNormalization (el: ASTElement): boolean {
423423
function genNode (node: ASTNode, state: CodegenState): string {
424424
if (node.type === 1) {
425425
return genElement(node, state)
426-
} if (node.type === 4) {
426+
} if (node.type === 3 && node.isComment) {
427427
return genComment(node)
428428
} else {
429429
return genText(node)
@@ -437,7 +437,7 @@ export function genText (text: ASTText | ASTExpression): string {
437437
})`
438438
}
439439

440-
export function genComment (comment: ASTComment): string {
440+
export function genComment (comment: ASTText): string {
441441
return `_e('${comment.text}')`
442442
}
443443

src/compiler/optimizer.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function markStaticRoots (node: ASTNode, isInFor: boolean) {
7777
// outweigh the benefits and it's better off to just always render it fresh.
7878
if (node.static && node.children.length && !(
7979
node.children.length === 1 &&
80-
(node.children[0].type === 3 || node.children[0].type === 4)
80+
node.children[0].type === 3
8181
)) {
8282
node.staticRoot = true
8383
return
@@ -104,9 +104,6 @@ function isStatic (node: ASTNode): boolean {
104104
if (node.type === 3) { // text
105105
return true
106106
}
107-
if (node.type === 4) { // comment
108-
return true
109-
}
110107
return !!(node.pre || (
111108
!node.hasBindings && // no dynamic bindings
112109
!node.if && !node.for && // not v-if or v-for or v-else

src/compiler/parser/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,9 @@ export function parse (
278278
},
279279
comment (text: string) {
280280
currentParent.children.push({
281-
type: 4,
282-
text
281+
type: 3,
282+
text,
283+
isComment: true
283284
})
284285
}
285286
})

src/server/optimizing-compiler/optimizer.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ function optimizeSiblings (el) {
107107
}
108108

109109
function isUnOptimizableTree (node: ASTNode): boolean {
110-
// text or expression or comment
111-
if (node.type === 2 || node.type === 3 || node.type === 4) {
110+
if (node.type === 2 || node.type === 3) { // text or expression
112111
return false
113112
}
114113
return (

test/unit/modules/compiler/parser.spec.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,8 @@ describe('parser', () => {
567567
expect(ast.children.length).toBe(2)
568568
expect(ast.children[0].type).toBe(3)
569569
expect(ast.children[0].text).toBe('123')
570-
expect(ast.children[1].type).toBe(4)
570+
expect(ast.children[1].type).toBe(3) // parse comment with ASTText
571+
expect(ast.children[1].isComment).toBe(true) // parse comment with ASTText
571572
expect(ast.children[1].text).toBe('comment here')
572573
})
573574
})

0 commit comments

Comments
 (0)