Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spread-keyword-and-bang-runs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"htmljs-parser": minor
---

A keyword operator directly after a spread's `...` is an operator, so `<div ...new Attrs()/>` is one spread rather than `...new` plus an attribute `Attrs()`. A run of non-null assertions (`x!!`) and one after a string or template literal (`"s"!`, `` `s`! ``) now ends an unenclosed value like `x!` does.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
1╭─ <div ...new Attrs() a/>
│ ││ │ │ │╰─ openTagEnd:selfClosed "/>"
│ ││ │ │ ╰─ attrName
│ ││ │ ╰─ attrSpread.value "new Attrs()"
│ ││ ╰─ attrSpread "...new Attrs()"
│ │╰─ tagName "div"
╰─ ╰─ openTagStart
2╭─ <div ...await load() b/>
│ ││ │ │ │╰─ openTagEnd:selfClosed "/>"
│ ││ │ │ ╰─ attrName
│ ││ │ ╰─ attrSpread.value "await load()"
│ ││ ╰─ attrSpread "...await load()"
│ │╰─ tagName "div"
╰─ ╰─ openTagStart
3╭─ <div ...input.new c/>
│ ││ │ │ │╰─ openTagEnd:selfClosed "/>"
│ ││ │ │ ╰─ attrName
│ ││ │ ╰─ attrSpread.value "input.new"
│ ││ ╰─ attrSpread "...input.new"
│ │╰─ tagName "div"
╰─ ╰─ openTagStart
4╰─
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div ...new Attrs() a/>
<div ...await load() b/>
<div ...input.new c/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
1╭─ <div a=x!! b="s"! c=`t`! d=f()!! e=!!x f/>
│ ││ │││ │││ │││ │││ │││ │╰─ openTagEnd:selfClosed "/>"
│ ││ │││ │││ │││ │││ │││ ╰─ attrName
│ ││ │││ │││ │││ │││ ││╰─ attrValue.value "!!x"
│ ││ │││ │││ │││ │││ │╰─ attrValue "=!!x"
│ ││ │││ │││ │││ │││ ╰─ attrName
│ ││ │││ │││ │││ ││╰─ attrValue.value "f()!!"
│ ││ │││ │││ │││ │╰─ attrValue "=f()!!"
│ ││ │││ │││ │││ ╰─ attrName
│ ││ │││ │││ ││╰─ attrValue.value "`t`!"
│ ││ │││ │││ │╰─ attrValue "=`t`!"
│ ││ │││ │││ ╰─ attrName
│ ││ │││ ││╰─ attrValue.value "\"s\"!"
│ ││ │││ │╰─ attrValue "=\"s\"!"
│ ││ │││ ╰─ attrName
│ ││ ││╰─ attrValue.value "x!!"
│ ││ │╰─ attrValue "=x!!"
│ ││ ╰─ attrName
│ │╰─ tagName "div"
╰─ ╰─ openTagStart
2╭─ static const g = h!!
╰─ ╰─ tagName "static"
3╭─ static const i = "j"!
│ ├─ openTagEnd
╰─ ╰─ tagName "static"
4╭─ <div k=typeof!! l m/>
│ ││ │││ │╰─ openTagEnd:selfClosed "/>"
│ ││ │││ ╰─ attrName
│ ││ ││╰─ attrValue.value "typeof!! l"
│ ││ │╰─ attrValue "=typeof!! l"
│ ││ ╰─ attrName
│ │╰─ tagName "div"
│ ├─ openTagEnd
╰─ ╰─ openTagStart
5╰─
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div a=x!! b="s"! c=`t`! d=f()!! e=!!x f/>
static const g = h!!
static const i = "j"!
<div k=typeof!! l m/>
34 changes: 25 additions & 9 deletions src/states/EXPRESSION.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,17 +483,28 @@ function lookBehindForOperator(
return curPos;

case CODE.EXCLAMATION: {
// After an operand, `!` is a TypeScript non-null assertion (postfix);
// after a keyword operator (`typeof!a`, `a in!b`) it is the prefix `!`.
const prevCode = data.charCodeAt(curPos - 1);
switch (prevCode) {
// After an operand, `!` is a TypeScript non-null assertion (postfix),
// as is each `!` of a run after one (`x!!`); after a keyword operator
// (`typeof!a`, `a in!b`) it is the prefix `!`.
let operandEnd = curPos - 1;
while (data.charCodeAt(operandEnd) === CODE.EXCLAMATION) operandEnd--;
const operandCode = data.charCodeAt(operandEnd);
switch (operandCode) {
case CODE.CLOSE_PAREN:
case CODE.CLOSE_SQUARE_BRACKET:
case CODE.DOUBLE_QUOTE:
case CODE.SINGLE_QUOTE:
case CODE.BACKTICK:
return -1;
default:
return isWordCode(prevCode) &&
lookBehindForOperator(expression, data, curPos) === -1 &&
lookBehindForKeyword(data, curPos - 1, relationalKeywords) === -1
return isWordCode(operandCode) &&
lookBehindForOperator(expression, data, operandEnd + 1) === -1 &&
lookBehindForKeyword(
expression,
data,
operandEnd,
relationalKeywords,
) === -1
? -1
: curPos;
}
Expand Down Expand Up @@ -534,6 +545,7 @@ function lookBehindForOperator(
if (code < CODE.LOWER_A || code > CODE.LOWER_Z) return -1;

return lookBehindForKeyword(
expression,
data,
curPos,
expression.inType ? tsUnaryKeywords : jsUnaryKeywords,
Expand Down Expand Up @@ -670,16 +682,20 @@ function lookBehindWhile(
return 0;
}

// Returns where a whole keyword ending at `pos` starts, or -1.
// Returns where a whole keyword ending at `pos` starts, or -1. A keyword that
// starts the expression is whole even after a `.` (a spread's `...new x`).
function lookBehindForKeyword(
expression: ExpressionMeta,
data: string,
pos: number,
keywords: readonly string[],
) {
for (const keyword of keywords) {
const keywordPos = lookBehindFor(data, pos, keyword);
if (keywordPos !== -1) {
return isWordOrPeriodCode(data.charCodeAt(keywordPos - 1))
return keywordPos < expression.start ||
(keywordPos > expression.start &&
isWordOrPeriodCode(data.charCodeAt(keywordPos - 1)))
? -1
: keywordPos;
}
Expand Down
Loading