123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.parse = void 0;
- var tslib_1 = require("tslib");
- var assert_1 = tslib_1.__importDefault(require("assert"));
- var types = tslib_1.__importStar(require("ast-types"));
- var b = types.builders;
- var isObject = types.builtInTypes.object;
- var isArray = types.builtInTypes.array;
- var options_1 = require("./options");
- var lines_1 = require("./lines");
- var comments_1 = require("./comments");
- var util = tslib_1.__importStar(require("./util"));
- function parse(source, options) {
- options = (0, options_1.normalize)(options);
- var lines = (0, lines_1.fromString)(source, options);
- var sourceWithoutTabs = lines.toString({
- tabWidth: options.tabWidth,
- reuseWhitespace: false,
- useTabs: false,
- });
- var comments = [];
- var ast = options.parser.parse(sourceWithoutTabs, {
- jsx: true,
- loc: true,
- locations: true,
- range: options.range,
- comment: true,
- onComment: comments,
- tolerant: util.getOption(options, "tolerant", true),
- ecmaVersion: 6,
- sourceType: util.getOption(options, "sourceType", "module"),
- });
-
-
-
-
- var tokens = Array.isArray(ast.tokens)
- ? ast.tokens
- : require("esprima").tokenize(sourceWithoutTabs, {
- loc: true,
- });
-
- delete ast.tokens;
-
- tokens.forEach(function (token) {
- if (typeof token.value !== "string") {
- token.value = lines.sliceString(token.loc.start, token.loc.end);
- }
- });
- if (Array.isArray(ast.comments)) {
- comments = ast.comments;
- delete ast.comments;
- }
- if (ast.loc) {
-
-
- util.fixFaultyLocations(ast, lines);
- }
- else {
- ast.loc = {
- start: lines.firstPos(),
- end: lines.lastPos(),
- };
- }
- ast.loc.lines = lines;
- ast.loc.indent = 0;
- var file;
- var program;
- if (ast.type === "Program") {
- program = ast;
-
-
-
-
- file = b.file(ast, options.sourceFileName || null);
- file.loc = {
- start: lines.firstPos(),
- end: lines.lastPos(),
- lines: lines,
- indent: 0,
- };
- }
- else if (ast.type === "File") {
- file = ast;
- program = file.program;
- }
-
- if (options.tokens) {
- file.tokens = tokens;
- }
-
-
-
-
-
- var trueProgramLoc = util.getTrueLoc({
- type: program.type,
- loc: program.loc,
- body: [],
- comments: comments,
- }, lines);
- program.loc.start = trueProgramLoc.start;
- program.loc.end = trueProgramLoc.end;
-
-
- (0, comments_1.attach)(comments, program.body.length ? file.program : file, lines);
-
-
- return new TreeCopier(lines, tokens).copy(file);
- }
- exports.parse = parse;
- var TreeCopier = function TreeCopier(lines, tokens) {
- assert_1.default.ok(this instanceof TreeCopier);
- this.lines = lines;
- this.tokens = tokens;
- this.startTokenIndex = 0;
- this.endTokenIndex = tokens.length;
- this.indent = 0;
- this.seen = new Map();
- };
- var TCp = TreeCopier.prototype;
- TCp.copy = function (node) {
- if (this.seen.has(node)) {
- return this.seen.get(node);
- }
- if (isArray.check(node)) {
- var copy_1 = new Array(node.length);
- this.seen.set(node, copy_1);
- node.forEach(function (item, i) {
- copy_1[i] = this.copy(item);
- }, this);
- return copy_1;
- }
- if (!isObject.check(node)) {
- return node;
- }
- util.fixFaultyLocations(node, this.lines);
- var copy = Object.create(Object.getPrototypeOf(node), {
- original: {
-
- value: node,
- configurable: false,
- enumerable: false,
- writable: true,
- },
- });
- this.seen.set(node, copy);
- var loc = node.loc;
- var oldIndent = this.indent;
- var newIndent = oldIndent;
- var oldStartTokenIndex = this.startTokenIndex;
- var oldEndTokenIndex = this.endTokenIndex;
- if (loc) {
-
-
-
-
-
- if (node.type === "Block" ||
- node.type === "Line" ||
- node.type === "CommentBlock" ||
- node.type === "CommentLine" ||
- this.lines.isPrecededOnlyByWhitespace(loc.start)) {
- newIndent = this.indent = loc.start.column;
- }
-
-
- loc.lines = this.lines;
- loc.tokens = this.tokens;
- loc.indent = newIndent;
-
-
-
- this.findTokenRange(loc);
- }
- var keys = Object.keys(node);
- var keyCount = keys.length;
- for (var i = 0; i < keyCount; ++i) {
- var key = keys[i];
- if (key === "loc") {
- copy[key] = node[key];
- }
- else if (key === "tokens" && node.type === "File") {
-
-
- copy[key] = node[key];
- }
- else {
- copy[key] = this.copy(node[key]);
- }
- }
- this.indent = oldIndent;
- this.startTokenIndex = oldStartTokenIndex;
- this.endTokenIndex = oldEndTokenIndex;
- return copy;
- };
- TCp.findTokenRange = function (loc) {
-
-
- while (this.startTokenIndex > 0) {
- var token = loc.tokens[this.startTokenIndex];
- if (util.comparePos(loc.start, token.loc.start) < 0) {
- --this.startTokenIndex;
- }
- else
- break;
- }
-
-
- while (this.endTokenIndex < loc.tokens.length) {
- var token = loc.tokens[this.endTokenIndex];
- if (util.comparePos(token.loc.end, loc.end) < 0) {
- ++this.endTokenIndex;
- }
- else
- break;
- }
-
-
- while (this.startTokenIndex < this.endTokenIndex) {
- var token = loc.tokens[this.startTokenIndex];
- if (util.comparePos(token.loc.start, loc.start) < 0) {
- ++this.startTokenIndex;
- }
- else
- break;
- }
-
- loc.start.token = this.startTokenIndex;
-
-
- while (this.endTokenIndex > this.startTokenIndex) {
- var token = loc.tokens[this.endTokenIndex - 1];
- if (util.comparePos(loc.end, token.loc.end) < 0) {
- --this.endTokenIndex;
- }
- else
- break;
- }
-
-
-
- loc.end.token = this.endTokenIndex;
- };
|