Skip to content

Commit

Permalink
Introduce standard.js compatibility flag
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerun committed Jan 19, 2019
1 parent 633a12a commit 96ab928
Show file tree
Hide file tree
Showing 20 changed files with 2,493 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ Use single quotes instead of double quotes in JSX.
| ------- | -------------------- | ------------------------ |
| `false` | `--jsx-single-quote` | `jsxSingleQuote: <bool>` |

## Standard.js compatibility

Configure prettier to be compatible with Standard.js

| Default | CLI Override | API Override |
| ------- | ------------ | ------------------ |
| `false` | `--standard` | `standard: <bool>` |

## Trailing Commas

Print trailing commas wherever possible when multi-line. (A single-line array, for example, never gets trailing commas.)
Expand Down
7 changes: 7 additions & 0 deletions src/language-js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,12 @@ module.exports = {
{ value: true, deprecated: "0.19.0", redirect: "es5" },
{ value: false, deprecated: "0.19.0", redirect: "none" }
]
},
standard: {
since: "1.16.0",
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: false,
description: "Use standard rules for formatting."
}
};
17 changes: 17 additions & 0 deletions src/language-js/printer-estree.js
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,9 @@ function printPathNoParens(path, options, print, args) {
parts.push("yield");

if (n.delegate) {
if (options.standard) {
parts.push(" ");
}
parts.push("*");
}
if (n.argument) {
Expand Down Expand Up @@ -3723,6 +3726,9 @@ function printMethod(path, options, print) {
if (!kind || kind === "init" || kind === "method" || kind === "constructor") {
if (node.value.generator) {
parts.push("*");
if (options.standard) {
parts.push(" ");
}
}
} else {
assert.ok(kind === "get" || kind === "set");
Expand All @@ -3744,6 +3750,7 @@ function printMethod(path, options, print) {
printFunctionTypeParameters(valuePath, options, print),
group(
concat([
options.standard ? " " : "",
printFunctionParams(valuePath, print, options),
printReturnType(valuePath, print, options)
])
Expand Down Expand Up @@ -4224,6 +4231,9 @@ function printFunctionDeclaration(path, print, options) {
parts.push("function");

if (n.generator) {
if (options.standard) {
parts.push(" ");
}
parts.push("*");
}
if (n.id) {
Expand All @@ -4234,6 +4244,7 @@ function printFunctionDeclaration(path, print, options) {
printFunctionTypeParameters(path, options, print),
group(
concat([
options.standard ? " " : "",
printFunctionParams(path, print, options),
printReturnType(path, print, options)
])
Expand All @@ -4254,6 +4265,9 @@ function printObjectMethod(path, options, print) {
}
if (objMethod.generator) {
parts.push("*");
if (options.standard) {
parts.push(" ");
}
}
if (
objMethod.method ||
Expand All @@ -4269,6 +4283,9 @@ function printObjectMethod(path, options, print) {
parts.push("[", key, "]");
} else {
parts.push(key);
if (options.standard) {
parts.push(" ");
}
}

parts.push(
Expand Down
6 changes: 6 additions & 0 deletions src/main/ast-to-doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ const docUtils = doc.utils;
* the path to the current node through the Abstract Syntax Tree.
*/
function printAstToDoc(ast, options, alignmentSize = 0) {
if (options.standard) {
options.semi = false;
options.singleQuote = true;
options.jsxSingleQuote = true;
}

const printer = options.printer;

if (printer.preprocess) {
Expand Down

0 comments on commit 96ab928

Please sign in to comment.