Skip to content

Commit

Permalink
prettierx: apply language-js features based on Prettier 2.3.1
Browse files Browse the repository at this point in the history
(in prettierx-update-branch-001)

based on some updates from:

- #549
- #569

(with very limited update needed in src/language-js/needs-parens.js)

TODO items:

- include test updates from prettierX dev branch
- update documentation

Co-authored-by: Adaline Valentina Simonian <[email protected]>
Co-authored-by: Christopher J. Brody <[email protected]>
  • Loading branch information
brodycj and adalinesimonian committed Jun 17, 2021
1 parent a0052a2 commit ed93030
Show file tree
Hide file tree
Showing 48 changed files with 1,814 additions and 212 deletions.
3 changes: 3 additions & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,11 @@
"yamafaktory",
"Yatharth",
"youtube",
"arijs",
"brodybits",
"Zatorski",
"memberexpr",
"oneline",
"Zosel"
],
"ignoreRegExpList": [
Expand Down
2 changes: 2 additions & 0 deletions src/document/doc-builders.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ function group(contents, opts = {}) {
id: opts.id,
contents,
break: Boolean(opts.shouldBreak),
// [prettierx] for --paren-spacing option support (...)
addedLine: Boolean(opts.addedLine),
expandedStates: opts.expandedStates,
};
}
Expand Down
5 changes: 4 additions & 1 deletion src/language-js/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ function handleIfStatementComments({
enclosingNode,
followingNode,
text,
// [prettierx] support --break-before-else option
options,
}) {
if (
!enclosingNode ||
Expand Down Expand Up @@ -201,7 +203,8 @@ function handleIfStatementComments({
precedingNode === enclosingNode.consequent &&
followingNode === enclosingNode.alternate
) {
if (precedingNode.type === "BlockStatement") {
// [prettierx] --break-before-else option support
if (precedingNode.type === "BlockStatement" && !options.breakBeforeElse) {
addTrailingComment(precedingNode, comment);
} else {
addDanglingComment(enclosingNode, comment);
Expand Down
6 changes: 4 additions & 2 deletions src/language-js/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ function embed(path, print, textToDoc, options) {
}

if (language === "css") {
return formatCss(path, print, textToDoc);
// [prettierx] templateCurlySpacing option support (...)
return formatCss(path, print, textToDoc, options);
}

if (language === "graphql") {
return formatGraphql(path, print, textToDoc);
// [prettierx] templateCurlySpacing option support (...)
return formatGraphql(path, print, textToDoc, options);
}

if (language === "html" || language === "angular") {
Expand Down
6 changes: 4 additions & 2 deletions src/language-js/embed/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const {
} = require("../../document");
const { printTemplateExpressions } = require("../print/template-literal");

function format(path, print, textToDoc) {
// [prettierx] templateCurlySpacing option support (...)
function format(path, print, textToDoc, options) {
const node = path.getValue();

// Get full template literal with expressions replaced by placeholders
Expand All @@ -29,7 +30,8 @@ function format(path, print, textToDoc) {
{ parser: "scss" },
{ stripTrailingHardline: true }
);
const expressionDocs = printTemplateExpressions(path, print);
// [prettierx] templateCurlySpacing option support (...)
const expressionDocs = printTemplateExpressions(path, print, options);
return transformCssDoc(doc, node, expressionDocs);
}

Expand Down
6 changes: 4 additions & 2 deletions src/language-js/embed/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ const {
printTemplateExpressions,
} = require("../print/template-literal");

function format(path, print, textToDoc) {
// [prettierx] with templateCurlySpacing option support (...)
function format(path, print, textToDoc, options) {
const node = path.getValue();

const numQuasis = node.quasis.length;
if (numQuasis === 1 && node.quasis[0].value.raw.trim() === "") {
return "``";
}

const expressionDocs = printTemplateExpressions(path, print);
// [prettierx] templateCurlySpacing option support (...)
const expressionDocs = printTemplateExpressions(path, print, options);
const parts = [];

for (let i = 0; i < numQuasis; i++) {
Expand Down
3 changes: 2 additions & 1 deletion src/language-js/embed/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ function format(path, print, textToDoc, options, { parser }) {
)
.join("");

const expressionDocs = printTemplateExpressions(path, print);
// [prettierx] templateCurlySpacing option support (...)
const expressionDocs = printTemplateExpressions(path, print, options);
if (expressionDocs.length === 0 && text.trim().length === 0) {
return "``";
}
Expand Down
10 changes: 7 additions & 3 deletions src/language-js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ const languages = [
createLanguage(require("linguist-languages/data/JavaScript.json"), () => ({
name: "Flow",
since: "0.0.0",
parsers: ["flow", "babel-flow"],
// [prettierx] must use Babel by default
// (since the flow-parser is now an optional dependency in prettierx)
parsers: ["babel-flow", "flow"],
vscodeLanguageIds: ["javascript"],
aliases: [],
filenames: [],
Expand Down Expand Up @@ -63,12 +65,14 @@ const languages = [
})),
createLanguage(require("linguist-languages/data/TypeScript.json"), () => ({
since: "1.4.0",
parsers: ["typescript", "babel-ts"],
// [prettierx] use babel-ts for TypeScript by default here
parsers: ["babel-ts", "typescript"],
vscodeLanguageIds: ["typescript"],
})),
createLanguage(require("linguist-languages/data/TSX.json"), () => ({
since: "1.4.0",
parsers: ["typescript", "babel-ts"],
// [prettierx] use babel-ts for TypeScript by default here
parsers: ["babel-ts", "typescript"],
vscodeLanguageIds: ["typescriptreact"],
})),
createLanguage(require("linguist-languages/data/JSON.json"), () => ({
Expand Down
3 changes: 2 additions & 1 deletion src/language-js/needs-parens.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ function needsParens(path, options) {
// to avoid unexpected `}}` in HTML interpolations
if (
options.__isInHtmlInterpolation &&
!options.bracketSpacing &&
// [prettierx]: objectCurlySpacing option support
!options.objectCurlySpacing &&
endsWithRightBracket(node) &&
isFollowedByRightBracket(path)
) {
Expand Down
153 changes: 152 additions & 1 deletion src/language-js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,110 @@ module.exports = {
},
],
},
bracketSpacing: commonOptions.bracketSpacing,
// [prettierx ...]
arrayBracketSpacing: {
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: false,
description:
"Put spaces between array brackets (similar to the corresponding eslint option). Status: experimental, with limited testing.",
},
computedPropertySpacing: {
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: false,
description:
"Put spaces between computed property brackets (similar to the corresponding eslint option). Status: experimental, with limited testing.",
},
indentChains: {
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: true,
description: "Put indents at the start of chained calls.",
oppositeDescription: "Disable indents at the start of chained calls.",
},
importFormatting: {
category: CATEGORY_JAVASCRIPT,
type: "choice",
default: "auto",
description:
"Formatting of import statements, may be `oneline` to avoid conflict with" +
' VSCode "Organize Imports" feature.',
choices: [
{
value: "auto",
description: "automatic formatting, like Prettier",
},
{
value: "oneline",
description: "keep import statements on one line",
},
],
},
spaceInParens: {
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: false,
description:
'Print spaces in between parens, WordPress style (similar to the corresponding eslint option). Not recommended in combination with the default `arrowParens: "always"` option. Status: experimental, with limited testing.',
},
spaceUnaryOps: {
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: false,
description:
"Put spaces after unary operator symbols, except in the middle of `!!` (similar to the corresponding eslint option). Status: experimental, with limited testing.",
},
templateCurlySpacing: {
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: false,
description:
"Put spaces between template curly brackets (similar to the corresponding eslint option). Status: experimental, with limited testing.",
},
typeAngleBracketSpacing: {
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: false,
description:
"Put spaces between type angle brackets. Status: experimental, with limited testing.",
},
typeBracketSpacing: {
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: false,
description:
"Print spaces between type brackets. Status: experimental, with limited testing.",
},
exportCurlySpacing: {
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: true,
description: "Put spaces between export curly braces.",
oppositeDescription: "Disable spaces between export curly braces.",
},
importCurlySpacing: {
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: true,
description: "Put spaces between import curly braces.",
oppositeDescription: "Disable spaces between import curly braces.",
},
objectCurlySpacing: {
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: true,
description:
"Put spaces between object curly braces (similar to the corresponding eslint option).",
oppositeDescription: "Disable spaces between object curly braces.",
},
typeCurlySpacing: {
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: true,
description: "Put spaces between type curly braces.",
oppositeDescription: "Disable spaces between type curly braces.",
},
jsxBracketSameLine: {
since: "0.17.0",
category: CATEGORY_JAVASCRIPT,
Expand All @@ -43,6 +146,54 @@ module.exports = {
oppositeDescription:
"Do not print semicolons, except at the beginning of lines which may need them.",
},
// [prettierx ...]
alignObjectProperties: {
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: false,
description: "Align colons in multiline object literals.",
},
offsetTernaryExpressions: {
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: false,
description:
'Indent and align ternary expression branches more consistently with "Standard JS" (similar to the corresponding eslint option).',
},
generatorStarSpacing: {
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: false,
description:
"Put spaces around the star (`*`) in generator functions (before and after - similar to the corresponding eslint option). (Default is after only.)",
},
yieldStarSpacing: {
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: false,
description:
"Put spaces around the star (`*`) in `yield*` expressions (before and after - similar to the corresponding eslint option). (Default is after only.)",
},
spaceBeforeFunctionParen: {
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: false,
description:
"Put a space before function parenthesis in all declarations (similar to the corresponding eslint option). (Default is to put a space before function parenthesis for untyped anonymous functions only.)",
},
breakBeforeElse: {
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: false,
description: "Always add a line break before else.",
},
breakLongMethodChains: {
category: CATEGORY_JAVASCRIPT,
type: "boolean",
default: false,
description:
"Break method chains with more than 3 method calls, like Prettier 1.x.",
},
singleQuote: commonOptions.singleQuote,
jsxSingleQuote: {
since: "1.15.0",
Expand Down
4 changes: 4 additions & 0 deletions src/language-js/parse-postprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ function postprocess(ast, options) {
node.optional = true;
}
break;
case "IfStatement": // [prettierx] quick workaround breakBeforeElse & AST compare test
if (node.consequent.innerComments) {
delete node.consequent.innerComments;
}
}
});

Expand Down
9 changes: 7 additions & 2 deletions src/language-js/print/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,16 @@ function printArray(path, options, print) {
? ifBreak(",", "", { groupId })
: ifBreak(",");

// [prettierx] arrayBracketSpacing option support (...)
parts.push(
group(
// [prettierx] arrayBracketSpacing option support (...)
[
openBracket,
// [prettierx] arrayBracketSpacing option support (...)
indent([
softline,
// [prettierx] arrayBracketSpacing option support (...)
options.arrayBracketSpacing ? line : softline,
shouldUseConciseFormatting
? printArrayItemsConcisely(path, options, print, trailingComma)
: [
Expand All @@ -106,7 +110,8 @@ function printArray(path, options, print) {
],
printDanglingComments(path, options, /* sameIndent */ true),
]),
softline,
// [prettierx] arrayBracketSpacing option support (...)
options.arrayBracketSpacing ? line : softline,
closeBracket,
],
{ shouldBreak, id: groupId }
Expand Down
11 changes: 10 additions & 1 deletion src/language-js/print/binaryish.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ function printBinaryishExpression(path, options, print) {
parent.type === "SwitchStatement" ||
parent.type === "DoWhileStatement");

// [prettierx] spaceInParens option support (...)
const parenLine = options.spaceInParens ? line : softline;

const parts = printBinaryishExpressions(
path,
print,
Expand All @@ -63,6 +66,9 @@ function printBinaryishExpression(path, options, print) {
return parts;
}

// [prettierx] spaceInParens option support
// (...)
//
// Break between the parens in
// unaries or in a member or specific call expression, i.e.
//
Expand All @@ -76,7 +82,10 @@ function printBinaryishExpression(path, options, print) {
parent.type === "UnaryExpression" ||
(isMemberExpression(parent) && !parent.computed)
) {
return group([indent([softline, ...parts]), softline]);
// [prettierx] spaceInParens option support (...)
return group([indent([parenLine, ...parts]), parenLine], {
addedLine: options.spaceInParens,
});
}

// Avoid indenting sub-expressions in some cases where the first sub-expression is already
Expand Down
Loading

0 comments on commit ed93030

Please sign in to comment.