diff --git a/src/language-js/utils/index.js b/src/language-js/utils/index.js index 9247b5aff..6e808ed09 100644 --- a/src/language-js/utils/index.js +++ b/src/language-js/utils/index.js @@ -883,9 +883,26 @@ function isSimpleCallArgument(node, depth) { ); } + const targetUnaryExpressionOperators = { + "!": true, + "-": true, + "+": true, + "~": true, + }; if ( node.type === "UnaryExpression" && - (node.operator === "!" || node.operator === "-") + targetUnaryExpressionOperators[node.operator] + ) { + return isSimpleCallArgument(node.argument, depth); + } + + const targetUpdateExpressionOperators = { + "++": true, + "--": true, + }; + if ( + node.type === "UpdateExpression" && + targetUpdateExpressionOperators[node.operator] ) { return isSimpleCallArgument(node.argument, depth); } diff --git a/tests/format/js/method-chain/13018.js b/tests/format/js/method-chain/13018.js new file mode 100644 index 000000000..a0a4dc5eb --- /dev/null +++ b/tests/format/js/method-chain/13018.js @@ -0,0 +1,8 @@ +foo(_a).bar().leet(); +foo(-a).bar().leet(); +foo(+a).bar().leet(); +foo(~a).bar().leet(); +foo(++a).bar().leet(); +foo(--a).bar().leet(); +foo(a++).bar().leet(); +foo(a--).bar().leet(); diff --git a/tests/format/js/method-chain/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/method-chain/__snapshots__/jsfmt.spec.js.snap index 4086bba33..8aee2b6a2 100644 --- a/tests/format/js/method-chain/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/js/method-chain/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,33 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`13018.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +foo(_a).bar().leet(); +foo(-a).bar().leet(); +foo(+a).bar().leet(); +foo(~a).bar().leet(); +foo(++a).bar().leet(); +foo(--a).bar().leet(); +foo(a++).bar().leet(); +foo(a--).bar().leet(); + +=====================================output===================================== +foo(_a).bar().leet(); +foo(-a).bar().leet(); +foo(+a).bar().leet(); +foo(~a).bar().leet(); +foo(++a).bar().leet(); +foo(--a).bar().leet(); +foo(a++).bar().leet(); +foo(a--).bar().leet(); + +================================================================================ +`; + exports[`bracket_0.js format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] diff --git a/website/versioned_docs/version-stable/api.md b/website/versioned_docs/version-stable/api.md index 958d0bae2..a8288f272 100644 --- a/website/versioned_docs/version-stable/api.md +++ b/website/versioned_docs/version-stable/api.md @@ -133,18 +133,19 @@ The support information looks like this: } ``` -## Custom Parser API + -If you need to make modifications to the AST (such as codemods), or you want to provide an alternate parser, you can do so by setting the `parser` option to a function. The function signature of the parser function is: +## Custom Parser API (deprecated) + +_Will be removed in v3.0.0 (superseded by the Plugin API)_ + +Before [plugins](plugins.md) were a thing, Prettier had a similar but more limited feature called custom parsers. It will be removed in v3.0.0 as its functionality is a subset of what the Plugin API does. If you used it, please check the example below on how to migrate. + +❌ Custom parser API (deprecated): ```js -(text: string, parsers: object, options: object) => AST; -``` - -Prettier’s built-in parsers are exposed as properties on the `parsers` argument. - -```js -prettier.format("lodash ( )", { +import { format } from "prettier"; +format("lodash ( )", { parser(text, { babel }) { const ast = babel(text); ast.program.body[0].expression.callee.name = "_"; @@ -154,4 +155,30 @@ prettier.format("lodash ( )", { // -> "_();\n" ``` -The `--parser` CLI option may be a path to a node.js module exporting a parse function. +✔️ Plugin API: + +```js +import { format } from "prettier"; +import parserBabel from "prettier/parser-babel.js"; +const myCustomPlugin = { + parsers: { + "my-custom-parser": { + parse(text) { + const ast = parserBabel.parsers.babel.parse(text); + ast.program.body[0].expression.callee.name = "_"; + return ast; + }, + astFormat: "estree", + }, + }, +}; +format("lodash ( )", { + parser: "my-custom-parser", + plugins: [myCustomPlugin], +}); +// -> "_();\n" +``` + +> Note: Overall, doing codemods this way isn’t recommended. Prettier uses the location data of AST nodes for many things like preserving blank lines and attaching comments. When the AST is modified after the parsing, the location data often gets out of sync, which may lead to unpredictable results. Consider using [jscodeshift](https://github.com/facebook/jscodeshift) if you need codemods. + +As part of the deprecated Custom parser API, it is possible to pass a path to a module exporting a `parse` function via the `--parser` option. Use the `--plugin` CLI option or the `plugins` API option instead to [load plugins](plugins.md#using-plugins). diff --git a/website/versioned_docs/version-stable/options.md b/website/versioned_docs/version-stable/options.md index 2b8a012ba..a1c39b50d 100644 --- a/website/versioned_docs/version-stable/options.md +++ b/website/versioned_docs/version-stable/options.md @@ -282,7 +282,7 @@ Valid options: - `"lwc"` (same parser as `"html"`, but also formats LWC-specific syntax for unquoted template attributes) _First available in 1.17.0_ - `"yaml"` (via [yaml](https://github.com/eemeli/yaml) and [yaml-unist-parser](https://github.com/ikatyang/yaml-unist-parser)) _First available in 1.14.0_ -[Custom parsers](api.md#custom-parser-api) are also supported. _First available in v1.5.0_ +[Custom parsers](api.md#custom-parser-api) are also supported. _First available in v1.5.0. Deprecated. Will be removed in v3.0.0 (superseded by the Plugin API)_ | Default | CLI Override | API Override | | ------- | ----------------------------------------------- | ---------------------------------------------------------- |