Skip to content

Commit 20ed95f

Browse files
committed
Simplify logic
1 parent e510581 commit 20ed95f

18 files changed

Lines changed: 101 additions & 104 deletions

src/angular-parser.ts

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,32 @@ import {
1111
import { type CommentLine } from './types.ts';
1212
import { sourceSpanToLocationInformation } from './utils.ts';
1313

14+
let parseSourceSpan: ParseSourceSpan;
1415
// https://github.com/angular/angular/blob/5e9707dc84e6590ec8c9d41e7d3be7deb2fa7c53/packages/compiler/test/expression_parser/utils/span.ts
15-
function getFakeSpan(fileName = 'test.html') {
16-
const file = new ParseSourceFile('', fileName);
17-
const location = new ParseLocation(file, 0, 0, 0);
18-
return new ParseSourceSpan(location, location);
16+
function getParseSourceSpan() {
17+
if (!parseSourceSpan) {
18+
const file = new ParseSourceFile('', 'test.html');
19+
const location = new ParseLocation(file, -1, -1, -1);
20+
parseSourceSpan = new ParseSourceSpan(location, location);
21+
}
22+
23+
return parseSourceSpan;
24+
}
25+
26+
let parser: Parser;
27+
function getParser() {
28+
return (parser ??= new Parser(new Lexer()));
1929
}
2030

2131
const getCommentStart = (text: string): number | null =>
2232
// @ts-expect-error -- need to call private _commentStart
2333
Parser.prototype._commentStart(text);
2434

25-
function extractComments(text: string, shouldExtractComment: boolean) {
26-
const commentStart = shouldExtractComment ? getCommentStart(text) : null;
35+
function extractComments(text: string) {
36+
const commentStart = getCommentStart(text);
2737

2838
if (commentStart === null) {
29-
return { text, comments: [] };
39+
return [];
3040
}
3141

3242
const comment: CommentLine = {
@@ -38,55 +48,48 @@ function extractComments(text: string, shouldExtractComment: boolean) {
3848
}),
3949
};
4050

41-
return { text, comments: [comment] };
51+
return [comment];
4252
}
4353

44-
function createAngularParseFunction<
45-
T extends ASTWithSource | TemplateBindingParseResult,
46-
>(parse: (text: string, parser: Parser) => T, shouldExtractComment = true) {
47-
return (originalText: string) => {
48-
const lexer = new Lexer();
49-
const parser = new Parser(lexer);
50-
51-
const { text, comments } = extractComments(
52-
originalText,
53-
shouldExtractComment,
54+
function throwErrors<
55+
ResultType extends ASTWithSource | TemplateBindingParseResult,
56+
>(result: ResultType) {
57+
if (result.errors.length !== 0) {
58+
const [{ message }] = result.errors;
59+
throw new SyntaxError(
60+
message.replace(/^Parser Error: | at column \d+ in [^]*$/g, ''),
5461
);
55-
const result = parse(text, parser);
56-
57-
if (result.errors.length !== 0) {
58-
const [{ message }] = result.errors;
59-
throw new SyntaxError(
60-
message.replace(/^Parser Error: | at column \d+ in [^]*$/g, ''),
61-
);
62-
}
63-
64-
console.log(result);
62+
}
6563

66-
return { result, comments, text };
67-
};
64+
return result;
6865
}
6966

70-
export const parseBinding = createAngularParseFunction((text, parser) =>
71-
parser.parseBinding(text, getFakeSpan(), 0),
67+
const createAstParser =
68+
(
69+
name:
70+
| 'parseBinding'
71+
| 'parseSimpleBinding'
72+
| 'parseAction'
73+
| 'parseInterpolationExpression',
74+
) =>
75+
(text: string) => ({
76+
result: throwErrors<ASTWithSource>(
77+
getParser()[name](text, getParseSourceSpan(), 0),
78+
),
79+
text,
80+
comments: extractComments(text),
81+
});
82+
83+
export const parseAction = createAstParser('parseAction');
84+
export const parseBinding = createAstParser('parseBinding');
85+
export const parseSimpleBinding = createAstParser('parseSimpleBinding');
86+
export const parseInterpolationExpression = createAstParser(
87+
'parseInterpolationExpression',
7288
);
73-
74-
export const parseSimpleBinding = createAngularParseFunction((text, parser) =>
75-
parser.parseSimpleBinding(text, getFakeSpan(), 0),
76-
);
77-
78-
export const parseAction = createAngularParseFunction((text, parser) =>
79-
parser.parseAction(text, getFakeSpan(), 0),
80-
);
81-
82-
export const parseInterpolationExpression = createAngularParseFunction(
83-
(text, parser) => parser.parseInterpolationExpression(text, getFakeSpan(), 0),
84-
);
85-
86-
export const parseTemplateBindings = createAngularParseFunction(
87-
(text, parser) => parser.parseTemplateBindings('', text, getFakeSpan(), 0, 0),
88-
/* shouldExtractComment */ false,
89-
);
90-
91-
export type AstParseResult = ReturnType<typeof parseBinding>;
92-
export type MicroSyntaxParseResult = ReturnType<typeof parseTemplateBindings>;
89+
export const parseTemplateBindings = (text: string) => ({
90+
result: throwErrors<TemplateBindingParseResult>(
91+
getParser().parseTemplateBindings('', text, getParseSourceSpan(), 0, 0),
92+
),
93+
text,
94+
comments: [],
95+
});

src/ast-transform/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { transform, transformAst } from './transform.ts';
1+
export { transformAst, transformAstNode } from './transform.ts';

src/ast-transform/transform-array-expression.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type LiteralArray } from '@angular/compiler';
22
import type * as babel from '@babel/types';
33

4-
import { type Transformer } from '../transform.ts';
4+
import { type Transformer } from './transform.ts';
55

66
export const visitLiteralArray = (
77
node: LiteralArray,

src/ast-transform/transform-binary-expression.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Binary } from '@angular/compiler';
22
import type * as babel from '@babel/types';
33

4-
import { type Transformer } from '../transform.ts';
4+
import { type Transformer } from './transform.ts';
55

66
const isAssignmentOperator = (
77
operator: Binary['operation'],

src/ast-transform/transform-call-expression.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type Call, type SafeCall } from '@angular/compiler';
22
import type * as babel from '@babel/types';
33

4-
import { type Transformer } from '../transform.ts';
4+
import { type Transformer } from './transform.ts';
55
import { isOptionalObjectOrCallee } from './utilities.ts';
66

77
const callOptions = { optional: false } as const;

src/ast-transform/transform-chained-expression.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type Chain } from '@angular/compiler';
22
import type * as babel from '@babel/types';
33

4-
import { type Transformer } from '../transform.ts';
4+
import { type Transformer } from './transform.ts';
55
import type { NGChainedExpression } from '../types.ts';
66

77
export const visitChain = (

src/ast-transform/transform-conditional-expression.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type Conditional } from '@angular/compiler';
22
import type * as babel from '@babel/types';
33

4-
import { type Transformer } from '../transform.ts';
4+
import { type Transformer } from './transform.ts';
55

66
export const visitConditional = (
77
node: Conditional,

src/ast-transform/transform-interpolation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type Interpolation } from '@angular/compiler';
22

3-
import { type Transformer } from '../transform.ts';
3+
import { type Transformer } from './transform.ts';
44

55
export const visitInterpolation = (
66
node: Interpolation,

src/ast-transform/transform-member-expression.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from '@angular/compiler';
88
import type * as babel from '@babel/types';
99

10-
import { type Transformer } from '../transform.ts';
10+
import { type Transformer } from './transform.ts';
1111
import { isOptionalObjectOrCallee } from './utilities.ts';
1212

1313
const keyedReadOptions = { computed: true, optional: false } as const;

src/ast-transform/transform-non-null-expression.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type NonNullAssert } from '@angular/compiler';
22
import type * as babel from '@babel/types';
33

4-
import { type Transformer } from '../transform.ts';
4+
import { type Transformer } from './transform.ts';
55

66
export const visitNonNullAssert = (
77
node: NonNullAssert,

0 commit comments

Comments
 (0)