From 282d1017f31319c04d1b2e4e7c6e92dca76945b1 Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Wed, 9 Oct 2019 15:29:37 +0100 Subject: [PATCH] Add support for V8 intrinsics via Babel (#6496) 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 | 25 ++++++++++ src/language-js/parser-babylon.js | 3 +- src/language-js/printer-estree.js | 2 + .../__snapshots__/jsfmt.spec.js.snap | 47 +++++++++++++++++++ tests/v8_intrinsic/intrinsic_call.js | 24 ++++++++++ tests/v8_intrinsic/jsfmt.spec.js | 1 + 6 files changed, 101 insertions(+), 1 deletion(-) 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 71254804..4e84dc42 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -836,6 +836,29 @@ type C = (number | string)["toString"]; type D = (keyof T1)["foo"]; ``` +#### 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 [#6033]: https://github.com/prettier/prettier/pull/6033 [#6186]: https://github.com/prettier/prettier/pull/6186 @@ -863,6 +886,7 @@ type D = (keyof T1)["foo"]; [#6467]: https://github.com/prettier/prettier/pull/6467 [#6377]: https://github.com/prettier/prettier/pull/6377 [#6604]: https://github.com/prettier/prettier/pull/6604 +[#6496]: https://github.com/prettier/prettier/pull/6496 [@brainkim]: https://github.com/brainkim [@duailibe]: https://github.com/duailibe [@gavinjoyce]: https://github.com/gavinjoyce @@ -872,3 +896,4 @@ type D = (keyof T1)["foo"]; [@bakkot]: https://gibhub.com/bakkot [@thorn0]: https://github.com/thorn0 [@dcyriller]: https://github.com/dcyriller +[@rreverser]: https://github.com/RReverser diff --git a/src/language-js/parser-babylon.js b/src/language-js/parser-babylon.js index 2b1a09b0..08b736bd 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/printer-estree.js b/src/language-js/printer-estree.js index 218662ce..202a5727 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -756,6 +756,8 @@ function printPathNoParens(path, options, print, args) { printTypeAnnotation(path, options, print) ]); } + case "V8IntrinsicIdentifier": + return concat(["%", n.name]); case "SpreadElement": case "SpreadElementPattern": case "RestProperty": 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 00000000..0248f069 --- /dev/null +++ b/tests/v8_intrinsic/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,47 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`intrinsic_call.js 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +function doSmth() { + %DebugPrint + ( + foo ) + } + + function printFunc ( + f +) { + if(% + IsAsmWasmCode(f)) console.log("asm.js"); + if( + + % IsWasmCode( + f)) + console.log ( + "wasm" + ); + + console.log + (% + GetFunctioName(f) + ); +} + +=====================================output===================================== +function doSmth() { + %DebugPrint(foo); +} + +function printFunc(f) { + if (%IsAsmWasmCode(f)) console.log("asm.js"); + if (%IsWasmCode(f)) console.log("wasm"); + + console.log(%GetFunctioName(f)); +} + +================================================================================ +`; diff --git a/tests/v8_intrinsic/intrinsic_call.js b/tests/v8_intrinsic/intrinsic_call.js new file mode 100644 index 00000000..43f8bbd7 --- /dev/null +++ b/tests/v8_intrinsic/intrinsic_call.js @@ -0,0 +1,24 @@ +function doSmth() { + %DebugPrint + ( + foo ) + } + + function printFunc ( + f +) { + if(% + IsAsmWasmCode(f)) console.log("asm.js"); + if( + + % IsWasmCode( + f)) + console.log ( + "wasm" + ); + + console.log + (% + GetFunctioName(f) + ); +} diff --git a/tests/v8_intrinsic/jsfmt.spec.js b/tests/v8_intrinsic/jsfmt.spec.js new file mode 100644 index 00000000..8382edde --- /dev/null +++ b/tests/v8_intrinsic/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, ["babel"]);