Skip to content

Commit

Permalink
[prettierx] feat: apply language-js features (...)
Browse files Browse the repository at this point in the history
into prettierx 0.19.0-01-update-branch

based on some updates from:

- #549
- #569
- #603

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

and with comments where updates from 0.18.x are NO LONGER NEEDED in
src/language-js/needs-parens.js

tested with prettierX-specific formatting test cases from prettierX
0.18.x (discovered a very limited number of deviations)

TODO items:

- include test updates from prettierX dev branch
- update documentation

Co-authored-by: Adaline Valentina Simonian <adalinesimonian@gmail.com>
Co-authored-by: Christopher J. Brody <chris.brody+brodybits@gmail.com>
  • Loading branch information
brodybits and adalinesimonian committed Jul 1, 2021
1 parent a6cf807 commit a0eb2be
Show file tree
Hide file tree
Showing 48 changed files with 1,817 additions and 211 deletions.
3 changes: 3 additions & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,11 @@
"Yatharth",
"youtube",
"Brody",
"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 --space-in-parens 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] --template-curly-spacing option support (...)
return formatCss(path, print, textToDoc, options);
}

if (language === "graphql") {
return formatGraphql(path, print, textToDoc);
// [prettierx] --template-curly-spacing 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] --template-curly-spacing 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] --template-curly-spacing 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 --template-curly-spacing 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] --template-curly-spacing 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] --template-curly-spacing 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
8 changes: 7 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 Expand Up @@ -255,6 +256,7 @@ function needsParens(path, options) {
case "CallExpression":
case "NewExpression":
case "OptionalCallExpression":
// [prettierx] --space-in-parens option support NO LONGER NEEDED HERE
return name === "callee";

case "ClassExpression":
Expand All @@ -271,10 +273,12 @@ function needsParens(path, options) {
case "AwaitExpression":
case "TSNonNullExpression":
case "UpdateExpression":
// [prettierx] --space-in-parens option support NO LONGER NEEDED HERE
return true;

case "MemberExpression":
case "OptionalMemberExpression":
// [prettierx] --space-in-parens option support NO LONGER NEEDED HERE
return name === "object";

case "AssignmentExpression":
Expand Down Expand Up @@ -531,6 +535,7 @@ function needsParens(path, options) {
}

return (
// [prettierx] --space-in-parens option support NO LONGER NEEDED HERE
name === "object" &&
parent.type === "MemberExpression" &&
typeof node.value === "number"
Expand Down Expand Up @@ -755,6 +760,7 @@ function needsParens(path, options) {
(isCallExpression(parent) && parent.arguments[name] === node) ||
(name === "right" && parent.type === "NGPipeExpression") ||
(name === "property" && parent.type === "MemberExpression") ||
// [prettierx] --space-in-parens option support NO LONGER NEEDED HERE
parent.type === "AssignmentExpression"
) {
return false;
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 @@ -160,6 +160,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

0 comments on commit a0eb2be

Please sign in to comment.