Skip to content

Commit 069199b

Browse files
authored
refactor!: remove loc from node, added range (#271)
1 parent 67360bd commit 069199b

File tree

7 files changed

+40
-54
lines changed

7 files changed

+40
-54
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
"release": "yarn build && standard-version"
2828
},
2929
"dependencies": {
30-
"lines-and-columns": "^2.0.4",
3130
"tslib": "^2.6.2"
3231
},
3332
"devDependencies": {
@@ -47,6 +46,7 @@
4746
"eslint-plugin-unicorn": "49.0.0",
4847
"jest": "29.7.0",
4948
"jest-snapshot-serializer-raw": "1.2.0",
49+
"lines-and-columns": "2.0.4",
5050
"npm-run-all": "4.1.5",
5151
"prettier": "3.0.3",
5252
"standard-version": "9.5.0",

src/context.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
import { LinesAndColumns } from 'lines-and-columns';
21
import { getCharacterIndex, getCharacterLastIndex } from './utils.js';
32

43
export class Context {
54
text;
6-
#linesAndColumns!: LinesAndColumns;
75

86
constructor(text: string) {
97
this.text = text;
108
}
119

12-
locationForIndex(index: number) {
13-
this.#linesAndColumns ??= new LinesAndColumns(this.text);
14-
const { line, column } = this.#linesAndColumns.locationForIndex(index)!;
15-
return { line: line + 1, column, index };
16-
}
17-
1810
getCharacterIndex(pattern: RegExp | string, index: number) {
1911
return getCharacterIndex(this.text, pattern, index);
2012
}

src/transform-microsyntax.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export function transformTemplateBindings(
196196
type: t,
197197
...transformSpan(span, context, stripSpaces),
198198
...n,
199-
} as T & RawNGSpan;
199+
} as T & { start: number; end: number; range: [number, number] };
200200
}
201201

