diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index 7b5a8f5be608..b06c706b2a61 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -41,3 +41,7 @@ Examples: ``` --> + +- API: Fix `prettier.getSupportInfo()` reporting babel parser for older versions of Prettier. ([#5826] by [@azz]) + + In version `1.16.0` of Prettier, the `babylon` parser was renamed to `babel`. Unfortunately this lead to a minor breaking change: `prettier.getSupportInfo('1.15.0')` would report that it supported `babel`, not `babylon`, which breaks text-editor integrations. This has now been fixed. diff --git a/src/main/support.js b/src/main/support.js index 4d72a513c0d1..a8db650a76de 100644 --- a/src/main/support.js +++ b/src/main/support.js @@ -75,6 +75,7 @@ function getSupportInfo(version, opts) { }); const usePostCssParser = semver.lt(version, "1.7.1"); + const useBabylonParser = semver.lt(version, "1.16.0"); const languages = plugins .reduce((all, plugin) => all.concat(plugin.languages || []), []) @@ -92,6 +93,15 @@ function getSupportInfo(version, opts) { }); } + // "babylon" was renamed to "babel" in 1.16.0 + if (useBabylonParser && language.parsers.indexOf("babel") !== -1) { + return Object.assign({}, language, { + parsers: language.parsers.map(parser => + parser === "babel" ? "babylon" : parser + ) + }); + } + if ( usePostCssParser && (language.name === "CSS" || language.group === "CSS") diff --git a/tests_integration/__tests__/__snapshots__/support-info.js.snap b/tests_integration/__tests__/__snapshots__/support-info.js.snap index dfa9251beabc..d899a190b974 100644 --- a/tests_integration/__tests__/__snapshots__/support-info.js.snap +++ b/tests_integration/__tests__/__snapshots__/support-info.js.snap @@ -67,15 +67,15 @@ exports[`API getSupportInfo() with version 0.0.0 1`] = ` Object { "languages": Object { "Flow": Array [ - "babel", + "babylon", "flow", ], "JSX": Array [ - "babel", + "babylon", "flow", ], "JavaScript": Array [ - "babel", + "babylon", "flow", ], }, @@ -137,14 +137,14 @@ exports[`API getSupportInfo() with version 1.0.0 -> 1.4.0 1`] = ` + \\"postcss\\", + ], \\"Flow\\": Array [ - \\"babel\\", + \\"babylon\\", \\"flow\\", ], \\"JSX\\": Array [ @@ -10,24 +13,51 @@ ], \\"JavaScript\\": Array [ - \\"babel\\", + \\"babylon\\", \\"flow\\", ], + \\"Less\\": Array [ @@ -232,7 +232,7 @@ exports[`API getSupportInfo() with version 1.4.0 -> 1.5.0 1`] = ` @@ -5,10 +5,19 @@ ], \\"Flow\\": Array [ - \\"babel\\", + \\"babylon\\", \\"flow\\", ], + \\"GraphQL\\": Array [ @@ -245,7 +245,7 @@ exports[`API getSupportInfo() with version 1.4.0 -> 1.5.0 1`] = ` + \\"json\\", + ], \\"JSX\\": Array [ - \\"babel\\", + \\"babylon\\", \\"flow\\", ], \\"JavaScript\\": Array [ @@ -277,12 +277,12 @@ exports[`API getSupportInfo() with version 1.5.0 -> 1.7.1 1`] = ` + \\"css\\", ], \\"Flow\\": Array [ - \\"babel\\", + \\"babylon\\", \\"flow\\", ], @@ -23,17 +23,17 @@ \\"JavaScript\\": Array [ - \\"babel\\", + \\"babylon\\", \\"flow\\", ], \\"Less\\": Array [ @@ -407,12 +407,12 @@ exports[`API getSupportInfo() with version 1.8.0 -> 1.8.2 1`] = ` \\"start\\": 0," `; -exports[`API getSupportInfo() with version 1.8.2 -> undefined 1`] = ` +exports[`API getSupportInfo() with version 1.8.2 -> 1.16.0 1`] = ` "Snapshot Diff: - First value + Second value -@@ -1,23 +1,35 @@ +@@ -1,34 +1,49 @@ Object { \\"languages\\": Object { + \\"Angular\\": Array [ @@ -422,7 +422,8 @@ exports[`API getSupportInfo() with version 1.8.2 -> undefined 1`] = ` \\"css\\", ], \\"Flow\\": Array [ - \\"babel\\", +- \\"babylon\\", ++ \\"babel\\", \\"flow\\", ], \\"GraphQL\\": Array [ @@ -444,11 +445,13 @@ exports[`API getSupportInfo() with version 1.8.2 -> undefined 1`] = ` + \\"json5\\", + ], \\"JSX\\": Array [ - \\"babel\\", +- \\"babylon\\", ++ \\"babel\\", \\"flow\\", ], \\"JavaScript\\": Array [ -@@ -25,10 +37,13 @@ +- \\"babylon\\", ++ \\"babel\\", \\"flow\\", ], \\"Less\\": Array [ @@ -589,6 +592,11 @@ exports[`API getSupportInfo() with version 1.8.2 -> undefined 1`] = ` \\"range\\": Object {" `; +exports[`API getSupportInfo() with version 1.16.0 -> undefined 1`] = ` +"Snapshot Diff: +Compared values have no visual difference." +`; + exports[`CLI --support-info (stderr) 1`] = `""`; exports[`CLI --support-info (stdout) 1`] = ` diff --git a/tests_integration/__tests__/support-info.js b/tests_integration/__tests__/support-info.js index eecbdc034ad3..f5d258a1620d 100644 --- a/tests_integration/__tests__/support-info.js +++ b/tests_integration/__tests__/support-info.js @@ -13,6 +13,7 @@ describe("API getSupportInfo()", () => { "1.7.1", "1.8.0", "1.8.2", + "1.16.0", undefined ];