From f8f3a688705b59c3e3376c7d5a5de124b35b59a7 Mon Sep 17 00:00:00 2001 From: fisker Date: Tue, 14 Nov 2023 22:10:24 +0800 Subject: [PATCH 1/4] Add tests --- tests/transform-microsyntax.test.ts | 37 ++++++++++++++++------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/tests/transform-microsyntax.test.ts b/tests/transform-microsyntax.test.ts index 4e3ee209..6964acca 100644 --- a/tests/transform-microsyntax.test.ts +++ b/tests/transform-microsyntax.test.ts @@ -4,23 +4,26 @@ import type { NGMicrosyntaxKeyedExpression } from '../src/types.js'; import { snapshotAst } from './helpers.js'; test.each` - input | types - ${''} | ${[]} - ${' let hero '} | ${['NGMicrosyntaxLet']} - ${' let hero = hello '} | ${['NGMicrosyntaxLet']} - ${' let hero of heroes '} | ${['NGMicrosyntaxLet', 'NGMicrosyntaxKeyedExpression']} - ${' let hero ; of : heroes '} | ${['NGMicrosyntaxLet', 'NGMicrosyntaxKeyedExpression']} - ${' as b '} | ${['NGMicrosyntaxAs']} - ${' a '} | ${['NGMicrosyntaxExpression']} - ${' a as b '} | ${['NGMicrosyntaxExpression']} - ${' a , b '} | ${['NGMicrosyntaxExpression', 'NGMicrosyntaxKey']} - ${' a ; b '} | ${['NGMicrosyntaxExpression', 'NGMicrosyntaxKey']} - ${' a ; b c '} | ${['NGMicrosyntaxExpression', 'NGMicrosyntaxKeyedExpression']} - ${' a ; b : c '} | ${['NGMicrosyntaxExpression', 'NGMicrosyntaxKeyedExpression']} - ${' a ; b : c as d '} | ${['NGMicrosyntaxExpression', 'NGMicrosyntaxKeyedExpression']} - ${' a ; b as c '} | ${['NGMicrosyntaxExpression', 'NGMicrosyntaxAs']} - ${' let "a" = "b" ; "c" as "d" '} | ${['NGMicrosyntaxLet', 'NGMicrosyntaxAs']} - ${' let "\\"" '} | ${['NGMicrosyntaxLet']} + input | types + ${''} | ${[]} + ${' let hero '} | ${['NGMicrosyntaxLet']} + ${' let hero = hello '} | ${['NGMicrosyntaxLet']} + ${' let hero of heroes '} | ${['NGMicrosyntaxLet', 'NGMicrosyntaxKeyedExpression']} + ${' let hero ; of : heroes '} | ${['NGMicrosyntaxLet', 'NGMicrosyntaxKeyedExpression']} + ${' as b '} | ${['NGMicrosyntaxAs']} + ${' a '} | ${['NGMicrosyntaxExpression']} + ${' a as b '} | ${['NGMicrosyntaxExpression']} + ${' a , b '} | ${['NGMicrosyntaxExpression', 'NGMicrosyntaxKey']} + ${' a ; b '} | ${['NGMicrosyntaxExpression', 'NGMicrosyntaxKey']} + ${' a ; b c '} | ${['NGMicrosyntaxExpression', 'NGMicrosyntaxKeyedExpression']} + ${' a ; b : c '} | ${['NGMicrosyntaxExpression', 'NGMicrosyntaxKeyedExpression']} + ${' a ; b : c as d '} | ${['NGMicrosyntaxExpression', 'NGMicrosyntaxKeyedExpression']} + ${' a ; b as c '} | ${['NGMicrosyntaxExpression', 'NGMicrosyntaxAs']} + ${' let "a" = "b" ; "c" as "d" '} | ${['NGMicrosyntaxLet', 'NGMicrosyntaxAs']} + ${' let "\\"" '} | ${['NGMicrosyntaxLet']} + ${' let item of items; index as i; trackBy: trackByFn'} | ${[]} + ${' item of items; track item '} | ${[]} + ${' item of items;index as i; trackBy: trackByFn'} | ${[]} `('$input', ({ input, types }) => { const ast = parseTemplateBindings(input); expect(snapshotAst(ast, input)).toMatchSnapshot(); From 43949ebfb6726e09017097a9858415742cf12195 Mon Sep 17 00:00:00 2001 From: fisker Date: Wed, 15 Nov 2023 01:10:30 +0800 Subject: [PATCH 2/4] Update --- src/parser.ts | 2 +- src/transform-microsyntax.ts | 304 +-- src/transform-node.ts | 6 +- src/types.ts | 46 +- src/utils.ts | 1 + .../transform-microsyntax.test.ts.snap | 1657 +++++++++++++++-- tests/transform-microsyntax.test.ts | 55 +- 7 files changed, 1696 insertions(+), 375 deletions(-) diff --git a/src/parser.ts b/src/parser.ts index 7ae64394..2c5d345d 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -66,7 +66,7 @@ function parseTemplateBindings(text: string) { const parser = createParser(); const { templateBindings: expressions, errors } = parser.parseTemplateBindings( - NG_PARSE_TEMPLATE_BINDINGS_FAKE_PREFIX, + '', text, NG_PARSE_FAKE_LOCATION, NG_PARSE_FAKE_ABSOLUTE_OFFSET, diff --git a/src/transform-microsyntax.ts b/src/transform-microsyntax.ts index 57db9708..4d76733b 100644 --- a/src/transform-microsyntax.ts +++ b/src/transform-microsyntax.ts @@ -1,4 +1,5 @@ -import type * as ng from '@angular/compiler'; +import * as ng from '@angular/compiler'; +import type * as b from '@babel/types'; import { ExpressionBinding as NGExpressionBinding, VariableBinding as NGVariableBinding, @@ -7,14 +8,11 @@ import { type Context } from './context.js'; import transformNode from './transform-node.js'; import type { NGMicrosyntax, - NGMicrosyntaxAs, - NGMicrosyntaxExpression, - NGMicrosyntaxKey, + NGMicrosyntaxAsExpression, NGMicrosyntaxKeyedExpression, - NGMicrosyntaxLet, - NGMicrosyntaxNode, NGNode, RawNGSpan, + NGMicrosyntaxOfExpression, } from './types.js'; import { NG_PARSE_TEMPLATE_BINDINGS_FAKE_PREFIX } from './parser.js'; import { toLowerCamelCase, transformSpan, createNode } from './utils.js'; @@ -92,8 +90,91 @@ function transformTemplateBindings({ }): NGMicrosyntax { rawTemplateBindings.forEach(fixTemplateBindingSpan); + function transformVariableBinding(variableBinding: ng.VariableBinding) { + const { key, sourceSpan } = variableBinding; + const startsWithLet = /^let\s$/.test( + context.text.slice(sourceSpan.start, sourceSpan.start + 4), + ); + if (startsWithLet) { + const { value } = variableBinding; + const id = _c({ + type: 'Identifier', + name: key.source, + ...key.span, + }); + const init = value + ? _c({ + type: 'Identifier', + name: value.source, + ...value.span, + }) + : null; + return _c({ + type: 'VariableDeclaration', + kind: 'let', + declarations: [ + _c({ + type: 'VariableDeclarator', + id, + init, + ...sourceSpan, + }), + ], + ...sourceSpan, + }); + } else { + const value = getAsVariableBindingValue(variableBinding, context); + return _c({ + type: 'NGMicrosyntaxAsExpression', + expression: _c({ + type: 'Identifier', + name: value!.source, + ...value!.span, + }), + alias: _c({ + type: 'Identifier', + name: key.source, + ...key.span, + }), + start: value!.span.start, + end: key.span.end, + }); + } + } + + function transformExpressionBinding(expressionBinding: ng.ExpressionBinding) { + const { key, value } = expressionBinding; + + if (!value) { + return transformTemplateBindingIdentifier(key); + } + + const hasKey = key.source !== ''; + + if (hasKey) { + return _c({ + type: 'NGMicrosyntaxKeyedExpression', + key: transformTemplateBindingIdentifier(key), + expression: _t(value), + ...expressionBinding.sourceSpan, + }); + } + + return _t(value); + } + + function transformTemplateBinding(templateBinding: ng.TemplateBinding) { + if (isExpressionBinding(templateBinding)) { + return transformExpressionBinding(templateBinding); + } + if (isVariableBinding(templateBinding)) { + return transformVariableBinding(templateBinding); + } + + throw new Error('Unexpected node'); + } + const [firstTemplateBinding] = rawTemplateBindings; - const { key: prefix } = firstTemplateBinding; const templateBindings = context.text .slice( @@ -106,138 +187,97 @@ function transformTemplateBindings({ const body: NGMicrosyntax['body'] = []; - let lastTemplateBinding: ng.TemplateBinding | null = null; + function isOfExpression(node: ng.TemplateBinding) { + return ( + isExpressionBinding(node) && + node.key.source === 'Of' && + node.value instanceof ng.ASTWithSource + ); + } + + function transformOfExpression( + first: ng.TemplateBinding, + second: ng.ExpressionBinding, + ) { + const left = transformTemplateBinding( + first, + ) as NGMicrosyntaxOfExpression['left']; + const right = _t(second.value!) as NGMicrosyntaxOfExpression['right']; + + return _c({ + type: 'NGMicrosyntaxOfExpression', + left, + right, + start: left.start, + end: left.end, + }); + } + + function isAsExpression( + first: ng.TemplateBinding, + second: ng.TemplateBinding, + ) { + return ( + isExpressionBinding(first) && + first.key.source === '' && + first.value instanceof ng.ASTWithSource && + isVariableBinding(second) && + context.text.slice(first.value.span.end, second.key.span.start).trim() == + 'as' + ); + } + + function transformAsExpression( + first: ng.ExpressionBinding, + second: ng.VariableBinding, + ) { + return _c({ + type: 'NGMicrosyntaxAsExpression', + expression: _t(first.value as ng.ASTWithSource), + alias: _c({ + type: 'Identifier', + name: second.key.source, + ...second.key.span, + }), + start: first.sourceSpan.start, + end: second.sourceSpan.end, + }); + } + for (let i = 0; i < templateBindings.length; i++) { const templateBinding = templateBindings[i]; + const nextTemplateBinding = templateBindings[i + 1]; + if (isOfExpression(nextTemplateBinding)) { + const ofExpression = transformOfExpression( + templateBinding, + nextTemplateBinding as ng.ExpressionBinding, + ); - if ( - lastTemplateBinding && - isExpressionBinding(lastTemplateBinding) && - isVariableBinding(templateBinding) && - templateBinding.value && - templateBinding.value.source === lastTemplateBinding.key.source - ) { - const alias = _c({ - type: 'NGMicrosyntaxKey', - name: templateBinding.key.source, - ...templateBinding.key.span, - }); - const updateSpanEnd = (node: T, end: number): T => ({ - ...node, - ...transformSpan({ start: node.start!, end }, context.text), - }); - const updateExpressionAlias = (expression: NGMicrosyntaxExpression) => ({ - ...updateSpanEnd(expression, alias.end), - alias, - }); + body.push(ofExpression); + i++; + continue; + } - 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 { - throw new Error(`Unexpected type ${lastNode.type}`); - } - } else { - body.push(transformTemplateBinding(templateBinding, i)); + if (isAsExpression(templateBinding, nextTemplateBinding)) { + const asExpression = transformAsExpression( + templateBinding as ng.ExpressionBinding, + nextTemplateBinding as ng.VariableBinding, + ); + body.push(asExpression); + i++; + continue; } - lastTemplateBinding = templateBinding; + body.push(transformTemplateBinding(templateBinding)); } return _c({ type: 'NGMicrosyntax', body, - ...(body.length === 0 - ? rawTemplateBindings[0].sourceSpan - : { start: body[0].start, end: body.at(-1)!.end }), + start: 0, + end: context.text.length, }); - function transformTemplateBinding( - templateBinding: ng.TemplateBinding, - index: number, - ): Exclude { - if (isExpressionBinding(templateBinding)) { - const { key, value } = templateBinding; - if (!value) { - return _c({ - type: 'NGMicrosyntaxKey', - name: removePrefix(key.source), - ...key.span, - }); - } else if (index === 0) { - return _c({ - type: 'NGMicrosyntaxExpression', - expression: _t(value.ast), - alias: null, - ...value.sourceSpan, - }); - } else { - return _c({ - type: 'NGMicrosyntaxKeyedExpression', - key: _c({ - type: 'NGMicrosyntaxKey', - name: removePrefix(key.source), - ...key.span, - }), - expression: _c({ - type: 'NGMicrosyntaxExpression', - expression: _t(value.ast), - alias: null, - ...value.sourceSpan, - }), - start: key.span.start, - end: value.sourceSpan.end, - }); - } - } else { - const { key, sourceSpan } = templateBinding; - const startsWithLet = /^let\s$/.test( - context.text.slice(sourceSpan.start, sourceSpan.start + 4), - ); - if (startsWithLet) { - const { value } = templateBinding; - return _c({ - type: 'NGMicrosyntaxLet', - key: _c({ - type: 'NGMicrosyntaxKey', - name: key.source, - ...key.span, - }), - value: !value - ? null - : _c({ - type: 'NGMicrosyntaxKey', - name: value.source, - ...value.span, - }), - start: sourceSpan.start, - end: value ? value.span.end : key.span.end, - }); - } else { - const value = getAsVariableBindingValue(templateBinding, context); - return _c({ - type: 'NGMicrosyntaxAs', - key: _c({ - type: 'NGMicrosyntaxKey', - name: value!.source, - ...value!.span, - }), - alias: _c({ - type: 'NGMicrosyntaxKey', - name: key.source, - ...key.span, - }), - start: value!.span.start, - end: key.span.end, - }); - } - } - } - function _t(node: ng.AST) { return transformNode(node, context) as T; } @@ -250,7 +290,7 @@ function transformTemplateBindings({ } function removePrefix(string: string) { - return toLowerCamelCase(string.slice(prefix.source.length)); + return toLowerCamelCase(string); } function fixTemplateBindingSpan(templateBinding: ng.TemplateBinding) { @@ -259,6 +299,16 @@ function transformTemplateBindings({ fixSpan(templateBinding.value.span, context.text); } } + + function transformTemplateBindingIdentifier( + node: ng.TemplateBindingIdentifier, + ) { + return _c({ + type: 'Identifier', + name: node.source, + ...node.span, + }); + } } export { transformTemplateBindings }; diff --git a/src/transform-node.ts b/src/transform-node.ts index 882c6a9e..560defe5 100644 --- a/src/transform-node.ts +++ b/src/transform-node.ts @@ -40,6 +40,9 @@ function transform( ): NGNode { const type = getAngularNodeType(node); switch (type) { + case 'ASTWithSource': { + return _t((node as ng.ASTWithSource).ast); + } case 'Unary': { const { operator, expr } = node as ng.Unary; const argumentNode = _t(expr); @@ -405,7 +408,8 @@ function transform( } // istanbul ignore next default: - throw new Error(`Unexpected node ${type}`); + console.log(node); + throw new Error(`Unexpected node`); } function _t(n: ng.AST) { diff --git a/src/types.ts b/src/types.ts index 569ed2af..4cea7ead 100644 --- a/src/types.ts +++ b/src/types.ts @@ -80,48 +80,34 @@ export interface RawNGSpan { */ export interface NGMicrosyntax extends NGBaseNode { type: 'NGMicrosyntax'; - body: ( - | NGMicrosyntaxAs - | NGMicrosyntaxExpression - | NGMicrosyntaxKey - | NGMicrosyntaxKeyedExpression - | NGMicrosyntaxLet - )[]; + body: NGMicrosyntaxExpression[]; } export type NGMicrosyntaxNode = | NGMicrosyntax - | NGMicrosyntaxAs - | NGMicrosyntaxExpression - | NGMicrosyntaxKey + | NGMicrosyntaxOfExpression + | NGMicrosyntaxAsExpression + | NGMicrosyntaxKeyedExpression; + +export type NGMicrosyntaxExpression = + | NGMicrosyntaxOfExpression + | NGMicrosyntaxAsExpression | NGMicrosyntaxKeyedExpression - | NGMicrosyntaxLet; + | NGNode; -export interface NGMicrosyntaxKey extends NGBaseNode { - type: 'NGMicrosyntaxKey'; - name: string; +export interface NGMicrosyntaxOfExpression extends NGBaseNode { + left: b.ForOfStatement['left'] & LocationInformation; + right: b.ForOfStatement['right'] & LocationInformation; } export interface NGMicrosyntaxKeyedExpression extends NGBaseNode { type: 'NGMicrosyntaxKeyedExpression'; - key: NGMicrosyntaxKey; + key: b.Identifier; expression: NGMicrosyntaxExpression; } -export interface NGMicrosyntaxExpression extends NGBaseNode { - type: 'NGMicrosyntaxExpression'; +export interface NGMicrosyntaxAsExpression extends NGBaseNode { + type: 'NGMicrosyntaxAsExpression'; expression: NGNode; - alias: NGMicrosyntaxKey | null; -} - -export interface NGMicrosyntaxAs extends NGBaseNode { - type: 'NGMicrosyntaxAs'; - key: NGMicrosyntaxKey; - alias: NGMicrosyntaxKey; -} - -export interface NGMicrosyntaxLet extends NGBaseNode { - type: 'NGMicrosyntaxLet'; - key: NGMicrosyntaxKey; - value: NGMicrosyntaxKey | null; + alias: b.Identifier; } diff --git a/src/utils.ts b/src/utils.ts index 8d0f455b..d121374a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -30,6 +30,7 @@ export function getAngularNodeType(node: (ng.AST | RawNGComment) & { type?: stri if (node instanceof ng.PropertyWrite) { return "PropertyWrite"; } if (node instanceof ng.SafeCall) { return "SafeCall"; } if (node instanceof ng.SafePropertyRead) { return "SafePropertyRead"; } + if (node instanceof ng.ASTWithSource) { return "ASTWithSource"; } // istanbul ignore next return node.type; } diff --git a/tests/__snapshots__/transform-microsyntax.test.ts.snap b/tests/__snapshots__/transform-microsyntax.test.ts.snap index 108a81dd..3e930c7f 100644 --- a/tests/__snapshots__/transform-microsyntax.test.ts.snap +++ b/tests/__snapshots__/transform-microsyntax.test.ts.snap @@ -8,22 +8,28 @@ NGMicrosyntax { | ^ `; +exports[` 2`] = ` +{ + "body": [], + "end": 0, + "range": [ + 0, + 0, + ], + "start": 0, + "type": "NGMicrosyntax", +} +`; + exports[` a 1`] = ` NGMicrosyntax { "body": [ - "NGMicrosyntaxExpression" + "Identifier" ] } > 1 | a | ^ -------------------------------------------------------------------------------- -NGMicrosyntaxExpression { - "expression": "Identifier", - "alias": null -} -> 1 | a - | ^ --------------------------------------------------------------------------------- Identifier { "name": "a" } @@ -31,83 +37,154 @@ Identifier { | ^ `; +exports[` a 2`] = ` +{ + "body": [ + { + "end": 2, + "name": "a", + "range": [ + 1, + 2, + ], + "start": 1, + "type": "Identifier", + }, + ], + "end": 2, + "range": [ + 1, + 2, + ], + "start": 1, + "type": "NGMicrosyntax", +} +`; + exports[` a , b 1`] = ` NGMicrosyntax { "body": [ - "NGMicrosyntaxExpression", - "NGMicrosyntaxKey" + "Identifier", + "Identifier" ] } > 1 | a , b | ^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxExpression { - "expression": "Identifier", - "alias": null -} -> 1 | a , b - | ^ --------------------------------------------------------------------------------- Identifier { "name": "a" } > 1 | a , b | ^ -------------------------------------------------------------------------------- -NGMicrosyntaxKey { - "name": "b" +Identifier { + "name": "B" } > 1 | a , b | ^ `; +exports[` a , b 2`] = ` +{ + "body": [ + { + "end": 2, + "name": "a", + "range": [ + 1, + 2, + ], + "start": 1, + "type": "Identifier", + }, + { + "end": 6, + "name": "B", + "range": [ + 5, + 6, + ], + "start": 5, + "type": "Identifier", + }, + ], + "end": 6, + "range": [ + 1, + 6, + ], + "start": 1, + "type": "NGMicrosyntax", +} +`; + exports[` a ; b 1`] = ` NGMicrosyntax { "body": [ - "NGMicrosyntaxExpression", - "NGMicrosyntaxKey" + "Identifier", + "Identifier" ] } > 1 | a ; b | ^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxExpression { - "expression": "Identifier", - "alias": null -} -> 1 | a ; b - | ^ --------------------------------------------------------------------------------- Identifier { "name": "a" } > 1 | a ; b | ^ -------------------------------------------------------------------------------- -NGMicrosyntaxKey { - "name": "b" +Identifier { + "name": "B" } > 1 | a ; b | ^ `; +exports[` a ; b 2`] = ` +{ + "body": [ + { + "end": 2, + "name": "a", + "range": [ + 1, + 2, + ], + "start": 1, + "type": "Identifier", + }, + { + "end": 6, + "name": "B", + "range": [ + 5, + 6, + ], + "start": 5, + "type": "Identifier", + }, + ], + "end": 6, + "range": [ + 1, + 6, + ], + "start": 1, + "type": "NGMicrosyntax", +} +`; + exports[` a ; b : c 1`] = ` NGMicrosyntax { "body": [ - "NGMicrosyntaxExpression", + "Identifier", "NGMicrosyntaxKeyedExpression" ] } > 1 | a ; b : c | ^^^^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxExpression { - "expression": "Identifier", - "alias": null -} -> 1 | a ; b : c - | ^ --------------------------------------------------------------------------------- Identifier { "name": "a" } @@ -115,25 +192,18 @@ Identifier { | ^ -------------------------------------------------------------------------------- NGMicrosyntaxKeyedExpression { - "key": "NGMicrosyntaxKey", - "expression": "NGMicrosyntaxExpression" + "key": "Identifier", + "expression": "Identifier" } > 1 | a ; b : c | ^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxKey { - "name": "b" +Identifier { + "name": "B" } > 1 | a ; b : c | ^ -------------------------------------------------------------------------------- -NGMicrosyntaxExpression { - "expression": "Identifier", - "alias": null -} -> 1 | a ; b : c - | ^ --------------------------------------------------------------------------------- Identifier { "name": "c" } @@ -141,23 +211,70 @@ Identifier { | ^ `; +exports[` a ; b : c 2`] = ` +{ + "body": [ + { + "end": 2, + "name": "a", + "range": [ + 1, + 2, + ], + "start": 1, + "type": "Identifier", + }, + { + "end": 10, + "expression": { + "end": 10, + "name": "c", + "range": [ + 9, + 10, + ], + "start": 9, + "type": "Identifier", + }, + "key": { + "end": 6, + "name": "B", + "range": [ + 5, + 6, + ], + "start": 5, + "type": "Identifier", + }, + "range": [ + 5, + 10, + ], + "start": 5, + "type": "NGMicrosyntaxKeyedExpression", + }, + ], + "end": 10, + "range": [ + 1, + 10, + ], + "start": 1, + "type": "NGMicrosyntax", +} +`; + exports[` a ; b : c as d 1`] = ` NGMicrosyntax { "body": [ - "NGMicrosyntaxExpression", - "NGMicrosyntaxKeyedExpression" + "Identifier", + "NGMicrosyntaxKeyedExpression", + "NGMicrosyntaxAsExpression" ] } > 1 | a ; b : c as d | ^^^^^^^^^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxExpression { - "expression": "Identifier", - "alias": null -} -> 1 | a ; b : c as d - | ^ --------------------------------------------------------------------------------- Identifier { "name": "a" } @@ -165,98 +282,225 @@ Identifier { | ^ -------------------------------------------------------------------------------- NGMicrosyntaxKeyedExpression { - "key": "NGMicrosyntaxKey", - "expression": "NGMicrosyntaxExpression" + "key": "Identifier", + "expression": "Identifier" } > 1 | a ; b : c as d - | ^^^^^^^^^^ + | ^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxKey { - "name": "b" +Identifier { + "name": "B" } > 1 | a ; b : c as d | ^ -------------------------------------------------------------------------------- -NGMicrosyntaxExpression { +Identifier { + "name": "c" +} +> 1 | a ; b : c as d + | ^ +-------------------------------------------------------------------------------- +NGMicrosyntaxAsExpression { "expression": "Identifier", - "alias": "NGMicrosyntaxKey" + "alias": "Identifier" } > 1 | a ; b : c as d - | ^^^^^^ + | ^^^^^^^^^^ -------------------------------------------------------------------------------- Identifier { - "name": "c" + "name": "B" } > 1 | a ; b : c as d - | ^ + | ^ -------------------------------------------------------------------------------- -NGMicrosyntaxKey { +Identifier { "name": "d" } > 1 | a ; b : c as d | ^ `; +exports[` a ; b : c as d 2`] = ` +{ + "body": [ + { + "end": 2, + "name": "a", + "range": [ + 1, + 2, + ], + "start": 1, + "type": "Identifier", + }, + { + "end": 10, + "expression": { + "end": 10, + "name": "c", + "range": [ + 9, + 10, + ], + "start": 9, + "type": "Identifier", + }, + "key": { + "end": 6, + "name": "B", + "range": [ + 5, + 6, + ], + "start": 5, + "type": "Identifier", + }, + "range": [ + 5, + 10, + ], + "start": 5, + "type": "NGMicrosyntaxKeyedExpression", + }, + { + "alias": { + "end": 15, + "name": "d", + "range": [ + 14, + 15, + ], + "start": 14, + "type": "Identifier", + }, + "end": 15, + "expression": { + "end": 6, + "name": "B", + "range": [ + 5, + 6, + ], + "start": 5, + "type": "Identifier", + }, + "range": [ + 5, + 15, + ], + "start": 5, + "type": "NGMicrosyntaxAsExpression", + }, + ], + "end": 15, + "range": [ + 1, + 15, + ], + "start": 1, + "type": "NGMicrosyntax", +} +`; + exports[` a ; b as c 1`] = ` NGMicrosyntax { "body": [ - "NGMicrosyntaxExpression", - "NGMicrosyntaxAs" + "Identifier", + "NGMicrosyntaxAsExpression" ] } > 1 | a ; b as c | ^^^^^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxExpression { - "expression": "Identifier", - "alias": null -} -> 1 | a ; b as c - | ^ --------------------------------------------------------------------------------- Identifier { "name": "a" } > 1 | a ; b as c | ^ -------------------------------------------------------------------------------- -NGMicrosyntaxAs { - "key": "NGMicrosyntaxKey", - "alias": "NGMicrosyntaxKey" +NGMicrosyntaxAsExpression { + "expression": "Identifier", + "alias": "Identifier" } > 1 | a ; b as c | ^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxKey { +Identifier { "name": "b" } > 1 | a ; b as c | ^ -------------------------------------------------------------------------------- -NGMicrosyntaxKey { +Identifier { "name": "c" } > 1 | a ; b as c | ^ `; +exports[` a ; b as c 2`] = ` +{ + "body": [ + { + "end": 2, + "name": "a", + "range": [ + 1, + 2, + ], + "start": 1, + "type": "Identifier", + }, + { + "alias": { + "end": 11, + "name": "c", + "range": [ + 10, + 11, + ], + "start": 10, + "type": "Identifier", + }, + "end": 11, + "expression": { + "end": 6, + "name": "b", + "range": [ + 5, + 6, + ], + "start": 5, + "type": "Identifier", + }, + "range": [ + 5, + 11, + ], + "start": 5, + "type": "NGMicrosyntaxAsExpression", + }, + ], + "end": 11, + "range": [ + 1, + 11, + ], + "start": 1, + "type": "NGMicrosyntax", +} +`; + exports[` a ; b c 1`] = ` NGMicrosyntax { "body": [ - "NGMicrosyntaxExpression", + "Identifier", "NGMicrosyntaxKeyedExpression" ] } > 1 | a ; b c | ^^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxExpression { - "expression": "Identifier", - "alias": null -} -> 1 | a ; b c - | ^ --------------------------------------------------------------------------------- Identifier { "name": "a" } @@ -264,25 +508,18 @@ Identifier { | ^ -------------------------------------------------------------------------------- NGMicrosyntaxKeyedExpression { - "key": "NGMicrosyntaxKey", - "expression": "NGMicrosyntaxExpression" + "key": "Identifier", + "expression": "Identifier" } > 1 | a ; b c | ^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxKey { - "name": "b" +Identifier { + "name": "B" } > 1 | a ; b c | ^ -------------------------------------------------------------------------------- -NGMicrosyntaxExpression { - "expression": "Identifier", - "alias": null -} -> 1 | a ; b c - | ^ --------------------------------------------------------------------------------- Identifier { "name": "c" } @@ -290,201 +527,846 @@ Identifier { | ^ `; +exports[` a ; b c 2`] = ` +{ + "body": [ + { + "end": 2, + "name": "a", + "range": [ + 1, + 2, + ], + "start": 1, + "type": "Identifier", + }, + { + "end": 8, + "expression": { + "end": 8, + "name": "c", + "range": [ + 7, + 8, + ], + "start": 7, + "type": "Identifier", + }, + "key": { + "end": 6, + "name": "B", + "range": [ + 5, + 6, + ], + "start": 5, + "type": "Identifier", + }, + "range": [ + 5, + 8, + ], + "start": 5, + "type": "NGMicrosyntaxKeyedExpression", + }, + ], + "end": 8, + "range": [ + 1, + 8, + ], + "start": 1, + "type": "NGMicrosyntax", +} +`; + exports[` a as b 1`] = ` NGMicrosyntax { "body": [ - "NGMicrosyntaxExpression" + "Identifier", + "NGMicrosyntaxAsExpression" ] } > 1 | a as b | ^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxExpression { +Identifier { + "name": "a" +} +> 1 | a as b + | ^ +-------------------------------------------------------------------------------- +NGMicrosyntaxAsExpression { "expression": "Identifier", - "alias": "NGMicrosyntaxKey" + "alias": "Identifier" } > 1 | a as b | ^^^^^^ -------------------------------------------------------------------------------- Identifier { - "name": "a" + "name": "" } > 1 | a as b - | ^ + | ^ -------------------------------------------------------------------------------- -NGMicrosyntaxKey { +Identifier { "name": "b" } > 1 | a as b | ^ `; +exports[` a as b 2`] = ` +{ + "body": [ + { + "end": 2, + "name": "a", + "range": [ + 1, + 2, + ], + "start": 1, + "type": "Identifier", + }, + { + "alias": { + "end": 7, + "name": "b", + "range": [ + 6, + 7, + ], + "start": 6, + "type": "Identifier", + }, + "end": 7, + "expression": { + "end": 0, + "name": "", + "range": [ + 0, + 0, + ], + "start": 0, + "type": "Identifier", + }, + "range": [ + 1, + 7, + ], + "start": 1, + "type": "NGMicrosyntaxAsExpression", + }, + ], + "end": 7, + "range": [ + 1, + 7, + ], + "start": 1, + "type": "NGMicrosyntax", +} +`; + exports[` as b 1`] = ` NGMicrosyntax { "body": [ - "NGMicrosyntaxAs" + "NGMicrosyntaxAsExpression" ] } > 1 | as b | ^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxAs { - "key": "NGMicrosyntaxKey", - "alias": "NGMicrosyntaxKey" +NGMicrosyntaxAsExpression { + "expression": "Identifier", + "alias": "Identifier" } > 1 | as b | ^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxKey { - "name": "$implicit" +Identifier { + "name": "" } > 1 | as b - | ^ + | ^ -------------------------------------------------------------------------------- -NGMicrosyntaxKey { +Identifier { "name": "b" } > 1 | as b | ^ `; +exports[` as b 2`] = ` +{ + "body": [ + { + "alias": { + "end": 5, + "name": "b", + "range": [ + 4, + 5, + ], + "start": 4, + "type": "Identifier", + }, + "end": 5, + "expression": { + "end": 0, + "name": "", + "range": [ + 0, + 0, + ], + "start": 0, + "type": "Identifier", + }, + "range": [ + 1, + 5, + ], + "start": 1, + "type": "NGMicrosyntaxAsExpression", + }, + ], + "end": 5, + "range": [ + 1, + 5, + ], + "start": 1, + "type": "NGMicrosyntax", +} +`; + +exports[` item of items; track item 1`] = ` +NGMicrosyntax { + "body": [ + "NGMicrosyntaxOfExpression", + "NGMicrosyntaxKeyedExpression" + ] +} +> 1 | item of items; track item + | ^^^^^^^^^^^^^^^^^^^^^^^^^ +-------------------------------------------------------------------------------- +NGMicrosyntaxOfExpression { + "left": "Identifier", + "right": "Identifier" +} +> 1 | item of items; track item + | ^^^^ +-------------------------------------------------------------------------------- +Identifier { + "name": "item" +} +> 1 | item of items; track item + | ^^^^ +-------------------------------------------------------------------------------- +Identifier { + "name": "items" +} +> 1 | item of items; track item + | ^^^^^ +-------------------------------------------------------------------------------- +NGMicrosyntaxKeyedExpression { + "key": "Identifier", + "expression": "Identifier" +} +> 1 | item of items; track item + | ^^^^^^^^^^ +-------------------------------------------------------------------------------- +Identifier { + "name": "Track" +} +> 1 | item of items; track item + | ^^^^^ +-------------------------------------------------------------------------------- +Identifier { + "name": "item" +} +> 1 | item of items; track item + | ^^^^ +`; + +exports[` item of items; track item 2`] = ` +{ + "body": [ + { + "end": 5, + "left": { + "end": 5, + "name": "item", + "range": [ + 1, + 5, + ], + "start": 1, + "type": "Identifier", + }, + "range": [ + 1, + 5, + ], + "right": { + "end": 14, + "name": "items", + "range": [ + 9, + 14, + ], + "start": 9, + "type": "Identifier", + }, + "start": 1, + "type": "NGMicrosyntaxOfExpression", + }, + { + "end": 26, + "expression": { + "end": 26, + "name": "item", + "range": [ + 22, + 26, + ], + "start": 22, + "type": "Identifier", + }, + "key": { + "end": 21, + "name": "Track", + "range": [ + 16, + 21, + ], + "start": 16, + "type": "Identifier", + }, + "range": [ + 16, + 26, + ], + "start": 16, + "type": "NGMicrosyntaxKeyedExpression", + }, + ], + "end": 26, + "range": [ + 1, + 26, + ], + "start": 1, + "type": "NGMicrosyntax", +} +`; + +exports[` item of items;index as i; trackBy: trackByFn 1`] = ` +NGMicrosyntax { + "body": [ + "NGMicrosyntaxOfExpression", + "NGMicrosyntaxAsExpression", + "NGMicrosyntaxKeyedExpression" + ] +} +> 1 | item of items;index as i; trackBy: trackByFn + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +-------------------------------------------------------------------------------- +NGMicrosyntaxOfExpression { + "left": "Identifier", + "right": "Identifier" +} +> 1 | item of items;index as i; trackBy: trackByFn + | ^^^^ +-------------------------------------------------------------------------------- +Identifier { + "name": "item" +} +> 1 | item of items;index as i; trackBy: trackByFn + | ^^^^ +-------------------------------------------------------------------------------- +Identifier { + "name": "items" +} +> 1 | item of items;index as i; trackBy: trackByFn + | ^^^^^ +-------------------------------------------------------------------------------- +NGMicrosyntaxAsExpression { + "expression": "Identifier", + "alias": "Identifier" +} +> 1 | item of items;index as i; trackBy: trackByFn + | ^^^^^^^^^^ +-------------------------------------------------------------------------------- +Identifier { + "name": "index" +} +> 1 | item of items;index as i; trackBy: trackByFn + | ^^^^^ +-------------------------------------------------------------------------------- +Identifier { + "name": "i" +} +> 1 | item of items;index as i; trackBy: trackByFn + | ^ +-------------------------------------------------------------------------------- +NGMicrosyntaxKeyedExpression { + "key": "Identifier", + "expression": "Identifier" +} +> 1 | item of items;index as i; trackBy: trackByFn + | ^^^^^^^^^^^^^^^^^^ +-------------------------------------------------------------------------------- +Identifier { + "name": "TrackBy" +} +> 1 | item of items;index as i; trackBy: trackByFn + | ^^^^^^^ +-------------------------------------------------------------------------------- +Identifier { + "name": "trackByFn" +} +> 1 | item of items;index as i; trackBy: trackByFn + | ^^^^^^^^^ +`; + +exports[` item of items;index as i; trackBy: trackByFn 2`] = ` +{ + "body": [ + { + "end": 5, + "left": { + "end": 5, + "name": "item", + "range": [ + 1, + 5, + ], + "start": 1, + "type": "Identifier", + }, + "range": [ + 1, + 5, + ], + "right": { + "end": 14, + "name": "items", + "range": [ + 9, + 14, + ], + "start": 9, + "type": "Identifier", + }, + "start": 1, + "type": "NGMicrosyntaxOfExpression", + }, + { + "alias": { + "end": 25, + "name": "i", + "range": [ + 24, + 25, + ], + "start": 24, + "type": "Identifier", + }, + "end": 25, + "expression": { + "end": 20, + "name": "index", + "range": [ + 15, + 20, + ], + "start": 15, + "type": "Identifier", + }, + "range": [ + 15, + 25, + ], + "start": 15, + "type": "NGMicrosyntaxAsExpression", + }, + { + "end": 45, + "expression": { + "end": 45, + "name": "trackByFn", + "range": [ + 36, + 45, + ], + "start": 36, + "type": "Identifier", + }, + "key": { + "end": 34, + "name": "TrackBy", + "range": [ + 27, + 34, + ], + "start": 27, + "type": "Identifier", + }, + "range": [ + 27, + 45, + ], + "start": 27, + "type": "NGMicrosyntaxKeyedExpression", + }, + ], + "end": 45, + "range": [ + 1, + 45, + ], + "start": 1, + "type": "NGMicrosyntax", +} +`; + exports[` let "\\"" 1`] = ` NGMicrosyntax { "body": [ - "NGMicrosyntaxLet" + "VariableDeclaration" + ] +} +> 1 | let "\\"" + | ^^^^^^^^ +-------------------------------------------------------------------------------- +VariableDeclaration { + "kind": "let", + "declarations": [ + "VariableDeclarator" ] } > 1 | let "\\"" | ^^^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxLet { - "key": "NGMicrosyntaxKey", - "value": null +VariableDeclarator { + "id": "Identifier", + "init": null } > 1 | let "\\"" | ^^^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxKey { +Identifier { "name": "\\"" } > 1 | let "\\"" | ^^^^ `; +exports[` let "\\"" 2`] = ` +{ + "body": [ + { + "declarations": [ + { + "end": 9, + "id": { + "end": 9, + "name": """, + "range": [ + 5, + 9, + ], + "start": 5, + "type": "Identifier", + }, + "init": null, + "range": [ + 1, + 9, + ], + "start": 1, + "type": "VariableDeclarator", + }, + ], + "end": 9, + "kind": "let", + "range": [ + 1, + 9, + ], + "start": 1, + "type": "VariableDeclaration", + }, + ], + "end": 9, + "range": [ + 1, + 9, + ], + "start": 1, + "type": "NGMicrosyntax", +} +`; + exports[` let "a" = "b" ; "c" as "d" 1`] = ` NGMicrosyntax { "body": [ - "NGMicrosyntaxLet", - "NGMicrosyntaxAs" + "VariableDeclaration", + "NGMicrosyntaxAsExpression" ] } > 1 | let "a" = "b" ; "c" as "d" | ^^^^^^^^^^^^^^^^^^^^^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxLet { - "key": "NGMicrosyntaxKey", - "value": "NGMicrosyntaxKey" +VariableDeclaration { + "kind": "let", + "declarations": [ + "VariableDeclarator" + ] +} +> 1 | let "a" = "b" ; "c" as "d" + | ^^^^^^^^^^^^^^^ +-------------------------------------------------------------------------------- +VariableDeclarator { + "id": "Identifier", + "init": "Identifier" } > 1 | let "a" = "b" ; "c" as "d" - | ^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxKey { +Identifier { "name": "a" } > 1 | let "a" = "b" ; "c" as "d" | ^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxKey { +Identifier { "name": "b" } > 1 | let "a" = "b" ; "c" as "d" | ^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxAs { - "key": "NGMicrosyntaxKey", - "alias": "NGMicrosyntaxKey" +NGMicrosyntaxAsExpression { + "expression": "Identifier", + "alias": "Identifier" } > 1 | let "a" = "b" ; "c" as "d" | ^^^^^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxKey { +Identifier { "name": "c" } > 1 | let "a" = "b" ; "c" as "d" | ^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxKey { +Identifier { "name": "d" } > 1 | let "a" = "b" ; "c" as "d" | ^^^ `; +exports[` let "a" = "b" ; "c" as "d" 2`] = ` +{ + "body": [ + { + "declarations": [ + { + "end": 16, + "id": { + "end": 8, + "name": "a", + "range": [ + 5, + 8, + ], + "start": 5, + "type": "Identifier", + }, + "init": { + "end": 14, + "name": "b", + "range": [ + 11, + 14, + ], + "start": 11, + "type": "Identifier", + }, + "range": [ + 1, + 16, + ], + "start": 1, + "type": "VariableDeclarator", + }, + ], + "end": 16, + "kind": "let", + "range": [ + 1, + 16, + ], + "start": 1, + "type": "VariableDeclaration", + }, + { + "alias": { + "end": 27, + "name": "d", + "range": [ + 24, + 27, + ], + "start": 24, + "type": "Identifier", + }, + "end": 27, + "expression": { + "end": 20, + "name": "c", + "range": [ + 17, + 20, + ], + "start": 17, + "type": "Identifier", + }, + "range": [ + 17, + 27, + ], + "start": 17, + "type": "NGMicrosyntaxAsExpression", + }, + ], + "end": 27, + "range": [ + 1, + 27, + ], + "start": 1, + "type": "NGMicrosyntax", +} +`; + exports[` let hero 1`] = ` NGMicrosyntax { "body": [ - "NGMicrosyntaxLet" + "VariableDeclaration" + ] +} +> 1 | let hero + | ^^^^^^^^ +-------------------------------------------------------------------------------- +VariableDeclaration { + "kind": "let", + "declarations": [ + "VariableDeclarator" ] } > 1 | let hero | ^^^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxLet { - "key": "NGMicrosyntaxKey", - "value": null +VariableDeclarator { + "id": "Identifier", + "init": null } > 1 | let hero | ^^^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxKey { +Identifier { "name": "hero" } > 1 | let hero | ^^^^ `; +exports[` let hero 2`] = ` +{ + "body": [ + { + "declarations": [ + { + "end": 9, + "id": { + "end": 9, + "name": "hero", + "range": [ + 5, + 9, + ], + "start": 5, + "type": "Identifier", + }, + "init": null, + "range": [ + 1, + 9, + ], + "start": 1, + "type": "VariableDeclarator", + }, + ], + "end": 9, + "kind": "let", + "range": [ + 1, + 9, + ], + "start": 1, + "type": "VariableDeclaration", + }, + ], + "end": 9, + "range": [ + 1, + 9, + ], + "start": 1, + "type": "NGMicrosyntax", +} +`; + exports[` let hero ; of : heroes 1`] = ` NGMicrosyntax { "body": [ - "NGMicrosyntaxLet", - "NGMicrosyntaxKeyedExpression" + "NGMicrosyntaxOfExpression" ] } > 1 | let hero ; of : heroes | ^^^^^^^^^^^^^^^^^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxLet { - "key": "NGMicrosyntaxKey", - "value": null -} -> 1 | let hero ; of : heroes - | ^^^^^^^^ --------------------------------------------------------------------------------- -NGMicrosyntaxKey { - "name": "hero" +NGMicrosyntaxOfExpression { + "left": "VariableDeclaration", + "right": "Identifier" } > 1 | let hero ; of : heroes - | ^^^^ + | ^^^^^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxKeyedExpression { - "key": "NGMicrosyntaxKey", - "expression": "NGMicrosyntaxExpression" +VariableDeclaration { + "kind": "let", + "declarations": [ + "VariableDeclarator" + ] } > 1 | let hero ; of : heroes - | ^^^^^^^^^^^ + | ^^^^^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxKey { - "name": "of" +VariableDeclarator { + "id": "Identifier", + "init": null } > 1 | let hero ; of : heroes - | ^^ + | ^^^^^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxExpression { - "expression": "Identifier", - "alias": null +Identifier { + "name": "hero" } > 1 | let hero ; of : heroes - | ^^^^^^ + | ^^^^ -------------------------------------------------------------------------------- Identifier { "name": "heroes" @@ -493,81 +1375,478 @@ Identifier { | ^^^^^^ `; +exports[` let hero ; of : heroes 2`] = ` +{ + "body": [ + { + "end": 11, + "left": { + "declarations": [ + { + "end": 11, + "id": { + "end": 9, + "name": "hero", + "range": [ + 5, + 9, + ], + "start": 5, + "type": "Identifier", + }, + "init": null, + "range": [ + 1, + 11, + ], + "start": 1, + "type": "VariableDeclarator", + }, + ], + "end": 11, + "kind": "let", + "range": [ + 1, + 11, + ], + "start": 1, + "type": "VariableDeclaration", + }, + "range": [ + 1, + 11, + ], + "right": { + "end": 23, + "name": "heroes", + "range": [ + 17, + 23, + ], + "start": 17, + "type": "Identifier", + }, + "start": 1, + "type": "NGMicrosyntaxOfExpression", + }, + ], + "end": 23, + "range": [ + 1, + 23, + ], + "start": 1, + "type": "NGMicrosyntax", +} +`; + exports[` let hero = hello 1`] = ` NGMicrosyntax { "body": [ - "NGMicrosyntaxLet" + "VariableDeclaration" + ] +} +> 1 | let hero = hello + | ^^^^^^^^^^^^^^^^ +-------------------------------------------------------------------------------- +VariableDeclaration { + "kind": "let", + "declarations": [ + "VariableDeclarator" ] } > 1 | let hero = hello | ^^^^^^^^^^^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxLet { - "key": "NGMicrosyntaxKey", - "value": "NGMicrosyntaxKey" +VariableDeclarator { + "id": "Identifier", + "init": "Identifier" } > 1 | let hero = hello | ^^^^^^^^^^^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxKey { +Identifier { "name": "hero" } > 1 | let hero = hello | ^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxKey { +Identifier { "name": "hello" } > 1 | let hero = hello | ^^^^^ `; +exports[` let hero = hello 2`] = ` +{ + "body": [ + { + "declarations": [ + { + "end": 17, + "id": { + "end": 9, + "name": "hero", + "range": [ + 5, + 9, + ], + "start": 5, + "type": "Identifier", + }, + "init": { + "end": 17, + "name": "hello", + "range": [ + 12, + 17, + ], + "start": 12, + "type": "Identifier", + }, + "range": [ + 1, + 17, + ], + "start": 1, + "type": "VariableDeclarator", + }, + ], + "end": 17, + "kind": "let", + "range": [ + 1, + 17, + ], + "start": 1, + "type": "VariableDeclaration", + }, + ], + "end": 17, + "range": [ + 1, + 17, + ], + "start": 1, + "type": "NGMicrosyntax", +} +`; + exports[` let hero of heroes 1`] = ` NGMicrosyntax { "body": [ - "NGMicrosyntaxLet", - "NGMicrosyntaxKeyedExpression" + "NGMicrosyntaxOfExpression" ] } > 1 | let hero of heroes | ^^^^^^^^^^^^^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxLet { - "key": "NGMicrosyntaxKey", - "value": null +NGMicrosyntaxOfExpression { + "left": "VariableDeclaration", + "right": "Identifier" } > 1 | let hero of heroes | ^^^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxKey { - "name": "hero" +VariableDeclaration { + "kind": "let", + "declarations": [ + "VariableDeclarator" + ] } > 1 | let hero of heroes - | ^^^^ + | ^^^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxKeyedExpression { - "key": "NGMicrosyntaxKey", - "expression": "NGMicrosyntaxExpression" +VariableDeclarator { + "id": "Identifier", + "init": null } > 1 | let hero of heroes - | ^^^^^^^^^ + | ^^^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxKey { - "name": "of" +Identifier { + "name": "hero" } > 1 | let hero of heroes - | ^^ + | ^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxExpression { - "expression": "Identifier", - "alias": null +Identifier { + "name": "heroes" } > 1 | let hero of heroes | ^^^^^^ +`; + +exports[` let hero of heroes 2`] = ` +{ + "body": [ + { + "end": 9, + "left": { + "declarations": [ + { + "end": 9, + "id": { + "end": 9, + "name": "hero", + "range": [ + 5, + 9, + ], + "start": 5, + "type": "Identifier", + }, + "init": null, + "range": [ + 1, + 9, + ], + "start": 1, + "type": "VariableDeclarator", + }, + ], + "end": 9, + "kind": "let", + "range": [ + 1, + 9, + ], + "start": 1, + "type": "VariableDeclaration", + }, + "range": [ + 1, + 9, + ], + "right": { + "end": 19, + "name": "heroes", + "range": [ + 13, + 19, + ], + "start": 13, + "type": "Identifier", + }, + "start": 1, + "type": "NGMicrosyntaxOfExpression", + }, + ], + "end": 19, + "range": [ + 1, + 19, + ], + "start": 1, + "type": "NGMicrosyntax", +} +`; + +exports[` let item of items; index as i; trackBy: trackByFn 1`] = ` +NGMicrosyntax { + "body": [ + "NGMicrosyntaxOfExpression", + "NGMicrosyntaxAsExpression", + "NGMicrosyntaxKeyedExpression" + ] +} +> 1 | let item of items; index as i; trackBy: trackByFn + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +-------------------------------------------------------------------------------- +NGMicrosyntaxOfExpression { + "left": "VariableDeclaration", + "right": "Identifier" +} +> 1 | let item of items; index as i; trackBy: trackByFn + | ^^^^^^^^ +-------------------------------------------------------------------------------- +VariableDeclaration { + "kind": "let", + "declarations": [ + "VariableDeclarator" + ] +} +> 1 | let item of items; index as i; trackBy: trackByFn + | ^^^^^^^^ +-------------------------------------------------------------------------------- +VariableDeclarator { + "id": "Identifier", + "init": null +} +> 1 | let item of items; index as i; trackBy: trackByFn + | ^^^^^^^^ -------------------------------------------------------------------------------- Identifier { - "name": "heroes" + "name": "item" +} +> 1 | let item of items; index as i; trackBy: trackByFn + | ^^^^ +-------------------------------------------------------------------------------- +Identifier { + "name": "items" +} +> 1 | let item of items; index as i; trackBy: trackByFn + | ^^^^^ +-------------------------------------------------------------------------------- +NGMicrosyntaxAsExpression { + "expression": "Identifier", + "alias": "Identifier" +} +> 1 | let item of items; index as i; trackBy: trackByFn + | ^^^^^^^^^^ +-------------------------------------------------------------------------------- +Identifier { + "name": "index" +} +> 1 | let item of items; index as i; trackBy: trackByFn + | ^^^^^ +-------------------------------------------------------------------------------- +Identifier { + "name": "i" +} +> 1 | let item of items; index as i; trackBy: trackByFn + | ^ +-------------------------------------------------------------------------------- +NGMicrosyntaxKeyedExpression { + "key": "Identifier", + "expression": "Identifier" +} +> 1 | let item of items; index as i; trackBy: trackByFn + | ^^^^^^^^^^^^^^^^^^ +-------------------------------------------------------------------------------- +Identifier { + "name": "TrackBy" +} +> 1 | let item of items; index as i; trackBy: trackByFn + | ^^^^^^^ +-------------------------------------------------------------------------------- +Identifier { + "name": "trackByFn" +} +> 1 | let item of items; index as i; trackBy: trackByFn + | ^^^^^^^^^ +`; + +exports[` let item of items; index as i; trackBy: trackByFn 2`] = ` +{ + "body": [ + { + "end": 9, + "left": { + "declarations": [ + { + "end": 9, + "id": { + "end": 9, + "name": "item", + "range": [ + 5, + 9, + ], + "start": 5, + "type": "Identifier", + }, + "init": null, + "range": [ + 1, + 9, + ], + "start": 1, + "type": "VariableDeclarator", + }, + ], + "end": 9, + "kind": "let", + "range": [ + 1, + 9, + ], + "start": 1, + "type": "VariableDeclaration", + }, + "range": [ + 1, + 9, + ], + "right": { + "end": 18, + "name": "items", + "range": [ + 13, + 18, + ], + "start": 13, + "type": "Identifier", + }, + "start": 1, + "type": "NGMicrosyntaxOfExpression", + }, + { + "alias": { + "end": 30, + "name": "i", + "range": [ + 29, + 30, + ], + "start": 29, + "type": "Identifier", + }, + "end": 30, + "expression": { + "end": 25, + "name": "index", + "range": [ + 20, + 25, + ], + "start": 20, + "type": "Identifier", + }, + "range": [ + 20, + 30, + ], + "start": 20, + "type": "NGMicrosyntaxAsExpression", + }, + { + "end": 50, + "expression": { + "end": 50, + "name": "trackByFn", + "range": [ + 41, + 50, + ], + "start": 41, + "type": "Identifier", + }, + "key": { + "end": 39, + "name": "TrackBy", + "range": [ + 32, + 39, + ], + "start": 32, + "type": "Identifier", + }, + "range": [ + 32, + 50, + ], + "start": 32, + "type": "NGMicrosyntaxKeyedExpression", + }, + ], + "end": 50, + "range": [ + 1, + 50, + ], + "start": 1, + "type": "NGMicrosyntax", } -> 1 | let hero of heroes - | ^^^^^^ `; diff --git a/tests/transform-microsyntax.test.ts b/tests/transform-microsyntax.test.ts index 6964acca..d0a7ffe9 100644 --- a/tests/transform-microsyntax.test.ts +++ b/tests/transform-microsyntax.test.ts @@ -6,36 +6,37 @@ import { snapshotAst } from './helpers.js'; test.each` input | types ${''} | ${[]} - ${' let hero '} | ${['NGMicrosyntaxLet']} - ${' let hero = hello '} | ${['NGMicrosyntaxLet']} - ${' let hero of heroes '} | ${['NGMicrosyntaxLet', 'NGMicrosyntaxKeyedExpression']} - ${' let hero ; of : heroes '} | ${['NGMicrosyntaxLet', 'NGMicrosyntaxKeyedExpression']} - ${' as b '} | ${['NGMicrosyntaxAs']} - ${' a '} | ${['NGMicrosyntaxExpression']} - ${' a as b '} | ${['NGMicrosyntaxExpression']} - ${' a , b '} | ${['NGMicrosyntaxExpression', 'NGMicrosyntaxKey']} - ${' a ; b '} | ${['NGMicrosyntaxExpression', 'NGMicrosyntaxKey']} - ${' a ; b c '} | ${['NGMicrosyntaxExpression', 'NGMicrosyntaxKeyedExpression']} - ${' a ; b : c '} | ${['NGMicrosyntaxExpression', 'NGMicrosyntaxKeyedExpression']} - ${' a ; b : c as d '} | ${['NGMicrosyntaxExpression', 'NGMicrosyntaxKeyedExpression']} - ${' a ; b as c '} | ${['NGMicrosyntaxExpression', 'NGMicrosyntaxAs']} - ${' let "a" = "b" ; "c" as "d" '} | ${['NGMicrosyntaxLet', 'NGMicrosyntaxAs']} - ${' let "\\"" '} | ${['NGMicrosyntaxLet']} - ${' let item of items; index as i; trackBy: trackByFn'} | ${[]} - ${' item of items; track item '} | ${[]} - ${' item of items;index as i; trackBy: trackByFn'} | ${[]} + ${' let hero '} | ${['VariableDeclaration']} + ${' let hero = hello '} | ${['VariableDeclaration']} + ${' let hero of heroes '} | ${['NGMicrosyntaxOfExpression']} + ${' let hero ; of : heroes '} | ${['VariableDeclaration', 'NGMicrosyntaxKeyedExpression']} + ${' as b '} | ${['NGMicrosyntaxAsExpression']} + ${' a '} | ${['Identifier']} + ${' a as b '} | ${['Identifier', 'NGMicrosyntaxAsExpression']} + ${' a , b '} | ${['Identifier', 'Identifier']} + ${' a ; b '} | ${['Identifier', 'Identifier']} + ${' a ; b c '} | ${['Identifier', 'NGMicrosyntaxKeyedExpression']} + ${' a ; b : c '} | ${['Identifier', 'NGMicrosyntaxKeyedExpression']} + ${' a ; b : c as d '} | ${['Identifier', 'NGMicrosyntaxKeyedExpression', 'NGMicrosyntaxAsExpression']} + ${' a ; b as c '} | ${['Identifier', 'NGMicrosyntaxAsExpression']} + ${' let "a" = "b" ; "c" as "d" '} | ${['VariableDeclaration', 'NGMicrosyntaxAsExpression']} + ${' let "\\"" '} | ${['VariableDeclaration']} + ${' let item of items; index as i; trackBy: trackByFn'} | ${['NGMicrosyntaxOfExpression', 'NGMicrosyntaxAsExpression', 'NGMicrosyntaxKeyedExpression']} + ${' item of items; track item '} | ${['NGMicrosyntaxOfExpression', 'NGMicrosyntaxKeyedExpression']} + ${' item of items;index as i; trackBy: trackByFn'} | ${['NGMicrosyntaxOfExpression', 'NGMicrosyntaxAsExpression', 'NGMicrosyntaxKeyedExpression']} `('$input', ({ input, types }) => { const ast = parseTemplateBindings(input); expect(snapshotAst(ast, input)).toMatchSnapshot(); + expect(ast).toMatchSnapshot(); expect(ast.body.map((node) => node.type)).toEqual(types); }); -test('Shorthand', () => { - const code = 'someTmpl; context: {app}'; - const ast = parseTemplateBindings(code); - const secondExpression = ast.body[1] as NGMicrosyntaxKeyedExpression; - const objectExpression = secondExpression.expression - .expression as b.ObjectExpression; - const firstProperty = objectExpression.properties[0] as b.ObjectProperty; - expect(firstProperty.shorthand).toBe(true); -}); +// test('Shorthand', () => { +// const code = 'someTmpl; context: {app}'; +// const ast = parseTemplateBindings(code); +// const secondExpression = ast.body[1] as NGMicrosyntaxKeyedExpression; +// const objectExpression = secondExpression.expression +// .expression as b.ObjectExpression; +// const firstProperty = objectExpression.properties[0] as b.ObjectProperty; +// expect(firstProperty.shorthand).toBe(true); +// }); From 91f256b391d82847d28020a91805512426dd31a2 Mon Sep 17 00:00:00 2001 From: fisker Date: Wed, 15 Nov 2023 01:18:08 +0800 Subject: [PATCH 3/4] Test --- src/transform-microsyntax.ts | 5 +- .../transform-microsyntax.test.ts.snap | 99 +++++++++++-------- 2 files changed, 62 insertions(+), 42 deletions(-) diff --git a/src/transform-microsyntax.ts b/src/transform-microsyntax.ts index 4d76733b..bd3e0224 100644 --- a/src/transform-microsyntax.ts +++ b/src/transform-microsyntax.ts @@ -191,7 +191,10 @@ function transformTemplateBindings({ return ( isExpressionBinding(node) && node.key.source === 'Of' && - node.value instanceof ng.ASTWithSource + node.value instanceof ng.ASTWithSource && + context.text + .slice(node.key.span.end, node.value.sourceSpan.start) + .trim() === '' ); } diff --git a/tests/__snapshots__/transform-microsyntax.test.ts.snap b/tests/__snapshots__/transform-microsyntax.test.ts.snap index 3e930c7f..d152ca01 100644 --- a/tests/__snapshots__/transform-microsyntax.test.ts.snap +++ b/tests/__snapshots__/transform-microsyntax.test.ts.snap @@ -1333,19 +1333,13 @@ exports[` let hero 2`] = ` exports[` let hero ; of : heroes 1`] = ` NGMicrosyntax { "body": [ - "NGMicrosyntaxOfExpression" + "VariableDeclaration", + "NGMicrosyntaxKeyedExpression" ] } > 1 | let hero ; of : heroes | ^^^^^^^^^^^^^^^^^^^^^^ -------------------------------------------------------------------------------- -NGMicrosyntaxOfExpression { - "left": "VariableDeclaration", - "right": "Identifier" -} -> 1 | let hero ; of : heroes - | ^^^^^^^^^^ --------------------------------------------------------------------------------- VariableDeclaration { "kind": "let", "declarations": [ @@ -1368,6 +1362,19 @@ Identifier { > 1 | let hero ; of : heroes | ^^^^ -------------------------------------------------------------------------------- +NGMicrosyntaxKeyedExpression { + "key": "Identifier", + "expression": "Identifier" +} +> 1 | let hero ; of : heroes + | ^^^^^^^^^^^ +-------------------------------------------------------------------------------- +Identifier { + "name": "Of" +} +> 1 | let hero ; of : heroes + | ^^ +-------------------------------------------------------------------------------- Identifier { "name": "heroes" } @@ -1379,44 +1386,40 @@ exports[` let hero ; of : heroes 2`] = ` { "body": [ { - "end": 11, - "left": { - "declarations": [ - { - "end": 11, - "id": { - "end": 9, - "name": "hero", - "range": [ - 5, - 9, - ], - "start": 5, - "type": "Identifier", - }, - "init": null, + "declarations": [ + { + "end": 11, + "id": { + "end": 9, + "name": "hero", "range": [ - 1, - 11, + 5, + 9, ], - "start": 1, - "type": "VariableDeclarator", + "start": 5, + "type": "Identifier", }, - ], - "end": 11, - "kind": "let", - "range": [ - 1, - 11, - ], - "start": 1, - "type": "VariableDeclaration", - }, + "init": null, + "range": [ + 1, + 11, + ], + "start": 1, + "type": "VariableDeclarator", + }, + ], + "end": 11, + "kind": "let", "range": [ 1, 11, ], - "right": { + "start": 1, + "type": "VariableDeclaration", + }, + { + "end": 23, + "expression": { "end": 23, "name": "heroes", "range": [ @@ -1426,8 +1429,22 @@ exports[` let hero ; of : heroes 2`] = ` "start": 17, "type": "Identifier", }, - "start": 1, - "type": "NGMicrosyntaxOfExpression", + "key": { + "end": 14, + "name": "Of", + "range": [ + 12, + 14, + ], + "start": 12, + "type": "Identifier", + }, + "range": [ + 12, + 23, + ], + "start": 12, + "type": "NGMicrosyntaxKeyedExpression", }, ], "end": 23, From 64dcfadbec9de1a3f6580adb9485de5114e3dfcd Mon Sep 17 00:00:00 2001 From: fisker Date: Wed, 15 Nov 2023 01:20:51 +0800 Subject: [PATCH 4/4] Snapshot --- .../transform-microsyntax.test.ts.snap | 1769 +++++++++-------- tests/transform-microsyntax.test.ts | 2 +- 2 files changed, 914 insertions(+), 857 deletions(-) diff --git a/tests/__snapshots__/transform-microsyntax.test.ts.snap b/tests/__snapshots__/transform-microsyntax.test.ts.snap index d152ca01..b4d70a6e 100644 --- a/tests/__snapshots__/transform-microsyntax.test.ts.snap +++ b/tests/__snapshots__/transform-microsyntax.test.ts.snap @@ -10,14 +10,17 @@ NGMicrosyntax { exports[` 2`] = ` { - "body": [], - "end": 0, - "range": [ - 0, - 0, - ], - "start": 0, - "type": "NGMicrosyntax", + "ast": { + "body": [], + "end": 0, + "range": [ + 0, + 0, + ], + "start": 0, + "type": "NGMicrosyntax", + }, + "text": "", } `; @@ -39,25 +42,28 @@ Identifier { exports[` a 2`] = ` { - "body": [ - { - "end": 2, - "name": "a", - "range": [ - 1, - 2, - ], - "start": 1, - "type": "Identifier", - }, - ], - "end": 2, - "range": [ - 1, - 2, - ], - "start": 1, - "type": "NGMicrosyntax", + "ast": { + "body": [ + { + "end": 2, + "name": "a", + "range": [ + 1, + 2, + ], + "start": 1, + "type": "Identifier", + }, + ], + "end": 2, + "range": [ + 1, + 2, + ], + "start": 1, + "type": "NGMicrosyntax", + }, + "text": " a ", } `; @@ -86,35 +92,38 @@ Identifier { exports[` a , b 2`] = ` { - "body": [ - { - "end": 2, - "name": "a", - "range": [ - 1, - 2, - ], - "start": 1, - "type": "Identifier", - }, - { - "end": 6, - "name": "B", - "range": [ - 5, - 6, - ], - "start": 5, - "type": "Identifier", - }, - ], - "end": 6, - "range": [ - 1, - 6, - ], - "start": 1, - "type": "NGMicrosyntax", + "ast": { + "body": [ + { + "end": 2, + "name": "a", + "range": [ + 1, + 2, + ], + "start": 1, + "type": "Identifier", + }, + { + "end": 6, + "name": "B", + "range": [ + 5, + 6, + ], + "start": 5, + "type": "Identifier", + }, + ], + "end": 6, + "range": [ + 1, + 6, + ], + "start": 1, + "type": "NGMicrosyntax", + }, + "text": " a , b ", } `; @@ -143,35 +152,38 @@ Identifier { exports[` a ; b 2`] = ` { - "body": [ - { - "end": 2, - "name": "a", - "range": [ - 1, - 2, - ], - "start": 1, - "type": "Identifier", - }, - { - "end": 6, - "name": "B", - "range": [ - 5, - 6, - ], - "start": 5, - "type": "Identifier", - }, - ], - "end": 6, - "range": [ - 1, - 6, - ], - "start": 1, - "type": "NGMicrosyntax", + "ast": { + "body": [ + { + "end": 2, + "name": "a", + "range": [ + 1, + 2, + ], + "start": 1, + "type": "Identifier", + }, + { + "end": 6, + "name": "B", + "range": [ + 5, + 6, + ], + "start": 5, + "type": "Identifier", + }, + ], + "end": 6, + "range": [ + 1, + 6, + ], + "start": 1, + "type": "NGMicrosyntax", + }, + "text": " a ; b ", } `; @@ -213,54 +225,57 @@ Identifier { exports[` a ; b : c 2`] = ` { - "body": [ - { - "end": 2, - "name": "a", - "range": [ - 1, - 2, - ], - "start": 1, - "type": "Identifier", - }, - { - "end": 10, - "expression": { - "end": 10, - "name": "c", + "ast": { + "body": [ + { + "end": 2, + "name": "a", "range": [ - 9, - 10, + 1, + 2, ], - "start": 9, + "start": 1, "type": "Identifier", }, - "key": { - "end": 6, - "name": "B", + { + "end": 10, + "expression": { + "end": 10, + "name": "c", + "range": [ + 9, + 10, + ], + "start": 9, + "type": "Identifier", + }, + "key": { + "end": 6, + "name": "B", + "range": [ + 5, + 6, + ], + "start": 5, + "type": "Identifier", + }, "range": [ 5, - 6, + 10, ], "start": 5, - "type": "Identifier", + "type": "NGMicrosyntaxKeyedExpression", }, - "range": [ - 5, - 10, - ], - "start": 5, - "type": "NGMicrosyntaxKeyedExpression", - }, - ], - "end": 10, - "range": [ - 1, - 10, - ], - "start": 1, - "type": "NGMicrosyntax", + ], + "end": 10, + "range": [ + 1, + 10, + ], + "start": 1, + "type": "NGMicrosyntax", + }, + "text": " a ; b : c ", } `; @@ -322,83 +337,86 @@ Identifier { exports[` a ; b : c as d 2`] = ` { - "body": [ - { - "end": 2, - "name": "a", - "range": [ - 1, - 2, - ], - "start": 1, - "type": "Identifier", - }, - { - "end": 10, - "expression": { - "end": 10, - "name": "c", + "ast": { + "body": [ + { + "end": 2, + "name": "a", "range": [ - 9, - 10, + 1, + 2, ], - "start": 9, + "start": 1, "type": "Identifier", }, - "key": { - "end": 6, - "name": "B", + { + "end": 10, + "expression": { + "end": 10, + "name": "c", + "range": [ + 9, + 10, + ], + "start": 9, + "type": "Identifier", + }, + "key": { + "end": 6, + "name": "B", + "range": [ + 5, + 6, + ], + "start": 5, + "type": "Identifier", + }, "range": [ 5, - 6, + 10, ], "start": 5, - "type": "Identifier", + "type": "NGMicrosyntaxKeyedExpression", }, - "range": [ - 5, - 10, - ], - "start": 5, - "type": "NGMicrosyntaxKeyedExpression", - }, - { - "alias": { + { + "alias": { + "end": 15, + "name": "d", + "range": [ + 14, + 15, + ], + "start": 14, + "type": "Identifier", + }, "end": 15, - "name": "d", - "range": [ - 14, - 15, - ], - "start": 14, - "type": "Identifier", - }, - "end": 15, - "expression": { - "end": 6, - "name": "B", + "expression": { + "end": 6, + "name": "B", + "range": [ + 5, + 6, + ], + "start": 5, + "type": "Identifier", + }, "range": [ 5, - 6, + 15, ], "start": 5, - "type": "Identifier", + "type": "NGMicrosyntaxAsExpression", }, - "range": [ - 5, - 15, - ], - "start": 5, - "type": "NGMicrosyntaxAsExpression", - }, - ], - "end": 15, - "range": [ - 1, - 15, - ], - "start": 1, - "type": "NGMicrosyntax", + ], + "end": 15, + "range": [ + 1, + 15, + ], + "start": 1, + "type": "NGMicrosyntax", + }, + "text": " a ; b : c as d ", } `; @@ -440,54 +458,57 @@ Identifier { exports[` a ; b as c 2`] = ` { - "body": [ - { - "end": 2, - "name": "a", - "range": [ - 1, - 2, - ], - "start": 1, - "type": "Identifier", - }, - { - "alias": { - "end": 11, - "name": "c", + "ast": { + "body": [ + { + "end": 2, + "name": "a", "range": [ - 10, - 11, + 1, + 2, ], - "start": 10, + "start": 1, "type": "Identifier", }, - "end": 11, - "expression": { - "end": 6, - "name": "b", + { + "alias": { + "end": 11, + "name": "c", + "range": [ + 10, + 11, + ], + "start": 10, + "type": "Identifier", + }, + "end": 11, + "expression": { + "end": 6, + "name": "b", + "range": [ + 5, + 6, + ], + "start": 5, + "type": "Identifier", + }, "range": [ 5, - 6, + 11, ], "start": 5, - "type": "Identifier", + "type": "NGMicrosyntaxAsExpression", }, - "range": [ - 5, - 11, - ], - "start": 5, - "type": "NGMicrosyntaxAsExpression", - }, - ], - "end": 11, - "range": [ - 1, - 11, - ], - "start": 1, - "type": "NGMicrosyntax", + ], + "end": 11, + "range": [ + 1, + 11, + ], + "start": 1, + "type": "NGMicrosyntax", + }, + "text": " a ; b as c ", } `; @@ -529,54 +550,57 @@ Identifier { exports[` a ; b c 2`] = ` { - "body": [ - { - "end": 2, - "name": "a", - "range": [ - 1, - 2, - ], - "start": 1, - "type": "Identifier", - }, - { - "end": 8, - "expression": { - "end": 8, - "name": "c", + "ast": { + "body": [ + { + "end": 2, + "name": "a", "range": [ - 7, - 8, + 1, + 2, ], - "start": 7, + "start": 1, "type": "Identifier", }, - "key": { - "end": 6, - "name": "B", + { + "end": 8, + "expression": { + "end": 8, + "name": "c", + "range": [ + 7, + 8, + ], + "start": 7, + "type": "Identifier", + }, + "key": { + "end": 6, + "name": "B", + "range": [ + 5, + 6, + ], + "start": 5, + "type": "Identifier", + }, "range": [ 5, - 6, + 8, ], "start": 5, - "type": "Identifier", + "type": "NGMicrosyntaxKeyedExpression", }, - "range": [ - 5, - 8, - ], - "start": 5, - "type": "NGMicrosyntaxKeyedExpression", - }, - ], - "end": 8, - "range": [ - 1, - 8, - ], - "start": 1, - "type": "NGMicrosyntax", + ], + "end": 8, + "range": [ + 1, + 8, + ], + "start": 1, + "type": "NGMicrosyntax", + }, + "text": " a ; b c ", } `; @@ -618,54 +642,57 @@ Identifier { exports[` a as b 2`] = ` { - "body": [ - { - "end": 2, - "name": "a", - "range": [ - 1, - 2, - ], - "start": 1, - "type": "Identifier", - }, - { - "alias": { - "end": 7, - "name": "b", + "ast": { + "body": [ + { + "end": 2, + "name": "a", "range": [ - 6, - 7, + 1, + 2, ], - "start": 6, + "start": 1, "type": "Identifier", }, - "end": 7, - "expression": { - "end": 0, - "name": "", + { + "alias": { + "end": 7, + "name": "b", + "range": [ + 6, + 7, + ], + "start": 6, + "type": "Identifier", + }, + "end": 7, + "expression": { + "end": 0, + "name": "", + "range": [ + 0, + 0, + ], + "start": 0, + "type": "Identifier", + }, "range": [ - 0, - 0, + 1, + 7, ], - "start": 0, - "type": "Identifier", + "start": 1, + "type": "NGMicrosyntaxAsExpression", }, - "range": [ - 1, - 7, - ], - "start": 1, - "type": "NGMicrosyntaxAsExpression", - }, - ], - "end": 7, - "range": [ - 1, - 7, - ], - "start": 1, - "type": "NGMicrosyntax", + ], + "end": 7, + "range": [ + 1, + 7, + ], + "start": 1, + "type": "NGMicrosyntax", + }, + "text": " a as b ", } `; @@ -700,44 +727,47 @@ Identifier { exports[` as b 2`] = ` { - "body": [ - { - "alias": { + "ast": { + "body": [ + { + "alias": { + "end": 5, + "name": "b", + "range": [ + 4, + 5, + ], + "start": 4, + "type": "Identifier", + }, "end": 5, - "name": "b", + "expression": { + "end": 0, + "name": "", + "range": [ + 0, + 0, + ], + "start": 0, + "type": "Identifier", + }, "range": [ - 4, + 1, 5, ], - "start": 4, - "type": "Identifier", - }, - "end": 5, - "expression": { - "end": 0, - "name": "", - "range": [ - 0, - 0, - ], - "start": 0, - "type": "Identifier", + "start": 1, + "type": "NGMicrosyntaxAsExpression", }, - "range": [ - 1, - 5, - ], - "start": 1, - "type": "NGMicrosyntaxAsExpression", - }, - ], - "end": 5, - "range": [ - 1, - 5, - ], - "start": 1, - "type": "NGMicrosyntax", + ], + "end": 5, + "range": [ + 1, + 5, + ], + "start": 1, + "type": "NGMicrosyntax", + }, + "text": " as b ", } `; @@ -792,73 +822,76 @@ Identifier { exports[` item of items; track item 2`] = ` { - "body": [ - { - "end": 5, - "left": { + "ast": { + "body": [ + { "end": 5, - "name": "item", + "left": { + "end": 5, + "name": "item", + "range": [ + 1, + 5, + ], + "start": 1, + "type": "Identifier", + }, "range": [ 1, 5, ], + "right": { + "end": 14, + "name": "items", + "range": [ + 9, + 14, + ], + "start": 9, + "type": "Identifier", + }, "start": 1, - "type": "Identifier", - }, - "range": [ - 1, - 5, - ], - "right": { - "end": 14, - "name": "items", - "range": [ - 9, - 14, - ], - "start": 9, - "type": "Identifier", + "type": "NGMicrosyntaxOfExpression", }, - "start": 1, - "type": "NGMicrosyntaxOfExpression", - }, - { - "end": 26, - "expression": { + { "end": 26, - "name": "item", - "range": [ - 22, - 26, - ], - "start": 22, - "type": "Identifier", - }, - "key": { - "end": 21, - "name": "Track", + "expression": { + "end": 26, + "name": "item", + "range": [ + 22, + 26, + ], + "start": 22, + "type": "Identifier", + }, + "key": { + "end": 21, + "name": "Track", + "range": [ + 16, + 21, + ], + "start": 16, + "type": "Identifier", + }, "range": [ 16, - 21, + 26, ], "start": 16, - "type": "Identifier", + "type": "NGMicrosyntaxKeyedExpression", }, - "range": [ - 16, - 26, - ], - "start": 16, - "type": "NGMicrosyntaxKeyedExpression", - }, - ], - "end": 26, - "range": [ - 1, - 26, - ], - "start": 1, - "type": "NGMicrosyntax", + ], + "end": 26, + "range": [ + 1, + 26, + ], + "start": 1, + "type": "NGMicrosyntax", + }, + "text": " item of items; track item ", } `; @@ -933,102 +966,105 @@ Identifier { exports[` item of items;index as i; trackBy: trackByFn 2`] = ` { - "body": [ - { - "end": 5, - "left": { + "ast": { + "body": [ + { "end": 5, - "name": "item", + "left": { + "end": 5, + "name": "item", + "range": [ + 1, + 5, + ], + "start": 1, + "type": "Identifier", + }, "range": [ 1, 5, ], + "right": { + "end": 14, + "name": "items", + "range": [ + 9, + 14, + ], + "start": 9, + "type": "Identifier", + }, "start": 1, - "type": "Identifier", - }, - "range": [ - 1, - 5, - ], - "right": { - "end": 14, - "name": "items", - "range": [ - 9, - 14, - ], - "start": 9, - "type": "Identifier", + "type": "NGMicrosyntaxOfExpression", }, - "start": 1, - "type": "NGMicrosyntaxOfExpression", - }, - { - "alias": { + { + "alias": { + "end": 25, + "name": "i", + "range": [ + 24, + 25, + ], + "start": 24, + "type": "Identifier", + }, "end": 25, - "name": "i", - "range": [ - 24, - 25, - ], - "start": 24, - "type": "Identifier", - }, - "end": 25, - "expression": { - "end": 20, - "name": "index", + "expression": { + "end": 20, + "name": "index", + "range": [ + 15, + 20, + ], + "start": 15, + "type": "Identifier", + }, "range": [ 15, - 20, + 25, ], "start": 15, - "type": "Identifier", + "type": "NGMicrosyntaxAsExpression", }, - "range": [ - 15, - 25, - ], - "start": 15, - "type": "NGMicrosyntaxAsExpression", - }, - { - "end": 45, - "expression": { + { "end": 45, - "name": "trackByFn", - "range": [ - 36, - 45, - ], - "start": 36, - "type": "Identifier", - }, - "key": { - "end": 34, - "name": "TrackBy", + "expression": { + "end": 45, + "name": "trackByFn", + "range": [ + 36, + 45, + ], + "start": 36, + "type": "Identifier", + }, + "key": { + "end": 34, + "name": "TrackBy", + "range": [ + 27, + 34, + ], + "start": 27, + "type": "Identifier", + }, "range": [ 27, - 34, + 45, ], "start": 27, - "type": "Identifier", + "type": "NGMicrosyntaxKeyedExpression", }, - "range": [ - 27, - 45, - ], - "start": 27, - "type": "NGMicrosyntaxKeyedExpression", - }, - ], - "end": 45, - "range": [ - 1, - 45, - ], - "start": 1, - "type": "NGMicrosyntax", + ], + "end": 45, + "range": [ + 1, + 45, + ], + "start": 1, + "type": "NGMicrosyntax", + }, + "text": " item of items;index as i; trackBy: trackByFn", } `; @@ -1066,47 +1102,50 @@ Identifier { exports[` let "\\"" 2`] = ` { - "body": [ - { - "declarations": [ - { - "end": 9, - "id": { + "ast": { + "body": [ + { + "declarations": [ + { "end": 9, - "name": """, + "id": { + "end": 9, + "name": """, + "range": [ + 5, + 9, + ], + "start": 5, + "type": "Identifier", + }, + "init": null, "range": [ - 5, + 1, 9, ], - "start": 5, - "type": "Identifier", + "start": 1, + "type": "VariableDeclarator", }, - "init": null, - "range": [ - 1, - 9, - ], - "start": 1, - "type": "VariableDeclarator", - }, - ], - "end": 9, - "kind": "let", - "range": [ - 1, - 9, - ], - "start": 1, - "type": "VariableDeclaration", - }, - ], - "end": 9, - "range": [ - 1, - 9, - ], - "start": 1, - "type": "NGMicrosyntax", + ], + "end": 9, + "kind": "let", + "range": [ + 1, + 9, + ], + "start": 1, + "type": "VariableDeclaration", + }, + ], + "end": 9, + "range": [ + 1, + 9, + ], + "start": 1, + "type": "NGMicrosyntax", + }, + "text": " let "\\"" ", } `; @@ -1170,85 +1209,88 @@ Identifier { exports[` let "a" = "b" ; "c" as "d" 2`] = ` { - "body": [ - { - "declarations": [ - { - "end": 16, - "id": { - "end": 8, - "name": "a", - "range": [ - 5, - 8, - ], - "start": 5, - "type": "Identifier", - }, - "init": { - "end": 14, - "name": "b", + "ast": { + "body": [ + { + "declarations": [ + { + "end": 16, + "id": { + "end": 8, + "name": "a", + "range": [ + 5, + 8, + ], + "start": 5, + "type": "Identifier", + }, + "init": { + "end": 14, + "name": "b", + "range": [ + 11, + 14, + ], + "start": 11, + "type": "Identifier", + }, "range": [ - 11, - 14, + 1, + 16, ], - "start": 11, - "type": "Identifier", + "start": 1, + "type": "VariableDeclarator", }, + ], + "end": 16, + "kind": "let", + "range": [ + 1, + 16, + ], + "start": 1, + "type": "VariableDeclaration", + }, + { + "alias": { + "end": 27, + "name": "d", "range": [ - 1, - 16, + 24, + 27, ], - "start": 1, - "type": "VariableDeclarator", + "start": 24, + "type": "Identifier", }, - ], - "end": 16, - "kind": "let", - "range": [ - 1, - 16, - ], - "start": 1, - "type": "VariableDeclaration", - }, - { - "alias": { "end": 27, - "name": "d", - "range": [ - 24, - 27, - ], - "start": 24, - "type": "Identifier", - }, - "end": 27, - "expression": { - "end": 20, - "name": "c", + "expression": { + "end": 20, + "name": "c", + "range": [ + 17, + 20, + ], + "start": 17, + "type": "Identifier", + }, "range": [ 17, - 20, + 27, ], "start": 17, - "type": "Identifier", + "type": "NGMicrosyntaxAsExpression", }, - "range": [ - 17, - 27, - ], - "start": 17, - "type": "NGMicrosyntaxAsExpression", - }, - ], - "end": 27, - "range": [ - 1, - 27, - ], - "start": 1, - "type": "NGMicrosyntax", + ], + "end": 27, + "range": [ + 1, + 27, + ], + "start": 1, + "type": "NGMicrosyntax", + }, + "text": " let "a" = "b" ; "c" as "d" ", } `; @@ -1286,47 +1328,50 @@ Identifier { exports[` let hero 2`] = ` { - "body": [ - { - "declarations": [ - { - "end": 9, - "id": { + "ast": { + "body": [ + { + "declarations": [ + { "end": 9, - "name": "hero", + "id": { + "end": 9, + "name": "hero", + "range": [ + 5, + 9, + ], + "start": 5, + "type": "Identifier", + }, + "init": null, "range": [ - 5, + 1, 9, ], - "start": 5, - "type": "Identifier", + "start": 1, + "type": "VariableDeclarator", }, - "init": null, - "range": [ - 1, - 9, - ], - "start": 1, - "type": "VariableDeclarator", - }, - ], - "end": 9, - "kind": "let", - "range": [ - 1, - 9, - ], - "start": 1, - "type": "VariableDeclaration", - }, - ], - "end": 9, - "range": [ - 1, - 9, - ], - "start": 1, - "type": "NGMicrosyntax", + ], + "end": 9, + "kind": "let", + "range": [ + 1, + 9, + ], + "start": 1, + "type": "VariableDeclaration", + }, + ], + "end": 9, + "range": [ + 1, + 9, + ], + "start": 1, + "type": "NGMicrosyntax", + }, + "text": " let hero ", } `; @@ -1384,76 +1429,79 @@ Identifier { exports[` let hero ; of : heroes 2`] = ` { - "body": [ - { - "declarations": [ - { - "end": 11, - "id": { - "end": 9, - "name": "hero", + "ast": { + "body": [ + { + "declarations": [ + { + "end": 11, + "id": { + "end": 9, + "name": "hero", + "range": [ + 5, + 9, + ], + "start": 5, + "type": "Identifier", + }, + "init": null, "range": [ - 5, - 9, + 1, + 11, ], - "start": 5, - "type": "Identifier", + "start": 1, + "type": "VariableDeclarator", }, - "init": null, - "range": [ - 1, - 11, - ], - "start": 1, - "type": "VariableDeclarator", - }, - ], - "end": 11, - "kind": "let", - "range": [ - 1, - 11, - ], - "start": 1, - "type": "VariableDeclaration", - }, - { - "end": 23, - "expression": { - "end": 23, - "name": "heroes", + ], + "end": 11, + "kind": "let", "range": [ - 17, - 23, + 1, + 11, ], - "start": 17, - "type": "Identifier", + "start": 1, + "type": "VariableDeclaration", }, - "key": { - "end": 14, - "name": "Of", + { + "end": 23, + "expression": { + "end": 23, + "name": "heroes", + "range": [ + 17, + 23, + ], + "start": 17, + "type": "Identifier", + }, + "key": { + "end": 14, + "name": "Of", + "range": [ + 12, + 14, + ], + "start": 12, + "type": "Identifier", + }, "range": [ 12, - 14, + 23, ], "start": 12, - "type": "Identifier", + "type": "NGMicrosyntaxKeyedExpression", }, - "range": [ - 12, - 23, - ], - "start": 12, - "type": "NGMicrosyntaxKeyedExpression", - }, - ], - "end": 23, - "range": [ - 1, - 23, - ], - "start": 1, - "type": "NGMicrosyntax", + ], + "end": 23, + "range": [ + 1, + 23, + ], + "start": 1, + "type": "NGMicrosyntax", + }, + "text": " let hero ; of : heroes ", } `; @@ -1497,56 +1545,59 @@ Identifier { exports[` let hero = hello 2`] = ` { - "body": [ - { - "declarations": [ - { - "end": 17, - "id": { - "end": 9, - "name": "hero", - "range": [ - 5, - 9, - ], - "start": 5, - "type": "Identifier", - }, - "init": { + "ast": { + "body": [ + { + "declarations": [ + { "end": 17, - "name": "hello", + "id": { + "end": 9, + "name": "hero", + "range": [ + 5, + 9, + ], + "start": 5, + "type": "Identifier", + }, + "init": { + "end": 17, + "name": "hello", + "range": [ + 12, + 17, + ], + "start": 12, + "type": "Identifier", + }, "range": [ - 12, + 1, 17, ], - "start": 12, - "type": "Identifier", + "start": 1, + "type": "VariableDeclarator", }, - "range": [ - 1, - 17, - ], - "start": 1, - "type": "VariableDeclarator", - }, - ], - "end": 17, - "kind": "let", - "range": [ - 1, - 17, - ], - "start": 1, - "type": "VariableDeclaration", - }, - ], - "end": 17, - "range": [ - 1, - 17, - ], - "start": 1, - "type": "NGMicrosyntax", + ], + "end": 17, + "kind": "let", + "range": [ + 1, + 17, + ], + "start": 1, + "type": "VariableDeclaration", + }, + ], + "end": 17, + "range": [ + 1, + 17, + ], + "start": 1, + "type": "NGMicrosyntax", + }, + "text": " let hero = hello ", } `; @@ -1597,66 +1648,69 @@ Identifier { exports[` let hero of heroes 2`] = ` { - "body": [ - { - "end": 9, - "left": { - "declarations": [ - { - "end": 9, - "id": { + "ast": { + "body": [ + { + "end": 9, + "left": { + "declarations": [ + { "end": 9, - "name": "hero", + "id": { + "end": 9, + "name": "hero", + "range": [ + 5, + 9, + ], + "start": 5, + "type": "Identifier", + }, + "init": null, "range": [ - 5, + 1, 9, ], - "start": 5, - "type": "Identifier", + "start": 1, + "type": "VariableDeclarator", }, - "init": null, - "range": [ - 1, - 9, - ], - "start": 1, - "type": "VariableDeclarator", - }, - ], - "end": 9, - "kind": "let", + ], + "end": 9, + "kind": "let", + "range": [ + 1, + 9, + ], + "start": 1, + "type": "VariableDeclaration", + }, "range": [ 1, 9, ], + "right": { + "end": 19, + "name": "heroes", + "range": [ + 13, + 19, + ], + "start": 13, + "type": "Identifier", + }, "start": 1, - "type": "VariableDeclaration", + "type": "NGMicrosyntaxOfExpression", }, - "range": [ - 1, - 9, - ], - "right": { - "end": 19, - "name": "heroes", - "range": [ - 13, - 19, - ], - "start": 13, - "type": "Identifier", - }, - "start": 1, - "type": "NGMicrosyntaxOfExpression", - }, - ], - "end": 19, - "range": [ - 1, - 19, - ], - "start": 1, - "type": "NGMicrosyntax", + ], + "end": 19, + "range": [ + 1, + 19, + ], + "start": 1, + "type": "NGMicrosyntax", + }, + "text": " let hero of heroes ", } `; @@ -1747,123 +1801,126 @@ Identifier { exports[` let item of items; index as i; trackBy: trackByFn 2`] = ` { - "body": [ - { - "end": 9, - "left": { - "declarations": [ - { - "end": 9, - "id": { + "ast": { + "body": [ + { + "end": 9, + "left": { + "declarations": [ + { "end": 9, - "name": "item", + "id": { + "end": 9, + "name": "item", + "range": [ + 5, + 9, + ], + "start": 5, + "type": "Identifier", + }, + "init": null, "range": [ - 5, + 1, 9, ], - "start": 5, - "type": "Identifier", + "start": 1, + "type": "VariableDeclarator", }, - "init": null, - "range": [ - 1, - 9, - ], - "start": 1, - "type": "VariableDeclarator", - }, - ], - "end": 9, - "kind": "let", + ], + "end": 9, + "kind": "let", + "range": [ + 1, + 9, + ], + "start": 1, + "type": "VariableDeclaration", + }, "range": [ 1, 9, ], + "right": { + "end": 18, + "name": "items", + "range": [ + 13, + 18, + ], + "start": 13, + "type": "Identifier", + }, "start": 1, - "type": "VariableDeclaration", + "type": "NGMicrosyntaxOfExpression", }, - "range": [ - 1, - 9, - ], - "right": { - "end": 18, - "name": "items", - "range": [ - 13, - 18, - ], - "start": 13, - "type": "Identifier", - }, - "start": 1, - "type": "NGMicrosyntaxOfExpression", - }, - { - "alias": { + { + "alias": { + "end": 30, + "name": "i", + "range": [ + 29, + 30, + ], + "start": 29, + "type": "Identifier", + }, "end": 30, - "name": "i", - "range": [ - 29, - 30, - ], - "start": 29, - "type": "Identifier", - }, - "end": 30, - "expression": { - "end": 25, - "name": "index", + "expression": { + "end": 25, + "name": "index", + "range": [ + 20, + 25, + ], + "start": 20, + "type": "Identifier", + }, "range": [ 20, - 25, + 30, ], "start": 20, - "type": "Identifier", + "type": "NGMicrosyntaxAsExpression", }, - "range": [ - 20, - 30, - ], - "start": 20, - "type": "NGMicrosyntaxAsExpression", - }, - { - "end": 50, - "expression": { + { "end": 50, - "name": "trackByFn", - "range": [ - 41, - 50, - ], - "start": 41, - "type": "Identifier", - }, - "key": { - "end": 39, - "name": "TrackBy", + "expression": { + "end": 50, + "name": "trackByFn", + "range": [ + 41, + 50, + ], + "start": 41, + "type": "Identifier", + }, + "key": { + "end": 39, + "name": "TrackBy", + "range": [ + 32, + 39, + ], + "start": 32, + "type": "Identifier", + }, "range": [ 32, - 39, + 50, ], "start": 32, - "type": "Identifier", + "type": "NGMicrosyntaxKeyedExpression", }, - "range": [ - 32, - 50, - ], - "start": 32, - "type": "NGMicrosyntaxKeyedExpression", - }, - ], - "end": 50, - "range": [ - 1, - 50, - ], - "start": 1, - "type": "NGMicrosyntax", + ], + "end": 50, + "range": [ + 1, + 50, + ], + "start": 1, + "type": "NGMicrosyntax", + }, + "text": " let item of items; index as i; trackBy: trackByFn", } `; diff --git a/tests/transform-microsyntax.test.ts b/tests/transform-microsyntax.test.ts index d0a7ffe9..bd89d0d7 100644 --- a/tests/transform-microsyntax.test.ts +++ b/tests/transform-microsyntax.test.ts @@ -27,7 +27,7 @@ test.each` `('$input', ({ input, types }) => { const ast = parseTemplateBindings(input); expect(snapshotAst(ast, input)).toMatchSnapshot(); - expect(ast).toMatchSnapshot(); + expect({ text: input, ast }).toMatchSnapshot(); expect(ast.body.map((node) => node.type)).toEqual(types); });