From 62021b6c3a9ee1bd0e67927d3159569c14ef24ed Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Wed, 18 Sep 2019 14:48:54 +0100 Subject: [PATCH] Add support for V8 intrinsics via Babel This plugin was recently added on Babel side and allows to parse custom syntax for V8 intrinsics. They don't clash with any real-world JavaScript syntax, so adding this option should be as safe as any other plugin, and would allow to format JavaScript that uses such intrinsics (e.g. code inside Node.js or V8 itself). --- CHANGELOG.unreleased.md | 24 +++++++++++++++++++ package.json | 2 +- src/language-js/parser-babylon.js | 3 ++- src/language-js/postprocess.js | 4 ++++ .../__snapshots__/jsfmt.spec.js.snap | 21 ++++++++++++++++ tests/v8_intrinsic/intrinsic_call.js | 5 ++++ tests/v8_intrinsic/jsfmt.spec.js | 1 + yarn.lock | 12 ++++------ 8 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 tests/v8_intrinsic/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/v8_intrinsic/intrinsic_call.js create mode 100644 tests/v8_intrinsic/jsfmt.spec.js diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index 5ea0f757cfcc..eae75585effe 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -598,6 +598,28 @@ export { export { fooooooooooooooooooooooooooooooooooooooooooooooooo } from "fooooooooooooooooooooooooooooo"; ``` +#### JavaScript: Support formatting code with V8 intrinsics. ([#6496] by [@rreverser]) + +```js +// Input +function doSmth() { + %DebugPrint + ( + foo ) + } + +// Prettier (stable) +SyntaxError: Unexpected token (2:13) + 1 | function doSmth() { +> 2 | %DebugPrint + | ^ + +// Prettier (master) +function doSmth() { + %DebugPrint(foo); +} +``` + [#5910]: https://github.com/prettier/prettier/pull/5910 [#6186]: https://github.com/prettier/prettier/pull/6186 [#6206]: https://github.com/prettier/prettier/pull/6206 @@ -619,6 +641,7 @@ export { fooooooooooooooooooooooooooooooooooooooooooooooooo } from "fooooooooooo [#6438]: https://github.com/prettier/prettier/pull/6411 [#6441]: https://github.com/prettier/prettier/pull/6441 [#6446]: https://github.com/prettier/prettier/pull/6446 +[#6496]: https://github.com/prettier/prettier/pull/6496 [@duailibe]: https://github.com/duailibe [@gavinjoyce]: https://github.com/gavinjoyce [@sosukesuzuki]: https://github.com/sosukesuzuki @@ -626,3 +649,4 @@ export { fooooooooooooooooooooooooooooooooooooooooooooooooo } from "fooooooooooo [@jounqin]: https://github.com/JounQin [@bakkot]: https://gibhub.com/bakkot [@thorn0]: https://github.com/thorn0 +[@rreverser]: https://github.com/RReverser diff --git a/package.json b/package.json index 29960fea80ed..5b080ad8db8d 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "dependencies": { "@angular/compiler": "7.2.9", "@babel/code-frame": "7.5.5", - "@babel/parser": "7.2.0", + "@babel/parser": "7.6.0", "@glimmer/syntax": "0.41.0", "@iarna/toml": "2.2.3", "@typescript-eslint/typescript-estree": "1.11.0", diff --git a/src/language-js/parser-babylon.js b/src/language-js/parser-babylon.js index 2b1a09b032db..08b736bdbaac 100644 --- a/src/language-js/parser-babylon.js +++ b/src/language-js/parser-babylon.js @@ -37,7 +37,8 @@ function babelOptions(extraOptions, extraPlugins) { "bigInt", "throwExpressions", "logicalAssignment", - "classPrivateMethods" + "classPrivateMethods", + "v8intrinsic" ].concat(extraPlugins) }, extraOptions diff --git a/src/language-js/postprocess.js b/src/language-js/postprocess.js index cdd12d36997f..4e835839e3d5 100644 --- a/src/language-js/postprocess.js +++ b/src/language-js/postprocess.js @@ -6,6 +6,10 @@ const { getLast } = require("../common/util"); function postprocess(ast, options) { visitNode(ast, node => { switch (node.type) { + case "V8IntrinsicIdentifier": + node.name = `%${node.name}`; + node.type = "Identifier"; + break; case "VariableDeclaration": { const lastDeclaration = getLast(node.declarations); if (lastDeclaration && lastDeclaration.init) { diff --git a/tests/v8_intrinsic/__snapshots__/jsfmt.spec.js.snap b/tests/v8_intrinsic/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000000..0d23c5481c37 --- /dev/null +++ b/tests/v8_intrinsic/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,21 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`intrinsic_call.js 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +function doSmth() { + %DebugPrint + ( + foo ) + } + +=====================================output===================================== +function doSmth() { + %DebugPrint(foo); +} + +================================================================================ +`; diff --git a/tests/v8_intrinsic/intrinsic_call.js b/tests/v8_intrinsic/intrinsic_call.js new file mode 100644 index 000000000000..f1295de4d241 --- /dev/null +++ b/tests/v8_intrinsic/intrinsic_call.js @@ -0,0 +1,5 @@ +function doSmth() { + %DebugPrint + ( + foo ) + } diff --git a/tests/v8_intrinsic/jsfmt.spec.js b/tests/v8_intrinsic/jsfmt.spec.js new file mode 100644 index 000000000000..8382eddeb1db --- /dev/null +++ b/tests/v8_intrinsic/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, ["babel"]); diff --git a/yarn.lock b/yarn.lock index 75634e4abe05..e0b45bf3a3c9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -282,9 +282,10 @@ esutils "^2.0.2" js-tokens "^4.0.0" -"@babel/parser@7.2.0": - version "7.2.0" - resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.2.0.tgz#02d01dbc330b6cbf36b76ac93c50752c69027065" +"@babel/parser@7.6.0", "@babel/parser@^7.6.0": + version "7.6.0" + resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.6.0.tgz#3e05d0647432a8326cb28d0de03895ae5a57f39b" + integrity sha512-+o2q111WEx4srBs7L9eJmcwi655eD8sXniLqMB93TBK9GrNzGrxDWSjiqz2hLU0Ha8MTXFIP0yd9fNdP+m43ZQ== "@babel/parser@^7.1.2": version "7.1.2" @@ -304,11 +305,6 @@ resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.5.5.tgz#02f077ac8817d3df4a832ef59de67565e71cca4b" integrity sha512-E5BN68cqR7dhKan1SfqgPGhQ178bkVKpXTPEXnFJBrEt8/DKRZlybmy+IgYLTeN7tp1R5Ccmbm2rBk17sHYU3g== -"@babel/parser@^7.6.0": - version "7.6.0" - resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.6.0.tgz#3e05d0647432a8326cb28d0de03895ae5a57f39b" - integrity sha512-+o2q111WEx4srBs7L9eJmcwi655eD8sXniLqMB93TBK9GrNzGrxDWSjiqz2hLU0Ha8MTXFIP0yd9fNdP+m43ZQ== - "@babel/plugin-proposal-async-generator-functions@^7.2.0": version "7.2.0" resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-async-generator-functions/-/plugin-proposal-async-generator-functions-7.2.0.tgz#b289b306669dce4ad20b0252889a15768c9d417e"