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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"release": "yarn build && standard-version"
},
"devDependencies": {
"@angular/compiler": "19.2.2",
"@angular/compiler": "20.0.0-next.8",
"@babel/code-frame": "7.26.2",
"@babel/parser": "7.26.10",
"@babel/types": "7.26.10",
Expand All @@ -50,7 +50,7 @@
"vitest": "3.0.8"
},
"peerDependencies": {
"@angular/compiler": ">=19.2.2"
"@angular/compiler": ">=19.2.2 || ^20.0.0"
},
"engines": {
"node": ">= 20"
Expand Down
55 changes: 44 additions & 11 deletions src/transform-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class Transformer extends Source {
if (node instanceof angular.Interpolation) {
const { expressions } = node;

// istanbul ignore next 3
/* c8 ignore next 3 */
if (expressions.length !== 1) {
throw new Error("Unexpected 'Interpolation'");
}
Expand Down Expand Up @@ -373,7 +373,7 @@ class Transformer extends Source {
{ type: 'Identifier', name: 'undefined', ...node.sourceSpan },
{ hasParentParens: isInParentParens },
);
// istanbul ignore next
/* c8 ignore next 4 */
default:
throw new Error(
`Unexpected LiteralPrimitive value type ${typeof value}`,
Expand Down Expand Up @@ -427,20 +427,35 @@ class Transformer extends Source {
);
}

const isPrefixNot = node instanceof angular.PrefixNot;
if (isPrefixNot || node instanceof angular.TypeofExpression) {
const expression = this.#transform<babel.Expression>(node.expression);
if (
node instanceof angular.PrefixNot ||
node instanceof angular.TypeofExpression ||
node instanceof angular.VoidExpression
) {
const operator =
node instanceof angular.PrefixNot
? '!'
: node instanceof angular.TypeofExpression
? 'typeof'
: node instanceof angular.VoidExpression
? 'void'
: /* c8 ignore next */
undefined;

/* c8 ignore next 3 */
if (!operator) {
throw new Error('Unexpected expression.');
}

const operator = isPrefixNot ? '!' : 'typeof';
let { start } = node.sourceSpan;

if (!isPrefixNot) {
if (operator === 'typeof' || operator === 'void') {
const index = this.text.lastIndexOf(operator, start);

// istanbul ignore next 7
/* c8 ignore next 7 */
if (index === -1) {
throw new Error(
`Cannot find operator ${operator} from index ${start} in ${JSON.stringify(
`Cannot find operator '${operator}' from index ${start} in ${JSON.stringify(
this.text,
)}`,
);
Expand All @@ -449,6 +464,8 @@ class Transformer extends Source {
start = index;
}

const expression = this.#transform<babel.Expression>(node.expression);

return this.#create<babel.UnaryExpression>(
{
type: 'UnaryExpression',
Expand Down Expand Up @@ -539,6 +556,15 @@ class Transformer extends Source {
);
}

if (node instanceof angular.TaggedTemplateLiteral) {
return this.#create<babel.TaggedTemplateExpression>({
type: 'TaggedTemplateExpression',
tag: this.#transform(node.tag) as babel.Expression,
quasi: this.#transform(node.template) as babel.TemplateLiteral,
...node.sourceSpan,
});
}

if (node instanceof angular.TemplateLiteral) {
const { elements, expressions } = node;

Expand Down Expand Up @@ -579,7 +605,11 @@ class Transformer extends Source {
);
}

// istanbul ignore next
if (node instanceof angular.ParenthesizedExpression) {
return this.#transformNode(node.expression);
}

/* c8 ignore next */
throw new Error(`Unexpected node type '${node.constructor.name}'`);
}
}
Expand Down Expand Up @@ -608,7 +638,10 @@ type SupportedNodes =
| angular.EmptyExpr
| angular.PrefixNot
| angular.TypeofExpression
| angular.TemplateLiteral; // Including `TemplateLiteralElement`
| angular.VoidExpression
| angular.TemplateLiteral // Including `TemplateLiteralElement`
| angular.TaggedTemplateLiteral
| angular.ParenthesizedExpression;
function transform(node: SupportedNodes, text: string): NGNode {
return new Transformer(node, text).node;
}
Expand Down
3 changes: 2 additions & 1 deletion src/transform-template-binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,14 @@ class Transformer extends NodeTransformer {
});

const lastNode = body.pop()!;
// istanbul ignore else

if (lastNode.type === 'NGMicrosyntaxExpression') {
body.push(updateExpressionAlias(lastNode));
} else if (lastNode.type === 'NGMicrosyntaxKeyedExpression') {
const expression = updateExpressionAlias(lastNode.expression);
body.push(updateSpanEnd({ ...lastNode, expression }, expression.end));
} else {
/* c8 ignore next 2 */
throw new Error(`Unexpected type ${lastNode.type}`);
}
} else {
Expand Down
1 change: 1 addition & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export function getCharacterLastIndex(
}
}

/* c8 ignore next 4 */
throw new Error(
`Cannot find front char ${pattern} from index ${fromIndex} in ${JSON.stringify(
text,
Expand Down
8 changes: 8 additions & 0 deletions tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,17 @@ const KNOWN_AST_TYPES = [
'SafePropertyRead',
'ThisReceiver',
'Interpolation',
'VoidExpression',
'TemplateLiteral',
'TaggedTemplateLiteral',
'ParenthesizedExpression',
] as const;

export function getAngularNodeType(node: angular.AST) {
if (node instanceof angular.ParenthesizedExpression) {
return getAngularNodeType(node.expression);
}

return (
KNOWN_AST_TYPES.find((type) => node instanceof angular[type]) ??
node.constructor.name
Expand Down
Loading