Skip to content

Commit

Permalink
fix: detect shorthand correctly (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jun 11, 2022
1 parent cd12cd0 commit 66ace4e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,15 @@ export const transform = (
const tKey = quoted
? _c<b.StringLiteral>('StringLiteral', { value: key }, keySpan)
: _c<b.Identifier>('Identifier', { name: key }, keySpan);
const shorthand = tKey.end < tKey.start;

return _c<b.ObjectProperty>(
'ObjectProperty',
{
key: tKey,
value: tValue,
method: false,
shorthand: false,
shorthand,
computed: false,
},
{ start: _getOuterStart(tKey), end: _getOuterEnd(tValue) },
Expand Down Expand Up @@ -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<b.Expression>(receiver);
Expand Down
21 changes: 21 additions & 0 deletions tests/transform-microsyntax.test.ts
Original file line number Diff line number Diff line change
@@ -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`
Expand All @@ -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);
}
});

0 comments on commit 66ace4e

Please sign in to comment.