react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / node_modules / commoner / node_modules / recast / lib / parser.js
81169 viewsvar assert = require("assert");1var types = require("./types");2var n = types.namedTypes;3var b = types.builders;4var isObject = types.builtInTypes.object;5var isArray = types.builtInTypes.array;6var isFunction = types.builtInTypes.function;7var Patcher = require("./patcher").Patcher;8var normalizeOptions = require("./options").normalize;9var fromString = require("./lines").fromString;10var attachComments = require("./comments").attach;1112exports.parse = function parse(source, options) {13options = normalizeOptions(options);1415var lines = fromString(source, options);1617var sourceWithoutTabs = lines.toString({18tabWidth: options.tabWidth,19reuseWhitespace: false,20useTabs: false21});2223var program = options.esprima.parse(sourceWithoutTabs, {24loc: true,25range: options.range,26comment: true,27tolerant: options.tolerant,28sourceType: 'module'29});3031var comments = program.comments;32delete program.comments;3334// In order to ensure we reprint leading and trailing program35// comments, wrap the original Program node with a File node.36var file = b.file(program);37file.loc = {38lines: lines,39indent: 0,40start: lines.firstPos(),41end: lines.lastPos()42};4344// Return a copy of the original AST so that any changes made may be45// compared to the original.46var copy = new TreeCopier(lines).copy(file);4748// Attach comments to the copy rather than the original.49attachComments(comments, copy, lines);5051return copy;52};5354function TreeCopier(lines) {55assert.ok(this instanceof TreeCopier);56this.lines = lines;57this.indent = 0;58}5960var TCp = TreeCopier.prototype;6162TCp.copy = function(node) {63if (isArray.check(node)) {64return node.map(this.copy, this);65}6667if (!isObject.check(node)) {68return node;69}7071if ((n.MethodDefinition && n.MethodDefinition.check(node)) ||72(n.Property.check(node) && (node.method || node.shorthand))) {73// If the node is a MethodDefinition or a .method or .shorthand74// Property, then the location information stored in75// node.value.loc is very likely untrustworthy (just the {body}76// part of a method, or nothing in the case of shorthand77// properties), so we null out that information to prevent78// accidental reuse of bogus source code during reprinting.79node.value.loc = null;8081if (n.FunctionExpression.check(node.value)) {82// FunctionExpression method values should be anonymous,83// because their .id fields are ignored anyway.84node.value.id = null;85}86}8788var copy = Object.create(Object.getPrototypeOf(node), {89original: { // Provide a link from the copy to the original.90value: node,91configurable: false,92enumerable: false,93writable: true94}95});9697var loc = node.loc;98var oldIndent = this.indent;99var newIndent = oldIndent;100101if (loc) {102if (loc.start.line < 1) {103loc.start.line = 1;104}105106if (loc.end.line < 1) {107loc.end.line = 1;108}109110if (this.lines.isPrecededOnlyByWhitespace(loc.start)) {111newIndent = this.indent = loc.start.column;112}113114loc.lines = this.lines;115loc.indent = newIndent;116}117118var keys = Object.keys(node);119var keyCount = keys.length;120for (var i = 0; i < keyCount; ++i) {121var key = keys[i];122if (key === "loc") {123copy[key] = node[key];124} else if (key === "comments") {125// Handled below.126} else {127copy[key] = this.copy(node[key]);128}129}130131this.indent = oldIndent;132133if (node.comments) {134Object.defineProperty(copy, "comments", {135value: node.comments,136enumerable: false137});138}139140return copy;141};142143144