-
Notifications
You must be signed in to change notification settings - Fork 644
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: switch to new compute node helper
- Loading branch information
1 parent
d45962d
commit b2e70bc
Showing
63 changed files
with
529 additions
and
213 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@marko/translator-default": minor | ||
"@marko/babel-utils": minor | ||
"@marko/compiler": minor | ||
"marko": minor | ||
--- | ||
|
||
Add compute node helper to replace babels `evaluate` helper. This helper is less aggressive and doesn't suffer from the false positives that popped up with babels version. |
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,6 @@ | ||
--- | ||
"@marko/translator-default": patch | ||
"marko": patch | ||
--- | ||
|
||
Avoid adding trailing semicolon to style attribute output. |
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,194 @@ | ||
/** | ||
* @param {import("@babel/types").Node} node | ||
*/ | ||
export function computeNode(node) { | ||
switch (node.type) { | ||
case "StringLiteral": | ||
case "NumericLiteral": | ||
case "BooleanLiteral": | ||
return { value: node.value }; | ||
case "RegExpLiteral": | ||
return { value: new RegExp(node.pattern, node.flags) }; | ||
case "NullLiteral": | ||
return { value: null }; | ||
case "Identifier": | ||
switch (node.name) { | ||
case "undefined": | ||
return { value: undefined }; | ||
case "NaN": | ||
return { value: NaN }; | ||
case "Infinity": | ||
return { value: Infinity }; | ||
default: | ||
return; | ||
} | ||
case "BigIntLiteral": | ||
return { value: BigInt(node.value) }; | ||
case "BinaryExpression": { | ||
const left = computeNode(node.left); | ||
if (!left) return; | ||
const right = computeNode(node.right); | ||
if (!right) return; | ||
switch (node.operator) { | ||
case "+": | ||
return { value: left.value + right.value }; | ||
case "-": | ||
return { value: left.value - right.value }; | ||
case "*": | ||
return { value: left.value * right.value }; | ||
case "/": | ||
return { value: left.value / right.value }; | ||
case "%": | ||
return { value: left.value % right.value }; | ||
case "**": | ||
return { value: left.value ** right.value }; | ||
case "|": | ||
return { value: left.value | right.value }; | ||
case "&": | ||
return { value: left.value & right.value }; | ||
case "^": | ||
return { value: left.value ^ right.value }; | ||
case "<<": | ||
return { value: left.value << right.value }; | ||
case ">>": | ||
return { value: left.value >> right.value }; | ||
case ">>>": | ||
return { value: left.value >>> right.value }; | ||
case "==": | ||
return { value: left.value == right.value }; | ||
case "!=": | ||
return { value: left.value != right.value }; | ||
case "===": | ||
return { value: left.value === right.value }; | ||
case "!==": | ||
return { value: left.value !== right.value }; | ||
case "<": | ||
return { value: left.value < right.value }; | ||
case "<=": | ||
return { value: left.value <= right.value }; | ||
case ">": | ||
return { value: left.value > right.value }; | ||
case ">=": | ||
return { value: left.value >= right.value }; | ||
default: | ||
return; | ||
} | ||
} | ||
case "UnaryExpression": { | ||
const arg = computeNode(node.argument); | ||
if (!arg) return; | ||
switch (node.operator) { | ||
case "+": | ||
return { value: +arg.value }; | ||
case "-": | ||
return { value: -arg.value }; | ||
case "~": | ||
return { value: ~arg.value }; | ||
case "!": | ||
return { value: !arg.value }; | ||
case "typeof": | ||
return { value: typeof arg.value }; | ||
case "void": | ||
return { value: void arg.value }; | ||
default: | ||
return; | ||
} | ||
} | ||
case "LogicalExpression": { | ||
const left = computeNode(node.left); | ||
if (!left) return; | ||
const right = computeNode(node.right); | ||
if (!right) return; | ||
switch (node.operator) { | ||
case "&&": | ||
return { value: left.value && right.value }; | ||
case "||": | ||
return { value: left.value || right.value }; | ||
case "??": | ||
return { value: left.value ?? right.value }; | ||
default: | ||
return; | ||
} | ||
} | ||
case "ConditionalExpression": { | ||
const test = computeNode(node.test); | ||
if (!test) return; | ||
const consequent = computeNode(node.consequent); | ||
if (!consequent) return; | ||
const alternate = computeNode(node.alternate); | ||
if (!alternate) return; | ||
return { value: test.value ? consequent.value : alternate.value }; | ||
} | ||
case "TemplateLiteral": { | ||
let value = node.quasis[0].cooked; | ||
for (let i = 0; i < node.expressions.length; i++) { | ||
const expr = computeNode(node.expressions[i]); | ||
if (!expr) return; | ||
value += expr.value + node.quasis[i + 1].cooked; | ||
} | ||
return { value }; | ||
} | ||
case "ObjectExpression": { | ||
const value = {}; | ||
for (const prop of node.properties) { | ||
if (prop.decorators) return; | ||
switch (prop.type) { | ||
case "ObjectProperty": { | ||
let key; | ||
if (prop.computed) { | ||
const keyNode = computeNode(prop.key); | ||
if (!keyNode) return; | ||
key = keyNode.value + ""; | ||
} else { | ||
switch (prop.key.type) { | ||
case "Identifier": | ||
key = prop.key.name; | ||
break; | ||
case "StringLiteral": | ||
key = prop.key.value; | ||
break; | ||
default: | ||
return; | ||
} | ||
} | ||
|
||
const propValue = computeNode(prop.value); | ||
if (!propValue) return; | ||
value[key] = propValue.value; | ||
break; | ||
} | ||
case "SpreadElement": { | ||
const arg = computeNode(prop.argument); | ||
if (!arg) return; | ||
Object.assign(value, arg.value); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return { value }; | ||
} | ||
case "ArrayExpression": { | ||
const value = []; | ||
for (const elem of node.elements) { | ||
if (elem) { | ||
if (elem.type === "SpreadElement") { | ||
const arg = computeNode(elem.argument); | ||
if (typeof arg?.value?.[Symbol.iterator] !== "function") return; | ||
for (const item of arg.value) { | ||
value.push(item); | ||
} | ||
} else { | ||
const elemValue = computeNode(elem); | ||
if (!elemValue) return; | ||
value.push(elemValue.value); | ||
} | ||
} else { | ||
value.length++; | ||
} | ||
} | ||
|
||
return { value }; | ||
} | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
packages/marko/test/render/fixtures/attrs-normalize-for-native-tag/expected.html
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 |
---|---|---|
@@ -1 +1 @@ | ||
<svg data-dash="case" viewBox="0 0 0 100" class="a" style="color:green;"></svg> | ||
<svg data-dash=case viewBox="0 0 0 100" class=a style=color:green /> |
2 changes: 1 addition & 1 deletion
2
packages/marko/test/render/fixtures/attrs-normalize-for-native-tag/vdom-expected.html
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 |
---|---|---|
@@ -1 +1 @@ | ||
<svg:svg class="a" data-dash="case" style="color:green;" viewBox="0 0 0 100"> | ||
<svg:svg class="a" data-dash="case" style="color:green" viewBox="0 0 0 100"> |
2 changes: 1 addition & 1 deletion
2
packages/marko/test/render/fixtures/dynamic-tag-object-class-style/expected.html
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 |
---|---|---|
@@ -1 +1 @@ | ||
<hello-world class="foo bar" style="color:blue;">My nested content</hello-world> | ||
<hello-world class="foo bar" style=color:blue>My nested content</hello-world> |
2 changes: 1 addition & 1 deletion
2
packages/marko/test/render/fixtures/dynamic-tag-object-class-style/vdom-expected.html
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
<HELLO-WORLD class="foo bar" style="color:blue;"> | ||
<HELLO-WORLD class="foo bar" style="color:blue"> | ||
"My nested content" |
2 changes: 1 addition & 1 deletion
2
packages/marko/test/render/fixtures/shorthand-div-id-dynamic/expected.html
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 |
---|---|---|
@@ -1 +1 @@ | ||
<div style="color: red;" id="foo-Frank">Hello Frank!</div> | ||
<div style="color: red" id="foo-Frank">Hello Frank!</div> |
2 changes: 1 addition & 1 deletion
2
packages/marko/test/render/fixtures/shorthand-div-id-dynamic/vdom-expected.html
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
<DIV id="foo-Frank" style="color: red;"> | ||
<DIV id="foo-Frank" style="color: red"> | ||
"Hello Frank!" |
2 changes: 1 addition & 1 deletion
2
packages/marko/test/render/fixtures/shorthand-div-id/expected.html
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 |
---|---|---|
@@ -1 +1 @@ | ||
<div style="color: red;" id="foo">Hello Frank!</div> | ||
<div style="color: red" id="foo">Hello Frank!</div> |
2 changes: 1 addition & 1 deletion
2
packages/marko/test/render/fixtures/shorthand-div-id/vdom-expected.html
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
<DIV id="foo" style="color: red;"> | ||
<DIV id="foo" style="color: red"> | ||
"Hello Frank!" |
2 changes: 1 addition & 1 deletion
2
packages/marko/test/render/fixtures/shorthand-div.foo/expected.html
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 |
---|---|---|
@@ -1 +1 @@ | ||
<div style="color: red;" class="foo">Hello Frank!</div> | ||
<div style="color: red" class="foo">Hello Frank!</div> |
2 changes: 1 addition & 1 deletion
2
packages/marko/test/render/fixtures/shorthand-div.foo/vdom-expected.html
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
<DIV class="foo" style="color: red;"> | ||
<DIV class="foo" style="color: red"> | ||
"Hello Frank!" |
2 changes: 1 addition & 1 deletion
2
packages/marko/test/render/fixtures/spread-attribute-class-style-html-tag/expected.html
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 |
---|---|---|
@@ -1 +1 @@ | ||
<div class="foo bar" style="color:blue;">Hello spread</div> | ||
<div class="foo bar" style="color:blue">Hello spread</div> |
2 changes: 1 addition & 1 deletion
2
packages/marko/test/render/fixtures/spread-attribute-class-style-html-tag/vdom-expected.html
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
<DIV class="foo bar" style="color:blue;"> | ||
<DIV class="foo bar" style="color:blue"> | ||
"Hello spread" |
2 changes: 1 addition & 1 deletion
2
packages/marko/test/render/fixtures/style-attr-array-mixed/expected.html
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 |
---|---|---|
@@ -1 +1 @@ | ||
<div style="color:red;font-weight:bold;background-color:blue;"></div> | ||
<div style="color:red;font-weight:bold;background-color:blue"></div> |
2 changes: 1 addition & 1 deletion
2
packages/marko/test/render/fixtures/style-attr-array-mixed/vdom-expected.html
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 |
---|---|---|
@@ -1 +1 @@ | ||
<DIV style="color:red;font-weight:bold;background-color:blue;"> | ||
<DIV style="color:red;font-weight:bold;background-color:blue"> |
Oops, something went wrong.