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).
master
Ingvar Stepanyan 2019-10-09 15:29:37 +01:00 committed by Christopher Chedeau
parent 1cc92752e5
commit 282d1017f3
6 changed files with 101 additions and 1 deletions

View File

@ -836,6 +836,29 @@ type C = (number | string)["toString"];
type D = (keyof T1)["foo"];
```
#### JavaScript: Support formatting code with V8 intrinsics. ([#6496] by [@rreverser])
<!-- prettier-ignore -->
```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

View File

@ -37,7 +37,8 @@ function babelOptions(extraOptions, extraPlugins) {
"bigInt",
"throwExpressions",
"logicalAssignment",
"classPrivateMethods"
"classPrivateMethods",
"v8intrinsic"
].concat(extraPlugins)
},
extraOptions

View File

@ -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":

View File

@ -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));
}
================================================================================
`;

View File

@ -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)
);
}

View File

@ -0,0 +1 @@
run_spec(__dirname, ["babel"]);