Skip to content

Commit

Permalink
feat: add parseInterpolationExpression, deprecate `parseInterpolati…
Browse files Browse the repository at this point in the history
…on` (#241)
  • Loading branch information
fisker committed Jan 30, 2023
1 parent 96d2da7 commit 817b64a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const ast = ngEstreeParser.parseBinding('a | b:c');

- `parseAction(input: string): AST` for `(target)="input"`
- `parseBinding(input: string): AST` for `[target]="input"`
- `parseInterpolation(input: string): AST` for `{{input}}`
- `parseInterpolationExpression(input: string): AST` for `{{input}}`
- `parseTemplateBindings(input: string): AST` for `*directive="input"`

## Development
Expand Down
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { NGMicrosyntax, NGNode, RawNGComment } from './types.js';
import {
parseNgAction,
parseNgBinding,
parseNgInterpolation,
parseNgInterpolationExpression,
parseNgSimpleBinding,
parseNgTemplateBindings,
} from './utils.js';
Expand All @@ -34,8 +34,8 @@ export function parseSimpleBinding(input: string): NGNode {
return parse(input, parseNgSimpleBinding);
}

export function parseInterpolation(input: string): NGNode {
return parse(input, parseNgInterpolation);
export function parseInterpolationExpression(input: string): NGNode {
return parse(input, parseNgInterpolationExpression);
}

export function parseAction(input: string): NGNode {
Expand All @@ -48,3 +48,6 @@ export function parseTemplateBindings(input: string): NGMicrosyntax {
new Context(input),
);
}

// TODO: Remove this in next major
export const parseInterpolation = parseInterpolationExpression;
28 changes: 11 additions & 17 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ export function parseNgAction(input: string) {
);
}

export function parseNgInterpolationExpression(input: string) {
return parseNg(input, (astInput, ngParser) => {
const result = ngParser.parseInterpolationExpression(
astInput,
...NG_PARSE_SHARED_PARAMS,
);
result.ast = (result.ast as ng.Interpolation).expressions[0];
return result;
});
}

export function parseNgTemplateBindings(input: string) {
const ngParser = createNgParser();
const { templateBindings: ast, errors } = ngParser.parseTemplateBindings(
Expand All @@ -56,23 +67,6 @@ export function parseNgTemplateBindings(input: string) {
return ast;
}

export function parseNgInterpolation(input: string) {
const ngParser = createNgParser();
const { astInput, comments } = extractComments(input, ngParser);
const prefix = '{{';
const suffix = '}}';

const { ast: rawAst, errors } = ngParser.parseInterpolation(
prefix + astInput + suffix,
NG_PARSE_FAKE_LOCATION,
-prefix.length,
null,
)!;
assertAstErrors(errors);
const ast = (rawAst as ng.Interpolation).expressions[0];
return { ast, comments };
}

function assertAstErrors(errors: ng.ParserError[]) {
if (errors.length !== 0) {
const [{ message }] = errors;
Expand Down
12 changes: 8 additions & 4 deletions tests/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import type * as b from '@babel/types';
import {
parseAction,
parseBinding,
parseInterpolation,
parseInterpolationExpression,
parseSimpleBinding,
} from '../src/index.js';
import type { NGNode } from '../src/types.js';
import {
getNgType,
parseNgAction,
parseNgBinding,
parseNgInterpolation,
parseNgInterpolationExpression,
parseNgSimpleBinding,
} from '../src/utils.js';
import {
Expand All @@ -32,7 +32,7 @@ describe.each`
${'BindingPipe'} | ${'NGPipeExpression'} | ${' a | b : c '} | ${false} | ${true} | ${false} | ${true}
${'Chain'} | ${'NGChainedExpression'} | ${' a ; b '} | ${true} | ${false} | ${false} | ${false}
${'Conditional'} | ${'ConditionalExpression'} | ${' a ? 1 : 2 '} | ${true} | ${true} | ${true} | ${true}
${'EmptyExpr'} | ${'NGEmptyExpression'} | ${''} | ${true} | ${true} | ${true} | ${false}
${'EmptyExpr'} | ${'NGEmptyExpression'} | ${''} | ${true} | ${true} | ${true} | ${true}
${'Call'} | ${'CallExpression'} | ${' ( a . b ) ( 1 , 2 ) '} | ${true} | ${true} | ${true} | ${true}
${'SafeCall'} | ${'OptionalCallExpression'} | ${' ( a . b )?.( 1 , 2 ) '} | ${true} | ${true} | ${true} | ${true}
${'Call'} | ${'CallExpression'} | ${' ( a ) ( 1 , 2 ) '} | ${true} | ${true} | ${true} | ${true}
Expand Down Expand Up @@ -111,7 +111,11 @@ describe.each`
testSection('action', parseNgAction, parseAction);
testSection('binding', parseNgBinding, parseBinding);
testSection('simple', parseNgSimpleBinding, parseSimpleBinding);
testSection('interpolation', parseNgInterpolation, parseInterpolation);
testSection(
'interpolation',
parseNgInterpolationExpression,
parseInterpolationExpression,
);

test('ast', () => {
expect(beforeNode).not.toEqual(null);
Expand Down

0 comments on commit 817b64a

Please sign in to comment.