UNPKG

sucrase

Version:

Super-fast alternative to Babel for when you can target modern JS runtimes

88 lines (67 loc) 2.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NodeUtils = void 0; var _index = _interopRequireDefault(require("./index")); var _util = _interopRequireDefault(require("./util")); var _location = require("../util/location"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } // Start an AST node, attaching a start offset. const commentKeys = ["leadingComments", "trailingComments", "innerComments"]; class Node { constructor(parser, pos, loc) { this.type = ""; this.start = pos; this.end = 0; this.loc = new _location.SourceLocation(loc); if (parser && parser.options.ranges) this.range = [pos, 0]; if (parser && parser.filename) this.loc.filename = parser.filename; } __clone() { // $FlowIgnore const node2 = new Node(); Object.keys(this).forEach(key => { // Do not clone comments that are already attached to the node if (commentKeys.indexOf(key) < 0) { // $FlowIgnore node2[key] = this[key]; } }); return node2; } } class NodeUtils extends _util.default { startNode() { // $FlowIgnore return new Node(this, this.state.start, this.state.startLoc); } startNodeAt(pos, loc) { // $FlowIgnore return new Node(this, pos, loc); } /** Start a new node with a previous node's location. */ startNodeAtNode(type) { return this.startNodeAt(type.start, type.loc.start); } // Finish an AST node, adding `type` and `end` properties. finishNode(node, type) { return this.finishNodeAt(node, type, this.state.lastTokEnd, this.state.lastTokEndLoc); } // Finish node at given position finishNodeAt(node, type, pos, loc) { node.type = type; node.end = pos; node.loc.end = loc; if (this.options.ranges) node.range[1] = pos; this.processComment(node); return node; } /** * Reset the start location of node to the start location of locationNode */ resetStartLocationFromNode(node, locationNode) { node.start = locationNode.start; node.loc.start = locationNode.loc.start; if (this.options.ranges) node.range[0] = locationNode.range[0]; } } exports.NodeUtils = NodeUtils;