Skip to content

Commit

Permalink
Expose .index on Position to internally track nodes location (#14174)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
tolmasky and nicolo-ribaudo committed Feb 2, 2022
1 parent df27d54 commit 97a8bcb
Show file tree
Hide file tree
Showing 5,013 changed files with 59,254 additions and 59,355 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
102 changes: 68 additions & 34 deletions packages/babel-core/test/fixtures/parse/output.json
Expand Up @@ -6,11 +6,13 @@
"loc": {
"start": {
"line": 1,
"column": 0
"column": 0,
"index": 0
},
"end": {
"line": 7,
"column": 0
"column": 0,
"index": 91
}
},
"program": {
Expand All @@ -20,11 +22,13 @@
"loc": {
"start": {
"line": 1,
"column": 0
"column": 0,
"index": 0
},
"end": {
"line": 7,
"column": 0
"column": 0,
"index": 91
}
},
"interpreter": null,
Expand All @@ -37,11 +41,13 @@
"loc": {
"start": {
"line": 1,
"column": 0
"column": 0,
"index": 0
},
"end": {
"line": 2,
"column": 17
"column": 17,
"index": 29
}
},
"decorators": [
Expand All @@ -52,11 +58,13 @@
"loc": {
"start": {
"line": 1,
"column": 0
"column": 0,
"index": 0
},
"end": {
"line": 1,
"column": 11
"column": 11,
"index": 11
}
},
"expression": {
Expand All @@ -66,11 +74,13 @@
"loc": {
"start": {
"line": 1,
"column": 1
"column": 1,
"index": 1
},
"end": {
"line": 1,
"column": 11
"column": 11,
"index": 11
},
"identifierName": "annotation"
},
Expand All @@ -85,11 +95,13 @@
"loc": {
"start": {
"line": 2,
"column": 6
"column": 6,
"index": 18
},
"end": {
"line": 2,
"column": 13
"column": 13,
"index": 25
},
"identifierName": "MyClass"
},
Expand All @@ -103,11 +115,13 @@
"loc": {
"start": {
"line": 2,
"column": 14
"column": 14,
"index": 26
},
"end": {
"line": 2,
"column": 17
"column": 17,
"index": 29
}
},
"body": []
Expand All @@ -120,11 +134,13 @@
"loc": {
"start": {
"line": 4,
"column": 0
"column": 0,
"index": 31
},
"end": {
"line": 6,
"column": 1
"column": 1,
"index": 90
}
},
"id": {
Expand All @@ -134,11 +150,13 @@
"loc": {
"start": {
"line": 4,
"column": 9
"column": 9,
"index": 40
},
"end": {
"line": 4,
"column": 19
"column": 19,
"index": 50
},
"identifierName": "annotation"
},
Expand All @@ -154,11 +172,13 @@
"loc": {
"start": {
"line": 4,
"column": 20
"column": 20,
"index": 51
},
"end": {
"line": 4,
"column": 26
"column": 26,
"index": 57
},
"identifierName": "target"
},
Expand All @@ -172,11 +192,13 @@
"loc": {
"start": {
"line": 4,
"column": 28
"column": 28,
"index": 59
},
"end": {
"line": 6,
"column": 1
"column": 1,
"index": 90
}
},
"body": [
Expand All @@ -187,11 +209,13 @@
"loc": {
"start": {
"line": 5,
"column": 3
"column": 3,
"index": 64
},
"end": {
"line": 5,
"column": 27
"column": 27,
"index": 88
}
},
"expression": {
Expand All @@ -201,11 +225,13 @@
"loc": {
"start": {
"line": 5,
"column": 3
"column": 3,
"index": 64
},
"end": {
"line": 5,
"column": 26
"column": 26,
"index": 87
}
},
"operator": "=",
Expand All @@ -216,11 +242,13 @@
"loc": {
"start": {
"line": 5,
"column": 3
"column": 3,
"index": 64
},
"end": {
"line": 5,
"column": 19
"column": 19,
"index": 80
}
},
"object": {
Expand All @@ -230,11 +258,13 @@
"loc": {
"start": {
"line": 5,
"column": 3
"column": 3,
"index": 64
},
"end": {
"line": 5,
"column": 9
"column": 9,
"index": 70
},
"identifierName": "target"
},
Expand All @@ -247,11 +277,13 @@
"loc": {
"start": {
"line": 5,
"column": 10
"column": 10,
"index": 71
},
"end": {
"line": 5,
"column": 19
"column": 19,
"index": 80
},
"identifierName": "annotated"
},
Expand All @@ -266,11 +298,13 @@
"loc": {
"start": {
"line": 5,
"column": 22
"column": 22,
"index": 83
},
"end": {
"line": 5,
"column": 26
"column": 26,
"index": 87
}
},
"value": true
Expand Down
7 changes: 3 additions & 4 deletions packages/babel-parser/src/parser/error.js
@@ -1,6 +1,6 @@
// @flow
/* eslint sort-keys: "error" */
import { type Position, indexes } from "../util/location";
import { type Position } from "../util/location";
import CommentsParser from "./comments";
import { type ErrorCode, ErrorCodes } from "./error-codes";
import { type Node } from "../types";
Expand Down Expand Up @@ -111,8 +111,7 @@ export default class ParserError extends CommentsParser {
{ code, template }: ErrorTemplate,
...params: any
): Error | empty {
// $FlowIgnore[incompatible-type] We know this exists, so it can't be undefined.
const pos: number = indexes.get(loc);
const pos = loc.index;
const message =
template.replace(/%(\d+)/g, (_, i: number) => params[i]) +
` (${loc.line}:${loc.column})`;
Expand All @@ -139,7 +138,7 @@ export default class ParserError extends CommentsParser {
errorTemplate: string,
...params: any
): Error | empty {
const pos = indexes.get(loc);
const pos = loc.index;
const message =
errorTemplate.replace(/%(\d+)/g, (_, i: number) => params[i]) +
` (${loc.line}:${loc.column})`;
Expand Down
20 changes: 6 additions & 14 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -44,11 +44,7 @@ import {
isIdentifierStart,
canBeReservedWord,
} from "../util/identifier";
import {
indexes,
Position,
createPositionWithColumnOffset,
} from "../util/location";
import { Position, createPositionWithColumnOffset } from "../util/location";
import * as charCodes from "charcodes";
import {
BIND_OUTSIDE,
Expand Down Expand Up @@ -318,22 +314,19 @@ export default class ExpressionParser extends LValParser {

if (
refExpressionErrors.doubleProtoLoc != null &&
// $FlowIgnore[incompatible-type] We know this exists, so it can't be undefined.
indexes.get(refExpressionErrors.doubleProtoLoc) >= startPos
refExpressionErrors.doubleProtoLoc.index >= startPos
) {
refExpressionErrors.doubleProtoLoc = null; // reset because double __proto__ is valid in assignment expression
}
if (
refExpressionErrors.shorthandAssignLoc != null &&
// $FlowIgnore[incompatible-type] We know this exists, so it can't be undefined.
indexes.get(refExpressionErrors.shorthandAssignLoc) >= startPos
refExpressionErrors.shorthandAssignLoc.index >= startPos
) {
refExpressionErrors.shorthandAssignLoc = null; // reset because shorthand default was used correctly
}
if (
refExpressionErrors.privateKeyLoc != null &&
// $FlowIgnore[incompatible-type] We know this exists, so it can't be undefined.
indexes.get(refExpressionErrors.privateKeyLoc) >= startPos
refExpressionErrors.privateKeyLoc.index >= startPos
) {
this.checkDestructuringPrivate(refExpressionErrors);
refExpressionErrors.privateKeyLoc = null; // reset because `({ #x: x })` is an assignable pattern
Expand Down Expand Up @@ -930,7 +923,7 @@ export default class ExpressionParser extends LValParser {
return (
base.type === "Identifier" &&
base.name === "async" &&
indexes.get(this.state.lastTokEndLoc) === base.end &&
this.state.lastTokEndLoc.index === base.end &&
!this.canInsertSemicolon() &&
// check there are no escape sequences, such as \u{61}sync
base.end - base.start === 5 &&
Expand Down Expand Up @@ -1788,8 +1781,7 @@ export default class ExpressionParser extends LValParser {
this.takeSurroundingComments(
val,
startPos,
// $FlowIgnore[incompatible-type] We know this exists, so it can't be undefined.
indexes.get(this.state.lastTokEndLoc),
this.state.lastTokEndLoc.index,
);

return val;
Expand Down
11 changes: 5 additions & 6 deletions packages/babel-parser/src/parser/node.js
Expand Up @@ -2,7 +2,7 @@

import type Parser from "./index";
import UtilParser from "./util";
import { indexes, type Position, SourceLocation } from "../util/location";
import { SourceLocation, type Position } from "../util/location";
import type { Comment, Node as NodeType, NodeBase } from "../types";

// Start an AST node, attaching a start offset.
Expand Down Expand Up @@ -126,9 +126,9 @@ export class NodeUtils extends UtilParser {
);
}
node.type = type;
node.end = indexes.get(endLoc);
node.end = endLoc.index;
node.loc.end = endLoc;
if (this.options.ranges) node.range[1] = node.end;
if (this.options.ranges) node.range[1] = endLoc.index;
if (this.options.attachComment) this.processComment(node);
return node;
}
Expand All @@ -143,10 +143,9 @@ export class NodeUtils extends UtilParser {
node: NodeBase,
endLoc?: Position = this.state.lastTokEndLoc,
): void {
// $FlowIgnore[incompatible-type] We know this exists, so it can't be undefined.
node.end = indexes.get(endLoc);
node.end = endLoc.index;
node.loc.end = endLoc;
if (this.options.ranges) node.range[1] = node.end;
if (this.options.ranges) node.range[1] = endLoc.index;
}

/**
Expand Down

0 comments on commit 97a8bcb

Please sign in to comment.