Repository: prettier/prettier (JavaScript)
Date: 2018-02-28 06:15:01+00:00
Task type: Bug Fix

Issue:
deprecated postcss parser no longer working
The problematic commit is [dc26445](https://github.com/prettier/prettier/commit/dc26445e518dfc122810850fde23df449c8b7095)

I was unable to reproduce on the playground because this is a deprecated option, thus I created a ~~[github repo](https://github.com/olsonpm/test-prettier)~~ with reproduction instructions.

I looked a bit at the code but was pretty lost at the intent of what all was going on in that commit.  It would be helpful to have someone more familiar with the codebase take a look, though I will spend some more time myself tomorrow.

Workaround: `npm i prettier@1.10`

I noticed this issue when prettier-atom was unable to format my `*.scss` file - it still relies on the deprecated option.

Thanks so much again for this library


Issue discussion:

Solution patch:
diff --git a/src/main/options.js b/src/main/options.js
index d285bfe7c6df..da786dbd528c 100644
--- a/src/main/options.js
+++ b/src/main/options.js
@@ -26,7 +26,8 @@ function normalize(options, opts) {
   const supportOptions = getSupportInfo(null, {
     plugins,
     pluginsLoaded: true,
-    showUnreleased: true
+    showUnreleased: true,
+    showDeprecated: true
   }).options;
   const defaults = supportOptions.reduce(
     (reduced, optionInfo) =>
@@ -49,10 +50,20 @@ function normalize(options, opts) {
     }
   }
 
-  const parser = resolveParser(rawOptions);
+  const parser = resolveParser(
+    !rawOptions.parser
+      ? rawOptions
+      : // handle deprecated parsers
+        normalizer.normalizeApiOptions(
+          rawOptions,
+          [supportOptions.find(x => x.name === "parser")],
+          { passThrough: true, logger: false }
+        )
+  );
   rawOptions.astFormat = parser.astFormat;
   rawOptions.locEnd = parser.locEnd;
   rawOptions.locStart = parser.locStart;
+
   const plugin = getPlugin(rawOptions);
   rawOptions.printer = plugin.printers[rawOptions.astFormat];
 


Test patch:
diff --git a/tests_integration/__tests__/__snapshots__/deprecated-parser.js.snap b/tests_integration/__tests__/__snapshots__/deprecated-parser.js.snap
new file mode 100644
index 000000000000..beb62c7a8536
--- /dev/null
+++ b/tests_integration/__tests__/__snapshots__/deprecated-parser.js.snap
@@ -0,0 +1,6 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`API format with deprecated parser should work 1`] = `
+"\`{ \\"parser\\": \\"postcss\\" }\` is deprecated. Prettier now treats it as \`{ \\"parser\\": \\"css\\" }\`.
+"
+`;
diff --git a/tests_integration/__tests__/deprecated-parser.js b/tests_integration/__tests__/deprecated-parser.js
new file mode 100644
index 000000000000..96e111fb35ee
--- /dev/null
+++ b/tests_integration/__tests__/deprecated-parser.js
@@ -0,0 +1,26 @@
+"use strict";
+
+const prettier = require("../../tests_config/require_prettier");
+
+let warnings = "";
+
+beforeAll(() => {
+  jest
+    .spyOn(console, "warn")
+    .mockImplementation(text => (warnings += text + "\n"));
+});
+
+beforeEach(() => {
+  warnings = "";
+});
+
+afterAll(() => {
+  jest.restoreAllMocks();
+});
+
+test("API format with deprecated parser should work", () => {
+  expect(() =>
+    prettier.format("body { color: #131313; }", { parser: "postcss" })
+  ).not.toThrowError();
+  expect(warnings).toMatchSnapshot();
+});