202202
function removePrefix(string: string) {

src/transform.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ export const transform = (
404404
type: t,
405405
...transformSpan(span, context, processSpan, hasParentParens),
406406
...n,
407-
} as T & RawNGSpan;
407+
} as T & { start: number; end: number; range: [number, number] };
408408
switch (t) {
409409
case 'NumericLiteral': {
410410
const numericLiteral = newNode as unknown as b.NumericLiteral;
@@ -503,36 +503,26 @@ export function transformSpan(
503503
): {
504504
start: NonNullable<b.Node['start']>;
505505
end: NonNullable<b.Node['end']>;
506-
loc: NonNullable<b.Node['loc']>;
506+
range: NonNullable<b.Node['range']>;
507507
} {
508508
if (!processSpan) {
509509
const { start, end } = span;
510510
return {
511511
start,
512512
end,
513-
loc: {
514-
filename: '',
515-
identifierName: '',
516-
start: context.locationForIndex(start),
517-
end: context.locationForIndex(end),
518-
},
513+
range: [start, end],
519514
};
520515
}
521516

522-
const { outerSpan, innerSpan, hasParens } = fitSpans(
523-
span,
524-
context.text,
525-
hasParentParens,
526-
);
517+
const {
518+
outerSpan,
519+
innerSpan: { start, end },
520+
hasParens,
521+
} = fitSpans(span, context.text, hasParentParens);
527522
return {
528-
start: innerSpan.start,
529-
end: innerSpan.end,
530-
loc: {
531-
filename: '',
532-
identifierName: '',
533-
start: context.locationForIndex(innerSpan.start),
534-
end: context.locationForIndex(innerSpan.end),
535-
},
523+
start,
524+
end,
525+
range: [start, end],
536526
...(hasParens && {
537527
extra: {
538528
parenthesized: true,

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export interface NGBaseNode {
44
type: string;
55
start: number;
66
end: number;
7-
loc: b.SourceLocation;
7+
range: [number, number];
88
}
99

1010
export type NGNode = { comments?: b.CommentLine[] } & (

tests/helpers.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
import { codeFrameColumns } from '@babel/code-frame';
22
import * as babelParser from '@babel/parser';
3+
import { LinesAndColumns } from 'lines-and-columns';
34
import { wrap } from 'jest-snapshot-serializer-raw';
45

56
const babelParserOptions: babelParser.ParserOptions = {
67
plugins: [
78
'typescript', // NonNullAssert
89
],
10+
ranges: true,
911
};
1012

1113
export function parseBabelExpression(input: string) {
1214
return babelParser.parseExpression(input, babelParserOptions);
1315
}
1416

1517
export function parseBabel(input: string) {
16-
return babelParser.parse(input, babelParserOptions);
18+
const ast = babelParser.parse(input, babelParserOptions);
19+
// https://github.com/babel/babel/issues/15115
20+
for (const comment of ast.comments!) {
21+
// @ts-expect-error -- missing types
22+
comment.range ??= [comment.start, comment.end];
23+
}
24+
return ast;
1725
}
1826

1927
export function massageAst(ast: any): any {
@@ -35,18 +43,13 @@ export function massageAst(ast: any): any {
3543
delete ast.method;
3644
}
3745

46+
delete ast.loc;
47+
3848
const massaged = Object.keys(ast).reduce((reduced: any, key) => {
3949
switch (key) {
4050
case 'trailingComments':
4151
// do nothing
4252
break;
43-
case 'loc': {
44-
const loc = massageAst(ast[key]);
45-
delete loc.filename;
46-
delete loc.identifierName;
47-
reduced[key] = loc;
48-
break;
49-
}
5053
case 'extra': {
5154
const extra = massageAst(ast[key]);
5255
if (extra) {
@@ -75,12 +78,13 @@ export function massageAst(ast: any): any {
7578
export function snapshotAst(ast: any, source: string) {
7679
const snapshots: string[] = [];
7780
const isNode = (x: any) => x && x.type;
81+
const linesAndColumns = new LinesAndColumns(source);
7882
visitAst(ast, (node) => {
7983
const props = Object.keys(node).reduce((reduced: any, key) => {
8084
const value = node[key];
8185
switch (key) {
8286
case 'type':
83-
case 'loc':
87+
case 'range':
8488
case 'start':
8589
case 'end':
8690
break;
@@ -96,13 +100,13 @@ export function snapshotAst(ast: any, source: string) {
96100
return reduced;
97101
}, {});
98102
const fixColumn = (p: { line: number; column: number }) => ({
99-
line: p.line,
103+
line: p.line + 1,
100104
column: p.column + 1,
101105
});
102-
const codeFrame = codeFrameColumns(source, {
103-
start: fixColumn(node.loc.start),
104-
end: fixColumn(node.loc.end),
105-
});
106+
const [start, end] = [node.start, node.end].map((index) =>
107+
fixColumn(linesAndColumns.locationForIndex(index)!),
108+
);
109+
const codeFrame = codeFrameColumns(source, { start, end });
106110
const propsString = JSON.stringify(props, undefined, 2);
107111
snapshots.push(`${node.type} ${propsString}\n${codeFrame}`);
108112
});

yarn.lock

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,7 @@ __metadata:
12961296
eslint-plugin-unicorn: "npm:49.0.0"
12971297
jest: "npm:29.7.0"
12981298
jest-snapshot-serializer-raw: "npm:1.2.0"
1299-
lines-and-columns: "npm:^2.0.4"
1299+
lines-and-columns: "npm:2.0.4"
13001300
npm-run-all: "npm:4.1.5"
13011301
prettier: "npm:3.0.3"
13021302
standard-version: "npm:9.5.0"
@@ -4476,20 +4476,20 @@ __metadata:
44764476
languageName: node
44774477
linkType: hard
44784478

4479+
"lines-and-columns@npm:2.0.4":
4480+
version: 2.0.4
4481+
resolution: "lines-and-columns@npm:2.0.4"
4482+
checksum: 81ac2f943f5428a46bd4ea2561c74ba674a107d8e6cc70cd317d16892a36ff3ba0dc6e599aca8b6f8668d26c85288394c6edf7a40e985ca843acab3701b80d4c
4483+
languageName: node
4484+
linkType: hard
4485+
44794486
"lines-and-columns@npm:^1.1.6":
44804487
version: 1.2.4
44814488
resolution: "lines-and-columns@npm:1.2.4"
44824489
checksum: 0c37f9f7fa212b38912b7145e1cd16a5f3cd34d782441c3e6ca653485d326f58b3caccda66efce1c5812bde4961bbde3374fae4b0d11bf1226152337f3894aa5
44834490
languageName: node
44844491
linkType: hard
44854492

4486-
"lines-and-columns@npm:^2.0.4":
4487-
version: 2.0.4
4488-
resolution: "lines-and-columns@npm:2.0.4"
4489-
checksum: 81ac2f943f5428a46bd4ea2561c74ba674a107d8e6cc70cd317d16892a36ff3ba0dc6e599aca8b6f8668d26c85288394c6edf7a40e985ca843acab3701b80d4c
4490-
languageName: node
4491-
linkType: hard
4492-
44934493
"load-json-file@npm:^4.0.0":
44944494
version: 4.0.0
44954495
resolution: "load-json-file@npm:4.0.0"

0 commit comments

Comments
 (0)