diff --git a/src/transform.ts b/src/transform.ts index 7140797c..bd610079 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -239,13 +239,15 @@ export const transform = ( const tKey = quoted ? _c('StringLiteral', { value: key }, keySpan) : _c('Identifier', { name: key }, keySpan); + const shorthand = tKey.end < tKey.start; + return _c( 'ObjectProperty', { key: tKey, value: tValue, method: false, - shorthand: false, + shorthand, computed: false, }, { start: _getOuterStart(tKey), end: _getOuterEnd(tValue) }, @@ -513,7 +515,7 @@ export const transform = ( props: { computed: boolean; optional: boolean }, { end = _getOuterEnd(tName), hasParentParens = false } = {}, ) { - if (_isImplicitThis(receiver)) { + if (_isImplicitThis(receiver) || receiver.span.start === tName.start) { return tName; } const tReceiver = _t(receiver); diff --git a/tests/transform-microsyntax.test.ts b/tests/transform-microsyntax.test.ts index b1b9619d..854741e4 100644 --- a/tests/transform-microsyntax.test.ts +++ b/tests/transform-microsyntax.test.ts @@ -1,4 +1,7 @@ +import { VERSION as angularVersion } from '@angular/compiler'; +import * as b from '@babel/types'; import { parseTemplateBindings } from '../src/index'; +import type { NGMicrosyntaxKeyedExpression } from '../src/types'; import { snapshotAst } from './helpers'; test.each` @@ -24,3 +27,21 @@ test.each` expect(snapshotAst(ast, input)).toMatchSnapshot(); expect(ast.body.map((node) => node.type)).toEqual(types); }); + +test('Shorthand', () => { + const major = Number(angularVersion.major); + const minor = Number(angularVersion.minor); + const code = 'someTmpl; context: {app}'; + if (major > 12 || (major === 12 && minor > 0)) { + 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); + } else { + expect(() => { + parseTemplateBindings(code); + }).toThrow(SyntaxError); + } +});