Skip to content

Commit 5622c7c

Browse files
committed
Minor refactoring
1 parent 78aae0d commit 5622c7c

File tree

6 files changed

+52
-43
lines changed

6 files changed

+52
-43
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type * as angular from '@angular/compiler';
22
import type * as babel from '@babel/types';
33

4-
import type { NGNode, RawNGSpan } from '../types.ts';
4+
import { type RawLocationInformation } from '../source.ts';
5+
import type { NGNode, Range } from '../types.ts';
56
import { type Transformer } from './transform.ts';
67

78
export const visitLiteralMap = (
@@ -11,18 +12,15 @@ export const visitLiteralMap = (
1112
const { keys, values } = node;
1213
const createChild = <T extends NGNode>(
1314
properties: Partial<T> & { type: T['type'] },
14-
location: angular.AST | RawNGSpan | [number, number] = node,
15+
location: RawLocationInformation = node,
1516
) =>
1617
transformer.create(properties, location, [node, ...transformer.ancestors]);
1718

1819
return {
1920
type: 'ObjectExpression',
2021
properties: keys.map((keyNode, index) => {
2122
const valueNode = values[index];
22-
const range: [number, number] = [
23-
keyNode.sourceSpan.start,
24-
valueNode.sourceSpan.end,
25-
];
23+
const range: Range = [keyNode.sourceSpan.start, valueNode.sourceSpan.end];
2624

2725
if (keyNode.kind === 'spread') {
2826
return createChild<babel.SpreadElement>(

src/ast-transform/transform.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {
55
ParenthesizedExpression,
66
} from '@angular/compiler';
77

8-
import { Source } from '../source.ts';
9-
import type { NGEmptyExpression, NGNode, RawNGSpan } from '../types.ts';
8+
import { type RawLocationInformation, Source } from '../source.ts';
9+
import type { NGEmptyExpression, NGNode } from '../types.ts';
1010
import { transformVisitor } from './visitor.ts';
1111

1212
class NodeTransformer extends Source {
@@ -29,7 +29,7 @@ class NodeTransformer extends Source {
2929

3030
create<T extends NGNode>(
3131
properties: Partial<T> & { type: T['type'] },
32-
location: AST | RawNGSpan | [number, number],
32+
location: RawLocationInformation,
3333
ancestors: AST[],
3434
) {
3535
const node = super.createNode(properties, location);
@@ -46,7 +46,7 @@ class NodeTransformer extends Source {
4646

4747
createNode<T extends NGNode>(
4848
properties: Partial<T> & { type: T['type'] },
49-
location: AST | RawNGSpan | [number, number] = this.node,
49+
location: RawLocationInformation = this.node,
5050
ancestorsToCreate: AST[] = this.ancestors,
5151
) {
5252
return this.create(properties, location, ancestorsToCreate);

src/source.ts

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import * as angular from '@angular/compiler';
1+
import { type AST } from '@angular/compiler';
22
import type * as babel from '@babel/types';
33

4-
import type { LocationInformation, NGNode, RawNGSpan } from './types.ts';
4+
import type { LocationInformation, NGNode, Range, StartEnd } from './types.ts';
55
import { getCharacterIndex, sourceSpanToLocationInformation } from './utils.ts';
66

7+
export type RawLocationInformation = AST | StartEnd | Range;
8+
79
export class Source {
810
text;
911

@@ -15,38 +17,51 @@ export class Source {
1517
return getCharacterIndex(this.text, pattern, index);
1618
}
1719

18-
transformSpan(span: RawNGSpan): LocationInformation {
20+
transformSpan(span: StartEnd): LocationInformation {
1921
return sourceSpanToLocationInformation(span);
2022
}
2123

2224
createNode<T extends NGNode>(
2325
properties: Partial<T> & { type: T['type'] },
24-
location: angular.AST | RawNGSpan | [number, number],
26+
location?: RawLocationInformation,
2527
) {
26-
let start: number;
27-
let end: number;
28-
let range: [number, number];
29-
if (Array.isArray(location)) {
30-
range = location;
31-
[start, end] = location;
32-
} else {
33-
({ start, end } =
34-
location instanceof angular.AST ? location.sourceSpan : location);
28+
let start: number | undefined | null = properties.start;
29+
let end: number | undefined | null = properties.end;
30+
let range: Range | undefined = properties.range;
31+
32+
if (location) {
33+
if (Array.isArray(location)) {
34+
[start, end] = location;
35+
range = location;
36+
} else {
37+
({ start, end } = (location as AST).sourceSpan ?? location);
38+
range = [start, end];
39+
}
40+
}
41+
42+
if (range) {
43+
[start, end] = range;
44+
} else if (typeof start === 'number' && typeof end === 'number') {
3545
range = [start, end];
3646
}
3747

48+
/* c8 ignore next 3 @preserve */
49+
if (!(typeof start === 'number' && typeof end === 'number' && range)) {
50+
throw new Error('Missing location information');
51+
}
52+
3853
const node = {
54+
...properties,
3955
start,
4056
end,
4157
range,
42-
...properties,
4358
} as T & LocationInformation;
4459

4560
switch (node.type) {
4661
case 'NumericLiteral':
4762
case 'StringLiteral':
4863
case 'RegExpLiteral': {
49-
const raw = this.text.slice(node.start, node.end);
64+
const raw = this.text.slice(start, end);
5065
const { value } = node as unknown as
5166
| babel.NumericLiteral
5267
| babel.StringLiteral;

src/transform-template-binding.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
} from '@angular/compiler';
66

77
import { transformAstNode } from './ast-transform/index.ts';
8-
import { Source } from './source.ts';
8+
import { type RawLocationInformation, Source } from './source.ts';
99
import type {
1010
NGMicrosyntax,
1111
NGMicrosyntaxAs,
@@ -15,7 +15,7 @@ import type {
1515
NGMicrosyntaxLet,
1616
NGMicrosyntaxNode,
1717
NGNode,
18-
RawNGSpan,
18+
StartEnd,
1919
} from './types.ts';
2020
import { lowercaseFirst } from './utils.ts';
2121

@@ -56,7 +56,7 @@ class TemplateBindingTransformer extends Source {
5656

5757
#create<T extends NGNode>(
5858
properties: Partial<T> & { type: T['type'] },
59-
location: angular.AST | RawNGSpan | [number, number],
59+
location: RawLocationInformation,
6060
) {
6161
return super.createNode<T>(properties, location);
6262
}
@@ -73,7 +73,7 @@ class TemplateBindingTransformer extends Source {
7373
* - "a" (start=0 end=1) -> (start=0 end=3)
7474
* - '\'' (start=0 end=1) -> (start=0 end=4)
7575
*/
76-
#fixSpan(span: RawNGSpan) {
76+
#fixSpan(span: StartEnd) {
7777
const text = this.#text;
7878
if (text[span.start] !== '"' && text[span.start] !== "'") {
7979
return;

src/types.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import type * as babel from '@babel/types';
22

3-
export interface LocationInformation {
4-
start: number;
5-
end: number;
6-
range: [number, number];
3+
export type Range = [number, number];
4+
export type StartEnd = { start: number; end: number };
5+
6+
export type LocationInformation = StartEnd & {
7+
range: Range;
78
extra?: any;
8-
}
9+
};
910

10-
export interface NGBaseNode extends LocationInformation {
11+
export type NGBaseNode = LocationInformation & {
1112
type: string;
12-
}
13+
};
1314

1415
export type NGOwnNode =
1516
| NGMicrosyntaxNode
@@ -37,11 +38,6 @@ export interface NGChainedExpression extends NGBaseNode {
3738
expressions: NGNode[];
3839
}
3940

40-
export interface RawNGSpan {
41-
start: number;
42-
end: number;
43-
}
44-
4541
/**
4642
* Microsyntax ::
4743
* ( ( Exp | Let ) ( Sep? ( Let | As | KeyExp ) )* Sep? Key? )?

src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { LocationInformation, RawNGSpan } from './types.ts';
1+
import type { LocationInformation, StartEnd } from './types.ts';
22

33
function getCharacterSearchTestFunction(pattern: RegExp | string) {
44
if (typeof pattern === 'string') {
@@ -36,7 +36,7 @@ export function lowercaseFirst(str: string) {
3636
}
3737

3838
export function sourceSpanToLocationInformation(
39-
span: RawNGSpan,
39+
span: StartEnd,
4040
): LocationInformation {
4141
const { start, end } = span;
4242
return {

0 commit comments

Comments
 (0)