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"]);