Remove legacy Recast code and simplify API. (#191)

master
Benjamin Tan 2017-01-15 12:25:30 +08:00 committed by James Long
parent 7f80d8dbff
commit 3f31a87da1
4 changed files with 4 additions and 121 deletions

View File

@ -50,7 +50,7 @@ function format(text, opts) {
opts.originalText = text;
const printer = new Printer(opts);
return printer.printGenerically(ast).code;
return printer.print(ast);
}
function formatWithShebang(text, opts) {

View File

@ -21,8 +21,7 @@
"flow-parser": "^0.37.0",
"get-stdin": "^5.0.1",
"minimist": "^1.2.0",
"private": "^0.1.6",
"source-map": "^0.5.6"
"private": "^0.1.6"
},
"devDependencies": {
"jest": "^18.0.0",

View File

@ -1,6 +1,5 @@
"use strict";
var assert = require("assert");
var sourceMap = require("source-map");
var printComments = require("./comments").printComments;
var pp = require("./pp");
var fromString = pp.fromString;
@ -27,52 +26,13 @@ var FastPath = require("./fast-path");
var util = require("./util");
var isIdentifierName = require("esutils").keyword.isIdentifierNameES6;
function PrintResult(code, sourceMap) {
assert.ok(this instanceof PrintResult);
isString.assert(code);
this.code = code;
if (sourceMap) {
isObject.assert(sourceMap);
this.map = sourceMap;
}
}
var PRp = PrintResult.prototype;
var warnedAboutToString = false;
PRp.toString = function() {
if (!warnedAboutToString) {
console.warn(
"Deprecation warning: recast.print now returns an object with " +
"a .code property. You appear to be treating the object as a " +
"string, which might still work but is strongly discouraged."
);
warnedAboutToString = true;
}
return this.code;
};
var emptyPrintResult = new PrintResult("");
function Printer(originalOptions) {
assert.ok(this instanceof Printer);
var explicitTabWidth = originalOptions && originalOptions.tabWidth;
var options = normalizeOptions(originalOptions);
assert.notStrictEqual(options, originalOptions);
// It's common for client code to pass the same options into both
// recast.parse and recast.print, but the Printer doesn't need (and
// can be confused by) options.sourceFileName, so we null it out.
options.sourceFileName = null;
// Print the entire AST generically.
function printGenerically(path) {
return printComments(
@ -84,37 +44,13 @@ function Printer(originalOptions) {
this.print = function(ast) {
if (!ast) {
return emptyPrintResult;
}
var lines = print(FastPath.from(ast), true);
return new PrintResult(
lines.toString(options),
util.composeSourceMaps(
options.inputSourceMap,
lines.getSourceMap(options.sourceMapName, options.sourceRoot)
)
);
};
this.printGenerically = function(ast) {
if (!ast) {
return emptyPrintResult;
return "";
}
var path = FastPath.from(ast);
var oldReuseWhitespace = options.reuseWhitespace;
// Do not reuse whitespace (or anything else, for that matter)
// when printing generically.
options.reuseWhitespace = false;
var res = printGenerically(path);
var pr = new PrintResult(pp.print(options.printWidth, res));
options.reuseWhitespace = oldReuseWhitespace;
return pr;
return pp.print(options.printWidth, res);
};
}

View File

@ -3,9 +3,6 @@ var assert = require("assert");
var types = require("ast-types");
var getFieldValue = types.getFieldValue;
var n = types.namedTypes;
var sourceMap = require("source-map");
var SourceMapConsumer = sourceMap.SourceMapConsumer;
var SourceMapGenerator = sourceMap.SourceMapGenerator;
var hasOwn = Object.prototype.hasOwnProperty;
var util = exports;
@ -33,55 +30,6 @@ function copyPos(pos) {
}
util.copyPos = copyPos;
util.composeSourceMaps = function(formerMap, latterMap) {
if (formerMap) {
if (!latterMap) {
return formerMap;
}
} else {
return latterMap || null;
}
var smcFormer = new SourceMapConsumer(formerMap);
var smcLatter = new SourceMapConsumer(latterMap);
var smg = new SourceMapGenerator({
file: latterMap.file,
sourceRoot: latterMap.sourceRoot
});
var sourcesToContents = {};
smcLatter.eachMapping(function(mapping) {
var origPos = smcFormer.originalPositionFor({
line: mapping.originalLine,
column: mapping.originalColumn
});
var sourceName = origPos.source;
if (sourceName === null) {
return;
}
smg.addMapping({
source: sourceName,
original: copyPos(origPos),
generated: {
line: mapping.generatedLine,
column: mapping.generatedColumn
},
name: mapping.name
});
var sourceContent = smcFormer.sourceContentFor(sourceName);
if (sourceContent && !hasOwn.call(sourcesToContents, sourceName)) {
sourcesToContents[sourceName] = sourceContent;
smg.setSourceContent(sourceName, sourceContent);
}
});
return smg.toJSON();
};
function expandLoc(parentNode, childNode) {
if (locStart(childNode) - locStart(parentNode) < 0) {
setLocStart(parentNode, locStart(childNode));