UNPKG

@babel/parser

Version:
128 lines (125 loc) 3.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NodeUtils = void 0; exports.cloneIdentifier = cloneIdentifier; exports.cloneStringLiteral = cloneStringLiteral; var _util = require("./util"); var _location = require("../util/location"); class Node { constructor(parser, pos, loc) { this.type = ""; this.start = pos; this.end = 0; this.loc = new _location.SourceLocation(loc); if (parser != null && parser.options.ranges) this.range = [pos, 0]; if (parser != null && parser.filename) this.loc.filename = parser.filename; } } const NodePrototype = Node.prototype; { NodePrototype.__clone = function () { const newNode = new Node(undefined, this.start, this.loc.start); const keys = Object.keys(this); for (let i = 0, length = keys.length; i < length; i++) { const key = keys[i]; if (key !== "leadingComments" && key !== "trailingComments" && key !== "innerComments") { newNode[key] = this[key]; } } return newNode; }; } function clonePlaceholder(node) { return cloneIdentifier(node); } function cloneIdentifier(node) { const { type, start, end, loc, range, extra, name } = node; const cloned = Object.create(NodePrototype); cloned.type = type; cloned.start = start; cloned.end = end; cloned.loc = loc; cloned.range = range; cloned.extra = extra; cloned.name = name; if (type === "Placeholder") { cloned.expectedNode = node.expectedNode; } return cloned; } function cloneStringLiteral(node) { const { type, start, end, loc, range, extra } = node; if (type === "Placeholder") { return clonePlaceholder(node); } const cloned = Object.create(NodePrototype); cloned.type = type; cloned.start = start; cloned.end = end; cloned.loc = loc; cloned.range = range; if (node.raw !== undefined) { cloned.raw = node.raw; } else { cloned.extra = extra; } cloned.value = node.value; return cloned; } class NodeUtils extends _util.default { startNode() { return new Node(this, this.state.start, this.state.startLoc); } startNodeAt(loc) { return new Node(this, loc.index, loc); } startNodeAtNode(type) { return this.startNodeAt(type.loc.start); } finishNode(node, type) { return this.finishNodeAt(node, type, this.state.lastTokEndLoc); } finishNodeAt(node, type, endLoc) { if (process.env.NODE_ENV !== "production" && node.end > 0) { throw new Error("Do not call finishNode*() twice on the same node." + " Instead use resetEndLocation() or change type directly."); } node.type = type; node.end = endLoc.index; node.loc.end = endLoc; if (this.options.ranges) node.range[1] = endLoc.index; if (this.options.attachComment) this.processComment(node); return node; } resetStartLocation(node, startLoc) { node.start = startLoc.index; node.loc.start = startLoc; if (this.options.ranges) node.range[0] = startLoc.index; } resetEndLocation(node, endLoc = this.state.lastTokEndLoc) { node.end = endLoc.index; node.loc.end = endLoc; if (this.options.ranges) node.range[1] = endLoc.index; } resetStartLocationFromNode(node, locationNode) { this.resetStartLocation(node, locationNode.loc.start); } } exports.NodeUtils = NodeUtils; //# sourceMappingURL=node.js.map