Skip to content

Commit

Permalink
Make index non-enumerable when using the estree plugin.
Browse files Browse the repository at this point in the history
Reviewed by @tolmasky.
  • Loading branch information
tolmasky committed Jan 31, 2022
1 parent c9a13b9 commit 259a9c2
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/babel-parser/src/plugins/estree.js
Expand Up @@ -7,8 +7,29 @@ import * as N from "../types";
import type { Position } from "../util/location";
import { Errors } from "../parser/error";

const { defineProperty } = Object;
const toUnenumerable = (object, key) =>
defineProperty(object, key, { enumerable: false, value: object[key] });

function toESTreeLocation(node) {
toUnenumerable(node.loc.start, "index");
toUnenumerable(node.loc.end, "index");

return node;
}

export default (superClass: Class<Parser>): Class<Parser> =>
class extends superClass {
parse(): File {
const file = toESTreeLocation(super.parse());

if (this.options.tokens) {
file.tokens = file.tokens.map(toESTreeLocation);
}

return file;
}

parseRegExpLiteral({ pattern, flags }): N.Node {
let regex = null;
try {
Expand Down Expand Up @@ -465,4 +486,16 @@ export default (superClass: Class<Parser>): Class<Parser> =>
isObjectMethod(node: N.Node): boolean {
return node.method || node.kind === "get" || node.kind === "set";
}

finishNodeAt<T: NodeType>(node: T, type: string, endLoc: Position): T {
return toESTreeLocation(super.finishNodeAt(node, type, endLoc));
}

resetEndLocation(
node: NodeBase,
endLoc?: Position = this.state.lastTokEndLoc,
): void {
super.resetEndLocation(node, endLoc);
toESTreeLocation(node);
}
};

0 comments on commit 259a9c2

Please sign in to comment.