Skip to content

Commit

Permalink
refactor!: remove loc from node, added range (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Nov 14, 2023
1 parent 67360bd commit 069199b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 54 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"release": "yarn build && standard-version"
},
"dependencies": {
"lines-and-columns": "^2.0.4",
"tslib": "^2.6.2"
},
"devDependencies": {
Expand All @@ -47,6 +46,7 @@
"eslint-plugin-unicorn": "49.0.0",
"jest": "29.7.0",
"jest-snapshot-serializer-raw": "1.2.0",
"lines-and-columns": "2.0.4",
"npm-run-all": "4.1.5",
"prettier": "3.0.3",
"standard-version": "9.5.0",
Expand Down
8 changes: 0 additions & 8 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import { LinesAndColumns } from 'lines-and-columns';
import { getCharacterIndex, getCharacterLastIndex } from './utils.js';

export class Context {
text;
#linesAndColumns!: LinesAndColumns;

constructor(text: string) {
this.text = text;
}

locationForIndex(index: number) {
this.#linesAndColumns ??= new LinesAndColumns(this.text);
const { line, column } = this.#linesAndColumns.locationForIndex(index)!;
return { line: line + 1, column, index };
}

getCharacterIndex(pattern: RegExp | string, index: number) {
return getCharacterIndex(this.text, pattern, index);
}
Expand Down
2 changes: 1 addition & 1 deletion src/transform-microsyntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export function transformTemplateBindings(
type: t,
...transformSpan(span, context, stripSpaces),
...n,
} as T & RawNGSpan;
} as T & { start: number; end: number; range: [number, number] };
}

function removePrefix(string: string) {
Expand Down
32 changes: 11 additions & 21 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ export const transform = (
type: t,
...transformSpan(span, context, processSpan, hasParentParens),
...n,
} as T & RawNGSpan;
} as T & { start: number; end: number; range: [number, number] };
switch (t) {
case 'NumericLiteral': {
const numericLiteral = newNode as unknown as b.NumericLiteral;
Expand Down Expand Up @@ -503,36 +503,26 @@ export function transformSpan(
): {
start: NonNullable<b.Node['start']>;
end: NonNullable<b.Node['end']>;
loc: NonNullable<b.Node['loc']>;
range: NonNullable<b.Node['range']>;
} {
if (!processSpan) {
const { start, end } = span;
return {
start,
end,
loc: {
filename: '',
identifierName: '',
start: context.locationForIndex(start),
end: context.locationForIndex(end),
},
range: [start, end],
};
}

const { outerSpan, innerSpan, hasParens } = fitSpans(
span,
context.text,
hasParentParens,
);
const {
outerSpan,
innerSpan: { start, end },
hasParens,
} = fitSpans(span, context.text, hasParentParens);
return {
start: innerSpan.start,
end: innerSpan.end,
loc: {
filename: '',
identifierName: '',
start: context.locationForIndex(innerSpan.start),
end: context.locationForIndex(innerSpan.end),
},
start,
end,
range: [start, end],
...(hasParens && {
extra: {
parenthesized: true,
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface NGBaseNode {
type: string;
start: number;
end: number;
loc: b.SourceLocation;
range: [number, number];
}

export type NGNode = { comments?: b.CommentLine[] } & (
Expand Down
32 changes: 18 additions & 14 deletions tests/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import { codeFrameColumns } from '@babel/code-frame';
import * as babelParser from '@babel/parser';
import { LinesAndColumns } from 'lines-and-columns';
import { wrap } from 'jest-snapshot-serializer-raw';

const babelParserOptions: babelParser.ParserOptions = {
plugins: [
'typescript', // NonNullAssert
],
ranges: true,
};

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

export function parseBabel(input: string) {
return babelParser.parse(input, babelParserOptions);
const ast = babelParser.parse(input, babelParserOptions);
// https://github.com/babel/babel/issues/15115
for (const comment of ast.comments!) {
// @ts-expect-error -- missing types
comment.range ??= [comment.start, comment.end];
}
return ast;
}

export function massageAst(ast: any): any {
Expand All @@ -35,18 +43,13 @@ export function massageAst(ast: any): any {
delete ast.method;
}

delete ast.loc;

const massaged = Object.keys(ast).reduce((reduced: any, key) => {
switch (key) {
case 'trailingComments':
// do nothing
break;
case 'loc': {
const loc = massageAst(ast[key]);
delete loc.filename;
delete loc.identifierName;
reduced[key] = loc;
break;
}
case 'extra': {
const extra = massageAst(ast[key]);
if (extra) {
Expand Down Expand Up @@ -75,12 +78,13 @@ export function massageAst(ast: any): any {
export function snapshotAst(ast: any, source: string) {
const snapshots: string[] = [];
const isNode = (x: any) => x && x.type;
const linesAndColumns = new LinesAndColumns(source);
visitAst(ast, (node) => {
const props = Object.keys(node).reduce((reduced: any, key) => {
const value = node[key];
switch (key) {
case 'type':
case 'loc':
case 'range':
case 'start':
case 'end':
break;
Expand All @@ -96,13 +100,13 @@ export function snapshotAst(ast: any, source: string) {
return reduced;
}, {});
const fixColumn = (p: { line: number; column: number }) => ({
line: p.line,
line: p.line + 1,
column: p.column + 1,
});
const codeFrame = codeFrameColumns(source, {
start: fixColumn(node.loc.start),
end: fixColumn(node.loc.end),
});
const [start, end] = [node.start, node.end].map((index) =>
fixColumn(linesAndColumns.locationForIndex(index)!),
);
const codeFrame = codeFrameColumns(source, { start, end });
const propsString = JSON.stringify(props, undefined, 2);
snapshots.push(`${node.type} ${propsString}\n${codeFrame}`);
});
Expand Down
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,7 @@ __metadata:
eslint-plugin-unicorn: "npm:49.0.0"
jest: "npm:29.7.0"
jest-snapshot-serializer-raw: "npm:1.2.0"
lines-and-columns: "npm:^2.0.4"
lines-and-columns: "npm:2.0.4"
npm-run-all: "npm:4.1.5"
prettier: "npm:3.0.3"
standard-version: "npm:9.5.0"
Expand Down Expand Up @@ -4476,20 +4476,20 @@ __metadata:
languageName: node
linkType: hard

"lines-and-columns@npm:2.0.4":
version: 2.0.4
resolution: "lines-and-columns@npm:2.0.4"
checksum: 81ac2f943f5428a46bd4ea2561c74ba674a107d8e6cc70cd317d16892a36ff3ba0dc6e599aca8b6f8668d26c85288394c6edf7a40e985ca843acab3701b80d4c
languageName: node
linkType: hard

"lines-and-columns@npm:^1.1.6":
version: 1.2.4
resolution: "lines-and-columns@npm:1.2.4"
checksum: 0c37f9f7fa212b38912b7145e1cd16a5f3cd34d782441c3e6ca653485d326f58b3caccda66efce1c5812bde4961bbde3374fae4b0d11bf1226152337f3894aa5
languageName: node
linkType: hard

"lines-and-columns@npm:^2.0.4":
version: 2.0.4
resolution: "lines-and-columns@npm:2.0.4"
checksum: 81ac2f943f5428a46bd4ea2561c74ba674a107d8e6cc70cd317d16892a36ff3ba0dc6e599aca8b6f8668d26c85288394c6edf7a40e985ca843acab3701b80d4c
languageName: node
linkType: hard

"load-json-file@npm:^4.0.0":
version: 4.0.0
resolution: "load-json-file@npm:4.0.0"
Expand Down

0 comments on commit 069199b

Please sign in to comment.