-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: merge neighboring text and expressions into 1 text node
- Loading branch information
Showing
36 changed files
with
558 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function is_string(node) { | ||
return node.type === 'TemplateLiteral' || (node.type === 'Literal' && typeof node.value === 'string'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { walk } from 'estree-walker'; | ||
import { Text, MustacheTag } from '../../interfaces'; | ||
|
||
export default function optimise(ast) { | ||
walk(ast, { | ||
enter(node: any) { | ||
if (node.type === 'Element') { | ||
optimise_text_content(node.children); | ||
} | ||
}, | ||
}); | ||
} | ||
|
||
const text_like_node_type = new Set(['MustacheTag', 'Text']); | ||
|
||
function optimise_text_content(children) { | ||
let start = 0; | ||
let end = 0; | ||
|
||
do { | ||
while ( | ||
start < children.length && | ||
!text_like_node_type.has(children[start].type) | ||
) | ||
start++; | ||
|
||
end = start; | ||
|
||
while (end < children.length && text_like_node_type.has(children[end].type)) | ||
end++; | ||
|
||
if (end > start) { | ||
const merged = merge_text_siblings(children.slice(start, end)); | ||
children.splice(start, end - start, ...merged); | ||
start = end; | ||
} | ||
} while (start < children.length); | ||
} | ||
|
||
function merge_text_siblings(children: Array<Text | MustacheTag>) { | ||
if (children.length < 3) { | ||
return children; | ||
} | ||
|
||
const literal = { | ||
type: 'TemplateLiteral', | ||
expressions: [], | ||
quasis: [], | ||
}; | ||
const state = { | ||
quasi: { | ||
type: 'TemplateElement', | ||
value: { raw: '' }, | ||
start: children[0].start, | ||
end: children[0].start | ||
}, | ||
}; | ||
|
||
for (const child of children) { | ||
if (child.type === 'MustacheTag') { | ||
literal.quasis.push(state.quasi); | ||
literal.expressions.push(child.expression); | ||
state.quasi = { | ||
type: 'TemplateElement', | ||
value: { raw: '' }, | ||
// @ts-ignore | ||
start: child.expression.end + 1, | ||
// @ts-ignore | ||
end: child.expression.end + 1 | ||
}; | ||
} else if (child.type === 'Text') { | ||
state.quasi.value.raw += child.data; | ||
state.quasi.end = child.end; | ||
} | ||
} | ||
|
||
literal.quasis.push(state.quasi); | ||
|
||
return [{ | ||
type: 'MustacheTag', | ||
expression: literal, | ||
start: children[0].start, | ||
end: children[children.length - 1].end, | ||
}]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.