Skip to content

Commit 0d517ec

Browse files
committed
feat: support typeof expression
1 parent 56304fb commit 0d517ec

File tree

3 files changed

+92
-68
lines changed

3 files changed

+92
-68
lines changed

src/transform-node.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,15 +416,34 @@ class Transformer extends Source {
416416
);
417417
}
418418

419-
if (node instanceof angular.PrefixNot) {
419+
const isPrefixNot = node instanceof angular.PrefixNot;
420+
if (isPrefixNot || node instanceof angular.TypeofExpression) {
420421
const expression = this.#transform<babel.Expression>(node.expression);
422+
423+
const operator = isPrefixNot ? '!' : 'typeof';
424+
let { start, end } = node.sourceSpan;
425+
426+
if (!isPrefixNot) {
427+
const index = this.text.lastIndexOf(operator, start);
428+
429+
if (index === -1) {
430+
throw new Error(
431+
`Cannot find operator ${operator} from index ${start} in ${JSON.stringify(
432+
this.text,
433+
)}`,
434+
);
435+
}
436+
437+
start = index;
438+
}
439+
421440
return this.#create<babel.UnaryExpression>(
422441
{
423442
type: 'UnaryExpression',
424443
prefix: true,
425-
operator: '!',
444+
operator,
426445
argument: expression,
427-
start: node.sourceSpan.start, // !
446+
start,
428447
end: getOuterEnd(expression),
429448
},
430449
{ hasParentParens: isInParentParens },
@@ -535,6 +554,7 @@ class Transformer extends Source {
535554
// SafeCall
536555
// EmptyExpr
537556
// PrefixNot
557+
// TypeofExpression
538558
function transform(node: angular.AST, text: string): NGNode {
539559
return new Transformer(node, text).node;
540560
}

tests/helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ const KNOWN_AST_TYPES = [
156156
'ImplicitReceiver',
157157
'KeyedRead',
158158
'SafeKeyedRead',
159+
'TypeofExpression',
159160
'KeyedWrite',
160161
'LiteralArray',
161162
'LiteralMap',

0 commit comments

Comments
 (0)