Skip to content

Commit

Permalink
Fix shorthand detection
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 24, 2022
1 parent cd12cd0 commit 225f34e
Show file tree
Hide file tree
Showing 2 changed files with 15 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
11 changes: 11 additions & 0 deletions tests/transform-microsyntax.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
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 +26,12 @@ test.each`
expect(snapshotAst(ast, input)).toMatchSnapshot();
expect(ast.body.map((node) => node.type)).toEqual(types);
});

test('Shorthand', () => {
const ast = parseTemplateBindings('someTmpl; context: {app}');
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);
});

0 comments on commit 225f34e

Please sign in to comment.