Repository: jlongster/prettier
Pull request #191 (2017-01-14 12:48:57)

Remove legacy Recast code and simplify API.
Source maps are no longer really relevant to this project, so I completely removed code dealing with them, as Prettier reprints everything anyway and it would be nearly impossible to remap from the old source to the new one. This also allows us to remove some legacy Recast options and API.

hzoo: a good point!
jlongster: This PR is great, I will take a closer look later, thanks. I think we can go ahead and remove this code to simplify it. We can always refer to recast again later to pull this code in again.

While sourcemaps aren't necessarily required, and involve a lot of complexity, I agree it's worth investigating if we can make them work. That would make it easier to use as a frontend to other tools that generate AST from a source. It'll require a lot of thought though and I really hope they don't kill performance (even if we made them optional).
hzoo: Yeah @jlongster I did want to investigate what would happen if prettier was used for Babel's output just for kicks. Right now its babylon -> babel transform(s) -> babel-generator but we could do babylon -> babel transform(s) -> prettier too?

should just be this

```js
// .babelrc
{
  "generatorOpts": {
    "generator": "prettier"
  }
}
```

if prettier is in node_modules

Patch:
diff --git a/README.md b/README.md
index d77edc0b4..a4a5b25af 100644
--- a/README.md
+++ b/README.md
@@ -181,6 +181,10 @@ Can also be installed using `ext install prettier-vscode`
 
 [Check repository for configuration and shortcuts](https://github.com/esbenp/prettier-vscode)
 
+### Sublime Text
+
+Please see [this issue](https://github.com/jlongster/prettier/issues/17) for those interested in working on Sublime Text integration.
+
 More editors are coming soon.
 
 ## Language Support
diff --git a/index.js b/index.js
index 54de9ad6c..1f7fed44d 100644
--- a/index.js
+++ b/index.js
@@ -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) {
diff --git a/package.json b/package.json
index 8c4662616..04c16ab22 100644
--- a/package.json
+++ b/package.json
@@ -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",
diff --git a/src/fast-path.js b/src/fast-path.js
index 5299ebc1c..4bda096aa 100644
--- a/src/fast-path.js
+++ b/src/fast-path.js
@@ -289,6 +289,7 @@ FPp.needsParens = function(assumeExpressionContext) {
           return true;
       }
 
+    case "AwaitExpression":
     case "YieldExpression":
       switch (parent.type) {
         case "BinaryExpression":
@@ -296,13 +297,14 @@ FPp.needsParens = function(assumeExpressionContext) {
         case "UnaryExpression":
         case "SpreadElement":
         case "SpreadProperty":
-        case "CallExpression":
-        case "MemberExpression":
         case "NewExpression":
         case "ConditionalExpression":
-        case "YieldExpression":
+        case "MemberExpression":
           return true;
 
+        case "CallExpression":
+          return parent.callee === node;
+
         default:
           return false;
       }
diff --git a/src/printer.js b/src/printer.js
index fde8865bf..ecfab4ea3 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -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);
   };
 }
 
diff --git a/src/util.js b/src/util.js
index d124a686f..bc51155ef 100644
--- a/src/util.js
+++ b/src/util.js
@@ -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));


Test patch:
diff --git a/tests/async/__snapshots__/jsfmt.spec.js.snap b/tests/async/__snapshots__/jsfmt.spec.js.snap
index 53f85d5cb..d0b6434b0 100644
--- a/tests/async/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/async/__snapshots__/jsfmt.spec.js.snap
@@ -459,6 +459,13 @@ console.log(x.await);
 
 var await = 3;
 var y = { await };
+
+async function f() { (await f()).length }
+async function g() {
+  invariant(
+    (await driver.navigator.getUrl()).substr(-7)
+  );
+}
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 async function f() {
   await 1;
@@ -514,5 +521,12 @@ console.log(x.await);
 
 var await = 3;
 var y = { await };
+
+async function f() {
+  (await f()).length;
+}
+async function g() {
+  invariant((await driver.navigator.getUrl()).substr(-7));
+}
 "
 `;
diff --git a/tests/async/await_parse.js b/tests/async/await_parse.js
index 10149204e..042e679b0 100644
--- a/tests/async/await_parse.js
+++ b/tests/async/await_parse.js
@@ -22,3 +22,10 @@ console.log(x.await);
 
 var await = 3;
 var y = { await };
+
+async function f() { (await f()).length }
+async function g() {
+  invariant(
+    (await driver.navigator.getUrl()).substr(-7)
+  );
+}