angular-estree-parser
Advanced tools
Comparing version
@@ -10,3 +10,4 @@ import * as angular from '@angular/compiler'; | ||
} | ||
declare function transform(node: angular.AST, text: string): NGNode; | ||
type SupportedNodes = angular.ASTWithSource | angular.PropertyRead | angular.PropertyWrite | angular.KeyedWrite | angular.Call | angular.LiteralPrimitive | angular.Unary | angular.Binary | angular.ThisReceiver | angular.KeyedRead | angular.Chain | angular.LiteralMap | angular.LiteralArray | angular.Conditional | angular.NonNullAssert | angular.BindingPipe | angular.SafeKeyedRead | angular.SafePropertyRead | angular.SafeCall | angular.EmptyExpr | angular.PrefixNot | angular.TypeofExpression | angular.TemplateLiteral; | ||
declare function transform(node: SupportedNodes, text: string): NGNode; | ||
export { transform, Transformer }; |
@@ -61,6 +61,10 @@ import * as angular from '@angular/compiler'; | ||
} | ||
#transform(node, isInParentParens = false) { | ||
return this.#transformNode(node, isInParentParens); | ||
#transform(node, options) { | ||
return this.#transformNode(node, options); | ||
} | ||
#transformNode(node, isInParentParens = false) { | ||
#transformNode(node, options) { | ||
const { isInParentParens } = { | ||
isInParentParens: false, | ||
...options, | ||
}; | ||
if (node instanceof angular.Interpolation) { | ||
@@ -234,3 +238,7 @@ const { expressions } = node; | ||
const tArgs = args.length === 1 | ||
? [this.#transform(args[0], true)] | ||
? [ | ||
this.#transform(args[0], { | ||
isInParentParens: true, | ||
}), | ||
] | ||
: args.map((node) => this.#transform(node)); | ||
@@ -248,3 +256,3 @@ const tReceiver = this.#transform(receiver); | ||
start: getOuterStart(tReceiver), | ||
end: node.sourceSpan.end, // ) | ||
end: node.sourceSpan.end, // `)` | ||
}, { hasParentParens: isInParentParens }); | ||
@@ -258,3 +266,3 @@ } | ||
start: getOuterStart(expression), | ||
end: node.sourceSpan.end, // ! | ||
end: node.sourceSpan.end, // `!` | ||
}, { hasParentParens: isInParentParens }); | ||
@@ -342,29 +350,46 @@ } | ||
} | ||
if (node instanceof angular.TemplateLiteral) { | ||
const { elements, expressions } = node; | ||
return this.#create({ | ||
type: 'TemplateLiteral', | ||
quasis: elements.map((element) => this.#transform(element, { parent: node })), | ||
expressions: expressions.map((expression) => this.#transform(expression)), | ||
...node.sourceSpan, | ||
}); | ||
} | ||
if (node instanceof angular.TemplateLiteralElement) { | ||
const templateLiteral = options.parent; | ||
const elementIndex = templateLiteral.elements.indexOf(node); | ||
const isFirst = elementIndex === 0; | ||
const isLast = elementIndex === templateLiteral.elements.length - 1; | ||
// The `TemplateLiteralElement` don't have correct location information | ||
const start = isFirst | ||
? templateLiteral.sourceSpan.start + 1 | ||
: node.sourceSpan.start; | ||
let end; | ||
if (isLast) { | ||
end = templateLiteral.sourceSpan.end - 1; | ||
} | ||
else { | ||
const nextExpression = templateLiteral.expressions[elementIndex]; | ||
// TODO: Support search multiple characters in `getCharacterLastIndex()` | ||
// FIXME: Search `${` instead | ||
end = this.getCharacterLastIndex('$', nextExpression.sourceSpan.start); | ||
} | ||
const raw = this.text.slice(start, end); | ||
return this.#create({ | ||
type: 'TemplateElement', | ||
value: { | ||
cooked: node.text, | ||
raw, | ||
}, | ||
start: start, | ||
end: end, | ||
tail: isLast, | ||
}, { stripSpaces: false }); | ||
} | ||
// istanbul ignore next | ||
throw Object.assign(new Error('Unexpected node'), { node }); | ||
throw new Error(`Unexpected node type '${node.constructor.name}'`); | ||
} | ||
} | ||
// See `convertAst` in `@angular/compiler` | ||
// ASTWithSource (Not handled) | ||
// PropertyRead | ||
// PropertyWrite | ||
// KeyedWrite | ||
// Call | ||
// LiteralPrimitive | ||
// Unary | ||
// Binary | ||
// ThisReceiver (Not handled) | ||
// KeyedRead | ||
// Chain | ||
// LiteralMap | ||
// LiteralArray | ||
// Conditional | ||
// NonNullAssert | ||
// BindingPipe | ||
// SafeKeyedRead | ||
// SafePropertyRead | ||
// SafeCall | ||
// EmptyExpr | ||
// PrefixNot | ||
// TypeofExpression | ||
function transform(node, text) { | ||
@@ -371,0 +396,0 @@ return new Transformer(node, text).node; |
{ | ||
"name": "angular-estree-parser", | ||
"version": "11.0.0", | ||
"version": "11.1.0", | ||
"description": "A parser that converts Angular source code into an ESTree-compatible form", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
46255
4.77%1001
2.77%