Skip to content

Commit 5b95a59

Browse files
authored
refactor: major refactoring (#291)
1 parent 10251bc commit 5b95a59

22 files changed

+1036
-930
lines changed

.eslintrc.cjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = {
1111
ecmaVersion: 'latest',
1212
sourceType: 'module',
1313
},
14-
plugins: ['@typescript-eslint', 'import', 'unicorn'],
14+
plugins: ['simple-import-sort', '@typescript-eslint', 'import', 'unicorn'],
1515
env: { node: true },
1616
rules: {
1717
'@typescript-eslint/no-namespace': 'off',
@@ -23,6 +23,8 @@ module.exports = {
2323
],
2424
'no-constant-condition': ['error', { checkLoops: false }],
2525
'import/extensions': ['error', 'always'],
26+
'simple-import-sort/imports': 'error',
27+
'simple-import-sort/exports': 'error',
2628
'unicorn/prefer-at': 'error',
2729
},
2830
overrides: [

.vscode/extensions.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"recommendations": [
3+
"esbenp.prettier-vscode",
4+
"editorconfig.editorconfig",
5+
"dbaeumer.vscode-eslint",
6+
"streetsidesoftware.code-spell-checker"
7+
]
8+
}

.vscode/settings.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"[javascript][typescript]": {
3+
"editor.codeActionsOnSave": ["source.fixAll.eslint"]
4+
},
5+
"[javascript][typescript][markdown][json][jsonc][yaml]": {
6+
"editor.defaultFormatter": "esbenp.prettier-vscode",
7+
"editor.formatOnSave": true
8+
},
9+
"editor.codeActionsOnSave": {
10+
"source.fixAll.eslint": "explicit"
11+
},
12+
"prettier.requireConfig": true,
13+
// "eslint.experimental.useFlatConfig": true,
14+
"files.exclude": {
15+
"lib": true
16+
}
17+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"eslint-config-prettier": "9.1.0",
4141
"eslint-plugin-deprecation": "2.0.0",
4242
"eslint-plugin-import": "2.29.1",
43+
"eslint-plugin-simple-import-sort": "12.0.0",
4344
"eslint-plugin-unicorn": "51.0.1",
4445
"jest": "29.7.0",
4546
"jest-snapshot-serializer-raw": "1.2.0",

src/angular-parser.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import {
2+
type ASTWithSource,
3+
Lexer,
4+
Parser,
5+
type TemplateBindingParseResult,
6+
} from '@angular/compiler';
7+
8+
import { type CommentLine } from './types.js';
9+
import { sourceSpanToLocationInformation } from './utils.js';
10+
11+
const getCommentStart = (text: string): number | null =>
12+
// @ts-expect-error -- need to call private _commentStart
13+
Parser.prototype._commentStart(text);
14+
15+
function extractComments(text: string, shouldExtractComment: boolean) {
16+
const commentStart = shouldExtractComment ? getCommentStart(text) : null;
17+
18+
if (commentStart === null) {
19+
return { text, comments: [] };
20+
}
21+
22+
const comment: CommentLine = {
23+
type: 'CommentLine',
24+
value: text.slice(commentStart + '//'.length),
25+
...sourceSpanToLocationInformation({
26+
start: commentStart,
27+
end: text.length,
28+
}),
29+
};
30+
31+
return {
32+
text: text.slice(0, commentStart),
33+
comments: [comment],
34+
};
35+
}
36+
37+
function createAngularParseFunction<
38+
T extends ASTWithSource | TemplateBindingParseResult,
39+
>(parse: (text: string, parser: Parser) => T, shouldExtractComment = true) {
40+
return (originalText: string) => {
41+
const lexer = new Lexer();
42+
const parser = new Parser(lexer);
43+
44+
const { text, comments } = extractComments(
45+
originalText,
46+
shouldExtractComment,
47+
);
48+
const result = parse(text, parser);
49+
50+
if (result.errors.length !== 0) {
51+
const [{ message }] = result.errors;
52+
throw new SyntaxError(
53+
message.replace(/^Parser Error: | at column \d+ in [^]*$/g, ''),
54+
);
55+
}
56+
57+
return { result, comments, text };
58+
};
59+
}
60+
61+
export const parseBinding = createAngularParseFunction((text, parser) =>
62+
parser.parseBinding(text, '', 0),
63+
);
64+
65+
export const parseSimpleBinding = createAngularParseFunction((text, parser) =>
66+
parser.parseSimpleBinding(text, '', 0),
67+
);
68+
69+
export const parseAction = createAngularParseFunction((text, parser) =>
70+
parser.parseAction(text, '', 0),
71+
);
72+
73+
export const parseInterpolationExpression = createAngularParseFunction(
74+
(text, parser) => parser.parseInterpolationExpression(text, '', 0),
75+
);
76+
77+
export const parseTemplateBindings = createAngularParseFunction(
78+
(text, parser) => parser.parseTemplateBindings('', text, '', 0, 0),
79+
/* shouldExtractComment */ false,
80+
);
81+
82+
export type AstParseResult = ReturnType<typeof parseBinding>;
83+
export type MicroSyntaxParseResult = ReturnType<typeof parseTemplateBindings>;

src/context.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/estree-parser.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as angularParser from './angular-parser.js';
2+
import { transformAstResult, transformMicrosyntaxResult } from './transform.js';
3+
4+
export const parseBinding = (text: string) =>
5+
transformAstResult(angularParser.parseBinding(text));
6+
7+
export const parseSimpleBinding = (text: string) =>
8+
transformAstResult(angularParser.parseSimpleBinding(text));
9+
10+
export const parseInterpolationExpression = (text: string) =>
11+
transformAstResult(angularParser.parseInterpolationExpression(text));
12+
13+
export const parseAction = (text: string) =>
14+
transformAstResult(angularParser.parseAction(text));
15+
16+
export const parseTemplateBindings = (text: string) =>
17+
transformMicrosyntaxResult(angularParser.parseTemplateBindings(text));

src/index.ts

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,2 @@
1-
import type * as ng from '@angular/compiler';
2-
import transformComment from './transform-comment.js';
3-
import transformNode from './transform-node.js';
4-
import { transformTemplateBindings } from './transform-microsyntax.js';
5-
import type {
6-
NGNode,
7-
RawNGComment,
8-
NGMicrosyntax,
9-
ParseResult,
10-
} from './types.js';
11-
import * as angularParser from './parser.js';
12-
import { type Context } from './context.js';
13-
14-
function createParser(
15-
parse: (text: string) => {
16-
ast: ng.AST;
17-
comments: RawNGComment[];
18-
context: Context;
19-
},
20-
) {
21-
return (text: string) => {
22-
const { ast: angularNode, comments, context } = parse(text);
23-
const ast = transformNode(angularNode, context) as ParseResult;
24-
ast.comments = comments.map((comment) => transformComment(comment));
25-
return ast;
26-
};
27-
}
28-
29-
export const parseBinding = createParser(angularParser.parseBinding);
30-
export const parseSimpleBinding = createParser(
31-
angularParser.parseSimpleBinding,
32-
);
33-
export const parseInterpolationExpression = createParser(
34-
angularParser.parseInterpolationExpression,
35-
);
36-
export const parseAction = createParser(angularParser.parseAction);
37-
export const parseTemplateBindings = (text: string): NGMicrosyntax =>
38-
transformTemplateBindings(angularParser.parseTemplateBindings(text));
39-
export type { NGMicrosyntax, NGNode };
1+
export * from './estree-parser.js';
2+
export type { NGMicrosyntax, NGNode } from './types.ts';

src/parser.ts

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)