Skip to content

Commit

Permalink
feat: support typeof expression
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Nov 25, 2024
1 parent 56304fb commit a53eaef
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 68 deletions.
26 changes: 23 additions & 3 deletions src/transform-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,34 @@ class Transformer extends Source {
);
}

if (node instanceof angular.PrefixNot) {
const isPrefixNot = node instanceof angular.PrefixNot;
if (isPrefixNot || node instanceof angular.TypeofExpression) {
const expression = this.#transform<babel.Expression>(node.expression);

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

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

if (index === -1) {
throw new Error(
`Cannot find operator ${operator} from index ${start} in ${JSON.stringify(
this.text,
)}`,
);
}

start = index;
}

return this.#create<babel.UnaryExpression>(
{
type: 'UnaryExpression',
prefix: true,
operator: '!',
operator,
argument: expression,
start: node.sourceSpan.start, // !
start,
end: getOuterEnd(expression),
},
{ hasParentParens: isInParentParens },
Expand Down Expand Up @@ -535,6 +554,7 @@ class Transformer extends Source {
// SafeCall
// EmptyExpr
// PrefixNot
// TypeofExpression
function transform(node: angular.AST, text: string): NGNode {
return new Transformer(node, text).node;
}
Expand Down
1 change: 1 addition & 0 deletions tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ const KNOWN_AST_TYPES = [
'ImplicitReceiver',
'KeyedRead',
'SafeKeyedRead',
'TypeofExpression',
'KeyedWrite',
'LiteralArray',
'LiteralMap',
Expand Down
Loading

0 comments on commit a53eaef

Please sign in to comment.