Add lint script, run it on Travis, prettify source code (#1634)
* Add `lint` script with eslint-plugin-prettier, use it in `format:all` See https://github.com/prettier/prettier/pull/1634#issuecomment-302945967 * Run `lint` script on Travis * Run Prettier on its own source code This was done with npm run format:all
This commit is contained in:
1 parent
569380155b
commit
d7aaaee0dd
14 files changed
+893
-324
No files matched your search
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"parserOptions": {
|
||||||
|
"ecmaVersion": "2015"
|
||||||
|
},
|
||||||
|
"plugins": [
|
||||||
|
"prettier"
|
||||||
|
],
|
||||||
|
"rules": {
|
||||||
|
"prettier/prettier": "error"
|
||||||
|
}
|
||||||
|
}
|
||||||
+3
-1
@@ -8,4 +8,6 @@ cache:
|
|||||||
yarn: true
|
yarn: true
|
||||||
directories:
|
directories:
|
||||||
- node_modules
|
- node_modules
|
||||||
script: npm test -- --runInBand
|
script:
|
||||||
|
- npm run lint
|
||||||
|
- npm test -- --runInBand
|
||||||
+23
-11
@@ -9,7 +9,7 @@ const chalk = require("chalk");
|
|||||||
const minimist = require("minimist");
|
const minimist = require("minimist");
|
||||||
const readline = require("readline");
|
const readline = require("readline");
|
||||||
const prettier = require("../index");
|
const prettier = require("../index");
|
||||||
const cleanAST = require('../src/clean-ast.js').cleanAST;
|
const cleanAST = require("../src/clean-ast.js").cleanAST;
|
||||||
|
|
||||||
const argv = minimist(process.argv.slice(2), {
|
const argv = minimist(process.argv.slice(2), {
|
||||||
boolean: [
|
boolean: [
|
||||||
@@ -33,8 +33,20 @@ const argv = minimist(process.argv.slice(2), {
|
|||||||
// Deprecated in 0.0.10
|
// Deprecated in 0.0.10
|
||||||
"flow-parser"
|
"flow-parser"
|
||||||
],
|
],
|
||||||
string: ["print-width", "tab-width", "parser", "trailing-comma", "range-start", "range-end"],
|
string: [
|
||||||
default: { semi: true, color: true, "bracket-spacing": true, parser: "babylon" },
|
"print-width",
|
||||||
|
"tab-width",
|
||||||
|
"parser",
|
||||||
|
"trailing-comma",
|
||||||
|
"range-start",
|
||||||
|
"range-end"
|
||||||
|
],
|
||||||
|
default: {
|
||||||
|
semi: true,
|
||||||
|
color: true,
|
||||||
|
"bracket-spacing": true,
|
||||||
|
parser: "babylon"
|
||||||
|
},
|
||||||
alias: { help: "h", version: "v", "list-different": "l" },
|
alias: { help: "h", version: "v", "list-different": "l" },
|
||||||
unknown: param => {
|
unknown: param => {
|
||||||
if (param.startsWith("-")) {
|
if (param.startsWith("-")) {
|
||||||
@@ -148,8 +160,9 @@ function format(input) {
|
|||||||
|
|
||||||
if (argv["debug-check"]) {
|
if (argv["debug-check"]) {
|
||||||
function diff(a, b) {
|
function diff(a, b) {
|
||||||
return require("diff")
|
return require("diff").createTwoFilesPatch("", "", a, b, "", "", {
|
||||||
.createTwoFilesPatch("", "", a, b, "", "", { context: 2 });
|
context: 2
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const pp = prettier.format(input, options);
|
const pp = prettier.format(input, options);
|
||||||
@@ -162,14 +175,13 @@ function format(input) {
|
|||||||
|
|
||||||
if (ast !== past) {
|
if (ast !== past) {
|
||||||
const MAX_AST_SIZE = 2097152; // 2MB
|
const MAX_AST_SIZE = 2097152; // 2MB
|
||||||
const astDiff = (ast.length > MAX_AST_SIZE || past.length > MAX_AST_SIZE)
|
const astDiff = ast.length > MAX_AST_SIZE || past.length > MAX_AST_SIZE
|
||||||
? "AST diff too large to render"
|
? "AST diff too large to render"
|
||||||
: diff(ast, past);
|
: diff(ast, past);
|
||||||
throw (
|
throw "ast(input) !== ast(prettier(input))\n" +
|
||||||
"ast(input) !== ast(prettier(input))\n" +
|
astDiff +
|
||||||
astDiff + "\n" +
|
"\n" +
|
||||||
diff(input, pp)
|
diff(input, pp);
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
|||||||
+5
-1
@@ -23,8 +23,11 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"diff": "3.2.0",
|
"diff": "3.2.0",
|
||||||
|
"eslint": "^3.19.0",
|
||||||
|
"eslint-plugin-prettier": "^2.1.1",
|
||||||
"jest": "20.0.0",
|
"jest": "20.0.0",
|
||||||
"mkdirp": "^0.5.1",
|
"mkdirp": "^0.5.1",
|
||||||
|
"prettier": "^1.3.1",
|
||||||
"rimraf": "^2.6.1",
|
"rimraf": "^2.6.1",
|
||||||
"rollup": "0.41.1",
|
"rollup": "0.41.1",
|
||||||
"rollup-plugin-commonjs": "7.0.0",
|
"rollup-plugin-commonjs": "7.0.0",
|
||||||
@@ -39,7 +42,8 @@
|
|||||||
"test": "jest",
|
"test": "jest",
|
||||||
"format": "./bin/prettier.js --write",
|
"format": "./bin/prettier.js --write",
|
||||||
"format:single": "npm run format -- src/printer.js",
|
"format:single": "npm run format -- src/printer.js",
|
||||||
"format:all": "npm run format -- index.js \"{src,bin,tests_config,scripts}/*.js\" \"tests/**/jsfmt.spec.js\"",
|
"format:all": "npm run lint -- --fix",
|
||||||
|
"lint": "eslint index.js \"{src,bin,tests_config,scripts}/*.js\" \"tests/**/jsfmt.spec.js\"",
|
||||||
"build:docs": "rollup -c docs/rollup.config.js"
|
"build:docs": "rollup -c docs/rollup.config.js"
|
||||||
},
|
},
|
||||||
"jest": {
|
"jest": {
|
||||||
|
|||||||
+1
-1
@@ -59,7 +59,7 @@ function massageAST(ast) {
|
|||||||
if (
|
if (
|
||||||
(ast.type === "Property" ||
|
(ast.type === "Property" ||
|
||||||
ast.type === "MethodDefinition" ||
|
ast.type === "MethodDefinition" ||
|
||||||
ast.type === "ClassProperty") &&
|
ast.type === "ClassProperty") &&
|
||||||
typeof ast.key === "object" &&
|
typeof ast.key === "object" &&
|
||||||
ast.key &&
|
ast.key &&
|
||||||
(ast.key.type === "Literal" || ast.key.type === "Identifier")
|
(ast.key.type === "Literal" || ast.key.type === "Identifier")
|
||||||
|
|||||||
+8
-3
@@ -403,7 +403,9 @@ function handleIfStatementComments(
|
|||||||
comment
|
comment
|
||||||
) {
|
) {
|
||||||
if (
|
if (
|
||||||
!enclosingNode || enclosingNode.type !== "IfStatement" || !followingNode
|
!enclosingNode ||
|
||||||
|
enclosingNode.type !== "IfStatement" ||
|
||||||
|
!followingNode
|
||||||
) {
|
) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -435,7 +437,9 @@ function handleIfStatementComments(
|
|||||||
// Same as IfStatement but for TryStatement
|
// Same as IfStatement but for TryStatement
|
||||||
function handleTryStatementComments(enclosingNode, followingNode, comment) {
|
function handleTryStatementComments(enclosingNode, followingNode, comment) {
|
||||||
if (
|
if (
|
||||||
!enclosingNode || enclosingNode.type !== "TryStatement" || !followingNode
|
!enclosingNode ||
|
||||||
|
enclosingNode.type !== "TryStatement" ||
|
||||||
|
!followingNode
|
||||||
) {
|
) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -903,7 +907,8 @@ function printComments(path, print, options, needsSemi) {
|
|||||||
const value = path.getValue();
|
const value = path.getValue();
|
||||||
const parent = path.getParentNode();
|
const parent = path.getParentNode();
|
||||||
const printed = print(path);
|
const printed = print(path);
|
||||||
const comments = n.Node.check(value) && types.getFieldValue(value, "comments");
|
const comments =
|
||||||
|
n.Node.check(value) && types.getFieldValue(value, "comments");
|
||||||
|
|
||||||
if (!comments || comments.length === 0) {
|
if (!comments || comments.length === 0) {
|
||||||
return printed;
|
return printed;
|
||||||
|
|||||||
+1
-4
@@ -102,10 +102,7 @@ function printDoc(doc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (doc.type === "fill") {
|
if (doc.type === "fill") {
|
||||||
return ("fill") +
|
return "fill" + "(" + doc.parts.map(printDoc).join(", ") + ")";
|
||||||
"(" +
|
|
||||||
doc.parts.map(printDoc).join(", ") +
|
|
||||||
")";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (doc.type === "line-suffix") {
|
if (doc.type === "line-suffix") {
|
||||||
|
|||||||
+16
-8
@@ -236,7 +236,6 @@ function printDocToString(doc, options) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Fills each line with as much code as possible before moving to a new
|
// Fills each line with as much code as possible before moving to a new
|
||||||
// line with the same indentation.
|
// line with the same indentation.
|
||||||
//
|
//
|
||||||
@@ -268,7 +267,7 @@ function printDocToString(doc, options) {
|
|||||||
const content = parts[0];
|
const content = parts[0];
|
||||||
const contentFlatCmd = [ind, MODE_FLAT, content];
|
const contentFlatCmd = [ind, MODE_FLAT, content];
|
||||||
const contentBreakCmd = [ind, MODE_BREAK, content];
|
const contentBreakCmd = [ind, MODE_BREAK, content];
|
||||||
const contentFits = fits(contentFlatCmd, [], width - rem, true)
|
const contentFits = fits(contentFlatCmd, [], width - rem, true);
|
||||||
|
|
||||||
if (parts.length === 1) {
|
if (parts.length === 1) {
|
||||||
if (contentFits) {
|
if (contentFits) {
|
||||||
@@ -280,8 +279,8 @@ function printDocToString(doc, options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const whitespace = parts[1];
|
const whitespace = parts[1];
|
||||||
const whitespaceFlatCmd = [ind, MODE_FLAT, whitespace]
|
const whitespaceFlatCmd = [ind, MODE_FLAT, whitespace];
|
||||||
const whitespaceBreakCmd = [ind, MODE_BREAK, whitespace]
|
const whitespaceBreakCmd = [ind, MODE_BREAK, whitespace];
|
||||||
|
|
||||||
if (parts.length === 2) {
|
if (parts.length === 2) {
|
||||||
if (contentFits) {
|
if (contentFits) {
|
||||||
@@ -298,15 +297,24 @@ function printDocToString(doc, options) {
|
|||||||
const remainingCmd = [ind, mode, fill(remaining)];
|
const remainingCmd = [ind, mode, fill(remaining)];
|
||||||
|
|
||||||
const secondContent = parts[2];
|
const secondContent = parts[2];
|
||||||
const firstAndSecondContentFlatCmd = [ind, MODE_FLAT, concat([content, whitespace, secondContent])];
|
const firstAndSecondContentFlatCmd = [
|
||||||
const firstAndSecondContentFits = fits(firstAndSecondContentFlatCmd, [], rem, true);
|
ind,
|
||||||
|
MODE_FLAT,
|
||||||
|
concat([content, whitespace, secondContent])
|
||||||
|
];
|
||||||
|
const firstAndSecondContentFits = fits(
|
||||||
|
firstAndSecondContentFlatCmd,
|
||||||
|
[],
|
||||||
|
rem,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
if (firstAndSecondContentFits) {
|
if (firstAndSecondContentFits) {
|
||||||
cmds.push(remainingCmd)
|
cmds.push(remainingCmd);
|
||||||
cmds.push(whitespaceFlatCmd);
|
cmds.push(whitespaceFlatCmd);
|
||||||
cmds.push(contentFlatCmd);
|
cmds.push(contentFlatCmd);
|
||||||
} else if (contentFits) {
|
} else if (contentFits) {
|
||||||
cmds.push(remainingCmd)
|
cmds.push(remainingCmd);
|
||||||
cmds.push(whitespaceBreakCmd);
|
cmds.push(whitespaceBreakCmd);
|
||||||
cmds.push(contentFlatCmd);
|
cmds.push(contentFlatCmd);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+6
-3
@@ -69,7 +69,7 @@ function getNodeHelper(path, count) {
|
|||||||
for (let i = s.length - 1; i >= 0; i -= 2) {
|
for (let i = s.length - 1; i >= 0; i -= 2) {
|
||||||
const value = s[i];
|
const value = s[i];
|
||||||
|
|
||||||
if ((n.Node.check(value)) && --count < 0) {
|
if (n.Node.check(value) && --count < 0) {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -449,7 +449,7 @@ FPp.needsParens = function() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
case "AssignmentExpression": {
|
case "AssignmentExpression": {
|
||||||
const grandParent = this.getParentNode(1)
|
const grandParent = this.getParentNode(1);
|
||||||
|
|
||||||
if (parent.type === "ArrowFunctionExpression" && parent.body === node) {
|
if (parent.type === "ArrowFunctionExpression" && parent.body === node) {
|
||||||
return true;
|
return true;
|
||||||
@@ -459,7 +459,10 @@ FPp.needsParens = function() {
|
|||||||
parent.computed
|
parent.computed
|
||||||
) {
|
) {
|
||||||
return false;
|
return false;
|
||||||
} else if (parent.type === "TSPropertySignature" && parent.name === node) {
|
} else if (
|
||||||
|
parent.type === "TSPropertySignature" &&
|
||||||
|
parent.name === node
|
||||||
|
) {
|
||||||
return false;
|
return false;
|
||||||
} else if (
|
} else if (
|
||||||
parent.type === "ForStatement" &&
|
parent.type === "ForStatement" &&
|
||||||
|
|||||||
+10
-11
@@ -103,12 +103,8 @@ function parseWithTypeScript(text) {
|
|||||||
// But if we get it wrong, try the opposite.
|
// But if we get it wrong, try the opposite.
|
||||||
return tryParseTypeScript(text, !jsx);
|
return tryParseTypeScript(text, !jsx);
|
||||||
}
|
}
|
||||||
} catch(e) {
|
} catch (e) {
|
||||||
throw createError(
|
throw createError(e.message, e.lineNumber, e.column);
|
||||||
e.message,
|
|
||||||
e.lineNumber,
|
|
||||||
e.column
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,11 +127,14 @@ function tryParseTypeScript(text, jsx) {
|
|||||||
* https://github.com/prettier/prettier/issues/1538
|
* https://github.com/prettier/prettier/issues/1538
|
||||||
*/
|
*/
|
||||||
function isProbablyJsx(text) {
|
function isProbablyJsx(text) {
|
||||||
return new RegExp([
|
return new RegExp(
|
||||||
"(^[^\"'`]*</)", // Contains "</" when probably not in a string
|
[
|
||||||
"|",
|
"(^[^\"'`]*</)", // Contains "</" when probably not in a string
|
||||||
"(^[^/]{2}.*\/>)" // Contains "/>" on line not starting with "//"
|
"|",
|
||||||
].join(""), "m").test(text);
|
"(^[^/]{2}.*\/>)" // Contains "/>" on line not starting with "//"
|
||||||
|
].join(""),
|
||||||
|
"m"
|
||||||
|
).test(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { parse };
|
module.exports = { parse };
|
||||||
+234
-257
@@ -67,7 +67,12 @@ function genericPrint(path, options, printPath, args) {
|
|||||||
|
|
||||||
const parts = [];
|
const parts = [];
|
||||||
let needsParens = false;
|
let needsParens = false;
|
||||||
const linesWithoutParens = genericPrintNoParens(path, options, printPath, args);
|
const linesWithoutParens = genericPrintNoParens(
|
||||||
|
path,
|
||||||
|
options,
|
||||||
|
printPath,
|
||||||
|
args
|
||||||
|
);
|
||||||
|
|
||||||
if (!node || isEmpty(linesWithoutParens)) {
|
if (!node || isEmpty(linesWithoutParens)) {
|
||||||
return linesWithoutParens;
|
return linesWithoutParens;
|
||||||
@@ -185,14 +190,7 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
case "ExpressionStatement":
|
case "ExpressionStatement":
|
||||||
// Detect Flow-parsed directives
|
// Detect Flow-parsed directives
|
||||||
if (n.directive) {
|
if (n.directive) {
|
||||||
return concat([
|
return concat([nodeStr(n.expression, options, true), semi]);
|
||||||
nodeStr(
|
|
||||||
n.expression,
|
|
||||||
options,
|
|
||||||
true
|
|
||||||
),
|
|
||||||
semi
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
return concat([path.call(print, "expression"), semi]); // Babel extension.
|
return concat([path.call(print, "expression"), semi]); // Babel extension.
|
||||||
case "ParenthesizedExpression":
|
case "ParenthesizedExpression":
|
||||||
@@ -285,16 +283,15 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
firstNonMemberParent = path.getParentNode(i);
|
firstNonMemberParent = path.getParentNode(i);
|
||||||
i++;
|
i++;
|
||||||
} while (
|
} while (
|
||||||
firstNonMemberParent &&
|
firstNonMemberParent && firstNonMemberParent.type === "MemberExpression"
|
||||||
firstNonMemberParent.type === "MemberExpression"
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const shouldInline =
|
const shouldInline =
|
||||||
firstNonMemberParent && (
|
(firstNonMemberParent &&
|
||||||
(firstNonMemberParent.type === "VariableDeclarator" &&
|
((firstNonMemberParent.type === "VariableDeclarator" &&
|
||||||
firstNonMemberParent.id.type !== "Identifier") ||
|
firstNonMemberParent.id.type !== "Identifier") ||
|
||||||
(firstNonMemberParent.type === "AssignmentExpression" &&
|
(firstNonMemberParent.type === "AssignmentExpression" &&
|
||||||
firstNonMemberParent.left.type !== "Identifier")) ||
|
firstNonMemberParent.left.type !== "Identifier"))) ||
|
||||||
n.computed ||
|
n.computed ||
|
||||||
(n.object.type === "Identifier" &&
|
(n.object.type === "Identifier" &&
|
||||||
n.property.type === "Identifier" &&
|
n.property.type === "Identifier" &&
|
||||||
@@ -328,15 +325,14 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
case "Path":
|
case "Path":
|
||||||
return join(".", n.body);
|
return join(".", n.body);
|
||||||
case "Identifier": {
|
case "Identifier": {
|
||||||
const parentNode = path.getParentNode()
|
const parentNode = path.getParentNode();
|
||||||
const isFunctionDeclarationIdentifier =
|
const isFunctionDeclarationIdentifier =
|
||||||
parentNode.type === 'DeclareFunction' &&
|
parentNode.type === "DeclareFunction" && parentNode.id === n;
|
||||||
parentNode.id === n;
|
|
||||||
|
|
||||||
return concat([
|
return concat([
|
||||||
n.name,
|
n.name,
|
||||||
n.optional ? "?" : "",
|
n.optional ? "?" : "",
|
||||||
(n.typeAnnotation && !isFunctionDeclarationIdentifier) ? ": " : "",
|
n.typeAnnotation && !isFunctionDeclarationIdentifier ? ": " : "",
|
||||||
path.call(print, "typeAnnotation")
|
path.call(print, "typeAnnotation")
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
@@ -773,11 +769,7 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (n.heritage.length) {
|
if (n.heritage.length) {
|
||||||
parts.push(
|
parts.push("extends ", join(", ", path.map(print, "heritage")), " ");
|
||||||
"extends ",
|
|
||||||
join(", ", path.map(print, "heritage")),
|
|
||||||
" "
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
parts.push(path.call(print, "body"));
|
parts.push(path.call(print, "body"));
|
||||||
@@ -791,7 +783,8 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
const isTypeAnnotation = n.type === "ObjectTypeAnnotation";
|
const isTypeAnnotation = n.type === "ObjectTypeAnnotation";
|
||||||
const isTypeScriptTypeAnnotation = n.type === "TSTypeLiteral";
|
const isTypeScriptTypeAnnotation = n.type === "TSTypeLiteral";
|
||||||
const isTypeScriptInterfaceBody = n.type === "TSInterfaceBody";
|
const isTypeScriptInterfaceBody = n.type === "TSInterfaceBody";
|
||||||
const isTypeScriptType = isTypeScriptTypeAnnotation || isTypeScriptInterfaceBody;
|
const isTypeScriptType =
|
||||||
|
isTypeScriptTypeAnnotation || isTypeScriptInterfaceBody;
|
||||||
// Leave this here because we *might* want to make this
|
// Leave this here because we *might* want to make this
|
||||||
// configurable later -- flow accepts ";" for type separators,
|
// configurable later -- flow accepts ";" for type separators,
|
||||||
// typescript accepts ";" and newlines
|
// typescript accepts ";" and newlines
|
||||||
@@ -807,7 +800,7 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
const parentIsUnionTypeAnnotation = parent.type === "UnionTypeAnnotation";
|
const parentIsUnionTypeAnnotation = parent.type === "UnionTypeAnnotation";
|
||||||
let propertiesField;
|
let propertiesField;
|
||||||
|
|
||||||
if (n.type === 'TSTypeLiteral') {
|
if (n.type === "TSTypeLiteral") {
|
||||||
propertiesField = "members";
|
propertiesField = "members";
|
||||||
} else if (n.type === "TSInterfaceBody") {
|
} else if (n.type === "TSInterfaceBody") {
|
||||||
propertiesField = "body";
|
propertiesField = "body";
|
||||||
@@ -836,25 +829,19 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let separatorParts = [];
|
let separatorParts = [];
|
||||||
const props = propsAndLoc
|
const props = propsAndLoc.sort((a, b) => a.loc - b.loc).map(prop => {
|
||||||
.sort((a, b) => a.loc - b.loc)
|
const result = concat(separatorParts.concat(group(prop.printed)));
|
||||||
.map(prop => {
|
separatorParts = [separator, line];
|
||||||
const result = concat(separatorParts.concat(group(prop.printed)));
|
if (util.isNextLineEmpty(options.originalText, prop.node)) {
|
||||||
separatorParts = [separator, line];
|
separatorParts.push(hardline);
|
||||||
if (
|
}
|
||||||
util.isNextLineEmpty(options.originalText, prop.node)
|
return result;
|
||||||
) {
|
});
|
||||||
separatorParts.push(hardline);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
});
|
|
||||||
|
|
||||||
const lastElem = util.getLast(n[propertiesField]);
|
const lastElem = util.getLast(n[propertiesField]);
|
||||||
|
|
||||||
const canHaveTrailingSeparator = !(
|
const canHaveTrailingSeparator = !(lastElem &&
|
||||||
lastElem &&
|
(lastElem.type === "RestProperty" || lastElem.type === "RestElement"));
|
||||||
(lastElem.type === "RestProperty" || lastElem.type === "RestElement")
|
|
||||||
);
|
|
||||||
|
|
||||||
let content;
|
let content;
|
||||||
if (props.length === 0 && !n.typeAnnotation) {
|
if (props.length === 0 && !n.typeAnnotation) {
|
||||||
@@ -876,17 +863,14 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
indent(
|
indent(
|
||||||
align(
|
align(
|
||||||
parentIsUnionTypeAnnotation ? 2 : 0,
|
parentIsUnionTypeAnnotation ? 2 : 0,
|
||||||
concat([
|
concat([options.bracketSpacing ? line : softline, concat(props)])
|
||||||
options.bracketSpacing ? line : softline,
|
|
||||||
concat(props)
|
|
||||||
])
|
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
ifBreak(
|
ifBreak(
|
||||||
canHaveTrailingSeparator &&
|
canHaveTrailingSeparator &&
|
||||||
(separator !== "," || shouldPrintComma(options))
|
(separator !== "," || shouldPrintComma(options))
|
||||||
? separator
|
? separator
|
||||||
: ""
|
: ""
|
||||||
),
|
),
|
||||||
align(
|
align(
|
||||||
parentIsUnionTypeAnnotation ? 2 : 0,
|
parentIsUnionTypeAnnotation ? 2 : 0,
|
||||||
@@ -1100,9 +1084,13 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
concat([
|
concat([
|
||||||
line,
|
line,
|
||||||
"? ",
|
"? ",
|
||||||
n.consequent.type === "ConditionalExpression" ? ifBreak("", "(") : "",
|
n.consequent.type === "ConditionalExpression"
|
||||||
|
? ifBreak("", "(")
|
||||||
|
: "",
|
||||||
align(2, path.call(print, "consequent")),
|
align(2, path.call(print, "consequent")),
|
||||||
n.consequent.type === "ConditionalExpression" ? ifBreak("", ")") : "",
|
n.consequent.type === "ConditionalExpression"
|
||||||
|
? ifBreak("", ")")
|
||||||
|
: "",
|
||||||
line,
|
line,
|
||||||
": ",
|
": ",
|
||||||
align(2, path.call(print, "alternate"))
|
align(2, path.call(print, "alternate"))
|
||||||
@@ -1162,12 +1150,14 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
options
|
options
|
||||||
);
|
);
|
||||||
case "WithStatement":
|
case "WithStatement":
|
||||||
return group(concat([
|
return group(
|
||||||
"with (",
|
concat([
|
||||||
path.call(print, "object"),
|
"with (",
|
||||||
")",
|
path.call(print, "object"),
|
||||||
adjustClause(n.body, path.call(print, "body"))
|
")",
|
||||||
]));
|
adjustClause(n.body, path.call(print, "body"))
|
||||||
|
])
|
||||||
|
);
|
||||||
case "IfStatement":
|
case "IfStatement":
|
||||||
const con = adjustClause(n.consequent, path.call(print, "consequent"));
|
const con = adjustClause(n.consequent, path.call(print, "consequent"));
|
||||||
const opening = group(
|
const opening = group(
|
||||||
@@ -1224,52 +1214,58 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
|
|
||||||
return concat([
|
return concat([
|
||||||
printedComments,
|
printedComments,
|
||||||
group(concat([
|
group(
|
||||||
"for (",
|
concat([
|
||||||
|
"for (",
|
||||||
|
group(
|
||||||
|
concat([
|
||||||
|
indent(
|
||||||
|
concat([
|
||||||
|
softline,
|
||||||
|
path.call(print, "init"),
|
||||||
|
";",
|
||||||
|
line,
|
||||||
|
path.call(print, "test"),
|
||||||
|
";",
|
||||||
|
line,
|
||||||
|
path.call(print, "update")
|
||||||
|
])
|
||||||
|
),
|
||||||
|
softline
|
||||||
|
])
|
||||||
|
),
|
||||||
|
")",
|
||||||
|
body
|
||||||
|
])
|
||||||
|
)
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
case "WhileStatement":
|
||||||
|
return group(
|
||||||
|
concat([
|
||||||
|
"while (",
|
||||||
group(
|
group(
|
||||||
concat([
|
concat([
|
||||||
indent(
|
indent(concat([softline, path.call(print, "test")])),
|
||||||
concat([
|
|
||||||
softline,
|
|
||||||
path.call(print, "init"),
|
|
||||||
";",
|
|
||||||
line,
|
|
||||||
path.call(print, "test"),
|
|
||||||
";",
|
|
||||||
line,
|
|
||||||
path.call(print, "update")
|
|
||||||
])
|
|
||||||
),
|
|
||||||
softline
|
softline
|
||||||
])
|
])
|
||||||
),
|
),
|
||||||
")",
|
")",
|
||||||
body
|
adjustClause(n.body, path.call(print, "body"))
|
||||||
]))
|
])
|
||||||
]);
|
);
|
||||||
}
|
|
||||||
case "WhileStatement":
|
|
||||||
return group(concat([
|
|
||||||
"while (",
|
|
||||||
group(
|
|
||||||
concat([
|
|
||||||
indent(concat([softline, path.call(print, "test")])),
|
|
||||||
softline
|
|
||||||
])
|
|
||||||
),
|
|
||||||
")",
|
|
||||||
adjustClause(n.body, path.call(print, "body"))
|
|
||||||
]));
|
|
||||||
case "ForInStatement":
|
case "ForInStatement":
|
||||||
// Note: esprima can't actually parse "for each (".
|
// Note: esprima can't actually parse "for each (".
|
||||||
return group(concat([
|
return group(
|
||||||
n.each ? "for each (" : "for (",
|
concat([
|
||||||
path.call(print, "left"),
|
n.each ? "for each (" : "for (",
|
||||||
" in ",
|
path.call(print, "left"),
|
||||||
path.call(print, "right"),
|
" in ",
|
||||||
")",
|
path.call(print, "right"),
|
||||||
adjustClause(n.body, path.call(print, "body"))
|
")",
|
||||||
]));
|
adjustClause(n.body, path.call(print, "body"))
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
case "ForOfStatement":
|
case "ForOfStatement":
|
||||||
case "ForAwaitStatement":
|
case "ForAwaitStatement":
|
||||||
@@ -1278,16 +1274,18 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
// https://github.com/estree/estree/pull/138
|
// https://github.com/estree/estree/pull/138
|
||||||
const isAwait = n.type === "ForAwaitStatement" || n.await;
|
const isAwait = n.type === "ForAwaitStatement" || n.await;
|
||||||
|
|
||||||
return group(concat([
|
return group(
|
||||||
"for",
|
concat([
|
||||||
isAwait ? " await" : "",
|
"for",
|
||||||
" (",
|
isAwait ? " await" : "",
|
||||||
path.call(print, "left"),
|
" (",
|
||||||
" of ",
|
path.call(print, "left"),
|
||||||
path.call(print, "right"),
|
" of ",
|
||||||
")",
|
path.call(print, "right"),
|
||||||
adjustClause(n.body, path.call(print, "body"))
|
")",
|
||||||
]));
|
adjustClause(n.body, path.call(print, "body"))
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
case "DoWhileStatement":
|
case "DoWhileStatement":
|
||||||
const clause = adjustClause(n.body, path.call(print, "body"));
|
const clause = adjustClause(n.body, path.call(print, "body"));
|
||||||
@@ -1301,12 +1299,11 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
}
|
}
|
||||||
parts.push("while (");
|
parts.push("while (");
|
||||||
|
|
||||||
parts.push(group(
|
parts.push(
|
||||||
concat([
|
group(concat([indent(softline), path.call(print, "test"), softline])),
|
||||||
indent(softline), path.call(print, "test"),
|
")",
|
||||||
softline
|
semi
|
||||||
])
|
);
|
||||||
), ")", semi);
|
|
||||||
|
|
||||||
return concat(parts);
|
return concat(parts);
|
||||||
case "DoExpression":
|
case "DoExpression":
|
||||||
@@ -1544,7 +1541,8 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
case "JSXText":
|
case "JSXText":
|
||||||
throw new Error("JSXTest should be handled by JSXElement");
|
throw new Error("JSXTest should be handled by JSXElement");
|
||||||
case "JSXEmptyExpression":
|
case "JSXEmptyExpression":
|
||||||
const requiresHardline = n.comments && !n.comments.every(util.isBlockComment);
|
const requiresHardline =
|
||||||
|
n.comments && !n.comments.every(util.isBlockComment);
|
||||||
|
|
||||||
return concat([
|
return concat([
|
||||||
comments.printDanglingComments(
|
comments.printDanglingComments(
|
||||||
@@ -1668,7 +1666,7 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
// of the `.
|
// of the `.
|
||||||
let size = 0;
|
let size = 0;
|
||||||
const value = childPath.getValue().value.raw;
|
const value = childPath.getValue().value.raw;
|
||||||
const index = value.lastIndexOf('\n');
|
const index = value.lastIndexOf("\n");
|
||||||
const tabWidth = options.tabWidth;
|
const tabWidth = options.tabWidth;
|
||||||
if (index !== -1) {
|
if (index !== -1) {
|
||||||
size = util.getAlignmentSize(value, tabWidth, index + 1);
|
size = util.getAlignmentSize(value, tabWidth, index + 1);
|
||||||
@@ -1680,12 +1678,7 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
tabWidth
|
tabWidth
|
||||||
);
|
);
|
||||||
|
|
||||||
parts.push(
|
parts.push("${", aligned, lineSuffixBoundary, "}");
|
||||||
"${",
|
|
||||||
aligned,
|
|
||||||
lineSuffixBoundary,
|
|
||||||
"}"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}, "quasis");
|
}, "quasis");
|
||||||
|
|
||||||
@@ -1717,7 +1710,7 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
// transformed away before printing.
|
// transformed away before printing.
|
||||||
case "TypeAnnotation":
|
case "TypeAnnotation":
|
||||||
if (n.typeAnnotation) {
|
if (n.typeAnnotation) {
|
||||||
return path.call(print, 'typeAnnotation')
|
return path.call(print, "typeAnnotation");
|
||||||
}
|
}
|
||||||
|
|
||||||
return "";
|
return "";
|
||||||
@@ -1930,11 +1923,10 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
|
|
||||||
const parent = path.getParentNode();
|
const parent = path.getParentNode();
|
||||||
// If there's a leading comment, the parent is doing the indentation
|
// If there's a leading comment, the parent is doing the indentation
|
||||||
const shouldIndent = (
|
const shouldIndent =
|
||||||
parent.type !== "TypeParameterInstantiation" &&
|
parent.type !== "TypeParameterInstantiation" &&
|
||||||
!(parent.type === "TypeAlias" &&
|
!(parent.type === "TypeAlias" &&
|
||||||
hasLeadingOwnLineComment(options.originalText, n))
|
hasLeadingOwnLineComment(options.originalText, n));
|
||||||
);
|
|
||||||
|
|
||||||
//const token = isIntersection ? "&" : "|";
|
//const token = isIntersection ? "&" : "|";
|
||||||
const code = concat([
|
const code = concat([
|
||||||
@@ -2012,9 +2004,7 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
parts.push("declare ");
|
parts.push("declare ");
|
||||||
}
|
}
|
||||||
|
|
||||||
const canBreak = (
|
const canBreak = n.right.type === "StringLiteralTypeAnnotation";
|
||||||
n.right.type === "StringLiteralTypeAnnotation"
|
|
||||||
);
|
|
||||||
|
|
||||||
const printed = printAssignmentRight(
|
const printed = printAssignmentRight(
|
||||||
n.right,
|
n.right,
|
||||||
@@ -2093,7 +2083,7 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
case "TSConstKeyword":
|
case "TSConstKeyword":
|
||||||
return "const";
|
return "const";
|
||||||
case "TSDeclareKeyword":
|
case "TSDeclareKeyword":
|
||||||
return "declare"
|
return "declare";
|
||||||
case "TSExportKeyword":
|
case "TSExportKeyword":
|
||||||
return "export";
|
return "export";
|
||||||
case "TSNeverKeyword":
|
case "TSNeverKeyword":
|
||||||
@@ -2129,7 +2119,8 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
case "TSArrayType":
|
case "TSArrayType":
|
||||||
return concat([path.call(print, "elementType"), "[]"]);
|
return concat([path.call(print, "elementType"), "[]"]);
|
||||||
case "TSPropertySignature":
|
case "TSPropertySignature":
|
||||||
const computed = !namedTypes.Identifier.check(n.name) &&
|
const computed =
|
||||||
|
!namedTypes.Identifier.check(n.name) &&
|
||||||
!namedTypes.Literal.check(n.name);
|
!namedTypes.Literal.check(n.name);
|
||||||
|
|
||||||
parts.push(printTypeScriptModifiers(path, options, print));
|
parts.push(printTypeScriptModifiers(path, options, print));
|
||||||
@@ -2155,7 +2146,7 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
|
|
||||||
// This isn't valid semantically, but it's in the AST so we can print it.
|
// This isn't valid semantically, but it's in the AST so we can print it.
|
||||||
if (n.initializer) {
|
if (n.initializer) {
|
||||||
parts.push(" = ", path.call(print, "initializer"));
|
parts.push(" = ", path.call(print, "initializer"));
|
||||||
}
|
}
|
||||||
|
|
||||||
return concat(parts);
|
return concat(parts);
|
||||||
@@ -2200,14 +2191,14 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
case "TSThisType":
|
case "TSThisType":
|
||||||
return "this";
|
return "this";
|
||||||
case "TSLastTypeNode":
|
case "TSLastTypeNode":
|
||||||
return path.call(print, "literal")
|
return path.call(print, "literal");
|
||||||
case "TSIndexedAccessType":
|
case "TSIndexedAccessType":
|
||||||
return concat([
|
return concat([
|
||||||
path.call(print, "objectType"),
|
path.call(print, "objectType"),
|
||||||
"[",
|
"[",
|
||||||
path.call(print, "indexType"),
|
path.call(print, "indexType"),
|
||||||
"]"
|
"]"
|
||||||
])
|
]);
|
||||||
case "TSConstructSignature":
|
case "TSConstructSignature":
|
||||||
case "TSConstructorType":
|
case "TSConstructorType":
|
||||||
case "TSCallSignature":
|
case "TSCallSignature":
|
||||||
@@ -2217,26 +2208,16 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
const isType = n.type === "TSConstructorType";
|
const isType = n.type === "TSConstructorType";
|
||||||
|
|
||||||
if (n.typeParameters) {
|
if (n.typeParameters) {
|
||||||
parts.push(printTypeParameters(path, options, print, "typeParameters"))
|
parts.push(printTypeParameters(path, options, print, "typeParameters"));
|
||||||
}
|
}
|
||||||
|
|
||||||
parts.push(
|
parts.push("(", join(", ", path.map(print, "parameters")), ")");
|
||||||
"(",
|
|
||||||
join(", ", path.map(print, "parameters")),
|
|
||||||
")"
|
|
||||||
);
|
|
||||||
if (n.typeAnnotation) {
|
if (n.typeAnnotation) {
|
||||||
parts.push(
|
parts.push(isType ? " => " : ": ", path.call(print, "typeAnnotation"));
|
||||||
isType ? " => " : ": ",
|
|
||||||
path.call(print, "typeAnnotation")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
return concat(parts);
|
return concat(parts);
|
||||||
case "TSTypeOperator":
|
case "TSTypeOperator":
|
||||||
return concat([
|
return concat(["keyof ", path.call(print, "typeAnnotation")]);
|
||||||
"keyof ",
|
|
||||||
path.call(print, "typeAnnotation")
|
|
||||||
])
|
|
||||||
case "TSMappedType":
|
case "TSMappedType":
|
||||||
return group(
|
return group(
|
||||||
concat([
|
concat([
|
||||||
@@ -2256,11 +2237,7 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
path.call(print, "typeAnnotation")
|
path.call(print, "typeAnnotation")
|
||||||
])
|
])
|
||||||
),
|
),
|
||||||
comments.printDanglingComments(
|
comments.printDanglingComments(path, options, /* sameIndent */ true),
|
||||||
path,
|
|
||||||
options,
|
|
||||||
/* sameIndent */ true
|
|
||||||
),
|
|
||||||
options.bracketSpacing ? line : softline,
|
options.bracketSpacing ? line : softline,
|
||||||
"}"
|
"}"
|
||||||
])
|
])
|
||||||
@@ -2269,41 +2246,29 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
parts.push(path.call(print, "name"));
|
parts.push(path.call(print, "name"));
|
||||||
|
|
||||||
if (n.constraint) {
|
if (n.constraint) {
|
||||||
parts.push(
|
parts.push(" in ", path.call(print, "constraint"));
|
||||||
" in ",
|
|
||||||
path.call(print, "constraint")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return concat(parts)
|
return concat(parts);
|
||||||
case "TSMethodSignature":
|
case "TSMethodSignature":
|
||||||
parts.push(
|
parts.push(
|
||||||
path.call(print, "name"),
|
path.call(print, "name"),
|
||||||
printFunctionTypeParameters(path, options, print),
|
printFunctionTypeParameters(path, options, print),
|
||||||
printFunctionParams(path, print, options)
|
printFunctionParams(path, print, options)
|
||||||
)
|
);
|
||||||
|
|
||||||
if (n.typeAnnotation) {
|
if (n.typeAnnotation) {
|
||||||
parts.push(
|
parts.push(": ", path.call(print, "typeAnnotation"));
|
||||||
": ",
|
|
||||||
path.call(print, "typeAnnotation")
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
return concat(parts)
|
return concat(parts);
|
||||||
case "TSNamespaceExportDeclaration":
|
case "TSNamespaceExportDeclaration":
|
||||||
if (n.declaration) {
|
if (n.declaration) {
|
||||||
parts.push(
|
parts.push("export ", path.call(print, "declaration"));
|
||||||
"export ",
|
|
||||||
path.call(print, "declaration")
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
parts.push(
|
parts.push("export as namespace ", path.call(print, "name"));
|
||||||
"export as namespace ",
|
|
||||||
path.call(print, "name")
|
|
||||||
)
|
|
||||||
|
|
||||||
if (options.semi) {
|
if (options.semi) {
|
||||||
parts.push(";")
|
parts.push(";");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2313,11 +2278,7 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
parts.push(printTypeScriptModifiers(path, options, print));
|
parts.push(printTypeScriptModifiers(path, options, print));
|
||||||
}
|
}
|
||||||
|
|
||||||
parts.push(
|
parts.push("enum ", path.call(print, "name"), " ");
|
||||||
"enum ",
|
|
||||||
path.call(print, "name"),
|
|
||||||
" "
|
|
||||||
)
|
|
||||||
|
|
||||||
if (n.members.length === 0) {
|
if (n.members.length === 0) {
|
||||||
parts.push(
|
parts.push(
|
||||||
@@ -2370,19 +2331,15 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
path.call(print, "name"),
|
path.call(print, "name"),
|
||||||
" = ",
|
" = ",
|
||||||
path.call(print, "moduleReference")
|
path.call(print, "moduleReference")
|
||||||
)
|
);
|
||||||
|
|
||||||
if (options.semi) {
|
if (options.semi) {
|
||||||
parts.push(";")
|
parts.push(";");
|
||||||
}
|
}
|
||||||
|
|
||||||
return group(concat(parts));
|
return group(concat(parts));
|
||||||
case "TSExternalModuleReference":
|
case "TSExternalModuleReference":
|
||||||
return concat([
|
return concat(["require(", path.call(print, "expression"), ")"]);
|
||||||
"require(",
|
|
||||||
path.call(print, "expression"),
|
|
||||||
")"
|
|
||||||
])
|
|
||||||
case "TSModuleDeclaration": {
|
case "TSModuleDeclaration": {
|
||||||
const parent = path.getParentNode();
|
const parent = path.getParentNode();
|
||||||
const isExternalModule = namedTypes.Literal.check(n.name);
|
const isExternalModule = namedTypes.Literal.check(n.name);
|
||||||
@@ -2396,10 +2353,11 @@ function genericPrintNoParens(path, options, print, args) {
|
|||||||
|
|
||||||
// Global declaration looks like this:
|
// Global declaration looks like this:
|
||||||
// declare global { ... }
|
// declare global { ... }
|
||||||
const isGlobalDeclaration = n.name.type === "Identifier" &&
|
const isGlobalDeclaration =
|
||||||
|
n.name.type === "Identifier" &&
|
||||||
n.name.name === "global" &&
|
n.name.name === "global" &&
|
||||||
n.modifiers &&
|
n.modifiers &&
|
||||||
n.modifiers.some(modifier => modifier.type === "TSDeclareKeyword");
|
n.modifiers.some(modifier => modifier.type === "TSDeclareKeyword");
|
||||||
|
|
||||||
if (!isGlobalDeclaration) {
|
if (!isGlobalDeclaration) {
|
||||||
parts.push(isExternalModule ? "module " : "namespace ");
|
parts.push(isExternalModule ? "module " : "namespace ");
|
||||||
@@ -2495,10 +2453,7 @@ function printStatementSequence(path, options, print) {
|
|||||||
|
|
||||||
// in no-semi mode, prepend statement with semicolon if it might break ASI
|
// in no-semi mode, prepend statement with semicolon if it might break ASI
|
||||||
if (!options.semi && !isClass && stmtNeedsASIProtection(stmtPath)) {
|
if (!options.semi && !isClass && stmtNeedsASIProtection(stmtPath)) {
|
||||||
if (
|
if (stmt.comments && stmt.comments.some(comment => comment.leading)) {
|
||||||
stmt.comments &&
|
|
||||||
stmt.comments.some(comment => comment.leading)
|
|
||||||
) {
|
|
||||||
// Note: stmtNeedsASIProtection requires stmtPath to already be printed
|
// Note: stmtNeedsASIProtection requires stmtPath to already be printed
|
||||||
// as it reads needsParens which is mutated on the instance
|
// as it reads needsParens which is mutated on the instance
|
||||||
parts.push(print(stmtPath, { needsSemi: true }));
|
parts.push(print(stmtPath, { needsSemi: true }));
|
||||||
@@ -2583,13 +2538,20 @@ function printMethod(path, options, print) {
|
|||||||
|
|
||||||
parts.push(
|
parts.push(
|
||||||
key,
|
key,
|
||||||
concat(path.call(valuePath => [
|
concat(
|
||||||
printFunctionTypeParameters(valuePath, options, print),
|
path.call(
|
||||||
group(concat([
|
valuePath => [
|
||||||
printFunctionParams(valuePath, print, options),
|
printFunctionTypeParameters(valuePath, options, print),
|
||||||
printReturnType(valuePath, print)
|
group(
|
||||||
]))
|
concat([
|
||||||
], "value"))
|
printFunctionParams(valuePath, print, options),
|
||||||
|
printReturnType(valuePath, print)
|
||||||
|
])
|
||||||
|
)
|
||||||
|
],
|
||||||
|
"value"
|
||||||
|
)
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!node.value.body || node.value.body.length === 0) {
|
if (!node.value.body || node.value.body.length === 0) {
|
||||||
@@ -2671,9 +2633,9 @@ function printArgumentsList(path, options, print) {
|
|||||||
let i = 0;
|
let i = 0;
|
||||||
path.each(function(argPath) {
|
path.each(function(argPath) {
|
||||||
if (shouldGroupFirst && i === 0) {
|
if (shouldGroupFirst && i === 0) {
|
||||||
printedExpanded =
|
printedExpanded = [
|
||||||
[argPath.call(p => print(p, { expandFirstArg: true }))]
|
argPath.call(p => print(p, { expandFirstArg: true }))
|
||||||
.concat(printed.slice(1));
|
].concat(printed.slice(1));
|
||||||
}
|
}
|
||||||
if (shouldGroupLast && i === args.length - 1) {
|
if (shouldGroupLast && i === args.length - 1) {
|
||||||
printedExpanded = printed
|
printedExpanded = printed
|
||||||
@@ -2735,17 +2697,13 @@ function printArgumentsList(path, options, print) {
|
|||||||
|
|
||||||
function printFunctionTypeParameters(path, options, print) {
|
function printFunctionTypeParameters(path, options, print) {
|
||||||
const fun = path.getValue();
|
const fun = path.getValue();
|
||||||
const paramsFieldIsArray = Array.isArray(fun["typeParameters"])
|
const paramsFieldIsArray = Array.isArray(fun["typeParameters"]);
|
||||||
|
|
||||||
if (fun.typeParameters) {
|
if (fun.typeParameters) {
|
||||||
// for TSFunctionType typeParameters is an array
|
// for TSFunctionType typeParameters is an array
|
||||||
// for FunctionTypeAnnotation it's a single node
|
// for FunctionTypeAnnotation it's a single node
|
||||||
if (paramsFieldIsArray) {
|
if (paramsFieldIsArray) {
|
||||||
return concat(
|
return concat("<", join(", ", path.map(print, "typeParameters")), ">");
|
||||||
"<",
|
|
||||||
join(", ", path.map(print, "typeParameters")),
|
|
||||||
">"
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
return path.call(print, "typeParameters");
|
return path.call(print, "typeParameters");
|
||||||
}
|
}
|
||||||
@@ -2757,10 +2715,10 @@ function printFunctionTypeParameters(path, options, print) {
|
|||||||
function printFunctionParams(path, print, options, expandArg) {
|
function printFunctionParams(path, print, options, expandArg) {
|
||||||
const fun = path.getValue();
|
const fun = path.getValue();
|
||||||
// namedTypes.Function.assert(fun);
|
// namedTypes.Function.assert(fun);
|
||||||
const paramsField = (fun.type === "TSFunctionType" ||
|
const paramsField = fun.type === "TSFunctionType" ||
|
||||||
fun.type === "TSMethodSignature")
|
fun.type === "TSMethodSignature"
|
||||||
? "parameters"
|
? "parameters"
|
||||||
: "params";
|
: "params";
|
||||||
|
|
||||||
const printed = path.map(print, paramsField);
|
const printed = path.map(print, paramsField);
|
||||||
|
|
||||||
@@ -2842,7 +2800,8 @@ function printFunctionParams(path, print, options, expandArg) {
|
|||||||
fun[paramsField].length === 1 &&
|
fun[paramsField].length === 1 &&
|
||||||
fun[paramsField][0].name === null &&
|
fun[paramsField][0].name === null &&
|
||||||
fun[paramsField][0].typeAnnotation &&
|
fun[paramsField][0].typeAnnotation &&
|
||||||
flowTypeAnnotations.indexOf(fun[paramsField][0].typeAnnotation.type) !== -1 &&
|
flowTypeAnnotations.indexOf(fun[paramsField][0].typeAnnotation.type) !==
|
||||||
|
-1 &&
|
||||||
!fun.rest;
|
!fun.rest;
|
||||||
|
|
||||||
if (isFlowShorthandWithOneArg) {
|
if (isFlowShorthandWithOneArg) {
|
||||||
@@ -2850,8 +2809,7 @@ function printFunctionParams(path, print, options, expandArg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const canHaveTrailingComma =
|
const canHaveTrailingComma =
|
||||||
!(lastParam && lastParam.type === "RestElement") &&
|
!(lastParam && lastParam.type === "RestElement") && !fun.rest;
|
||||||
!fun.rest;
|
|
||||||
|
|
||||||
return concat([
|
return concat([
|
||||||
"(",
|
"(",
|
||||||
@@ -2920,7 +2878,9 @@ function printObjectMethod(path, options, print) {
|
|||||||
parts.push("*");
|
parts.push("*");
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
objMethod.method || objMethod.kind === "get" || objMethod.kind === "set"
|
objMethod.method ||
|
||||||
|
objMethod.kind === "get" ||
|
||||||
|
objMethod.kind === "set"
|
||||||
) {
|
) {
|
||||||
return printMethod(path, options, print);
|
return printMethod(path, options, print);
|
||||||
}
|
}
|
||||||
@@ -3100,10 +3060,7 @@ function printTypeScriptModifiers(path, options, print) {
|
|||||||
if (!n.modifiers || !n.modifiers.length) {
|
if (!n.modifiers || !n.modifiers.length) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
return concat([
|
return concat([join(" ", path.map(print, "modifiers")), " "]);
|
||||||
join(" ", path.map(print, "modifiers")),
|
|
||||||
" "
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function printTypeParameters(path, options, print, paramsKey) {
|
function printTypeParameters(path, options, print, paramsKey) {
|
||||||
@@ -3115,7 +3072,7 @@ function printTypeParameters(path, options, print, paramsKey) {
|
|||||||
|
|
||||||
// for TypeParameterDeclaration typeParameters is a single node
|
// for TypeParameterDeclaration typeParameters is a single node
|
||||||
if (!Array.isArray(n[paramsKey])) {
|
if (!Array.isArray(n[paramsKey])) {
|
||||||
return path.call(print, paramsKey)
|
return path.call(print, paramsKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
const shouldInline =
|
const shouldInline =
|
||||||
@@ -3200,18 +3157,13 @@ function printMemberLookup(path, options, print) {
|
|||||||
if (
|
if (
|
||||||
!n.property ||
|
!n.property ||
|
||||||
(n.property.type === "Literal" && typeof n.property.value === "number") ||
|
(n.property.type === "Literal" && typeof n.property.value === "number") ||
|
||||||
n.property.type === "NumericLiteral"
|
n.property.type === "NumericLiteral"
|
||||||
) {
|
) {
|
||||||
return concat(["[", property, "]"]);
|
return concat(["[", property, "]"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return group(
|
return group(
|
||||||
concat([
|
concat(["[", indent(concat([softline, property])), softline, "]"])
|
||||||
"[",
|
|
||||||
indent(concat([softline, property])),
|
|
||||||
softline,
|
|
||||||
"]"
|
|
||||||
])
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3243,10 +3195,11 @@ function printMemberChain(path, options, print) {
|
|||||||
node: node,
|
node: node,
|
||||||
printed: comments.printComments(
|
printed: comments.printComments(
|
||||||
path,
|
path,
|
||||||
() => concat([
|
() =>
|
||||||
|
concat([
|
||||||
printFunctionTypeParameters(path, options, print),
|
printFunctionTypeParameters(path, options, print),
|
||||||
printArgumentsList(path, options, print)
|
printArgumentsList(path, options, print)
|
||||||
]),
|
]),
|
||||||
options
|
options
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
@@ -3274,8 +3227,8 @@ function printMemberChain(path, options, print) {
|
|||||||
printedNodes.unshift({
|
printedNodes.unshift({
|
||||||
node: path.getValue(),
|
node: path.getValue(),
|
||||||
printed: concat([
|
printed: concat([
|
||||||
printFunctionTypeParameters(path, options, print),
|
printFunctionTypeParameters(path, options, print),
|
||||||
printArgumentsList(path, options, print)
|
printArgumentsList(path, options, print)
|
||||||
])
|
])
|
||||||
});
|
});
|
||||||
path.call(callee => rec(callee), "callee");
|
path.call(callee => rec(callee), "callee");
|
||||||
@@ -3332,7 +3285,8 @@ function printMemberChain(path, options, print) {
|
|||||||
let hasSeenCallExpression = false;
|
let hasSeenCallExpression = false;
|
||||||
for (; i < printedNodes.length; ++i) {
|
for (; i < printedNodes.length; ++i) {
|
||||||
if (
|
if (
|
||||||
hasSeenCallExpression && printedNodes[i].node.type === "MemberExpression"
|
hasSeenCallExpression &&
|
||||||
|
printedNodes[i].node.type === "MemberExpression"
|
||||||
) {
|
) {
|
||||||
// [0] should be appended at the end of the group instead of the
|
// [0] should be appended at the end of the group instead of the
|
||||||
// beginning of the next one
|
// beginning of the next one
|
||||||
@@ -3494,7 +3448,6 @@ function printJSXChildren(path, options, print, jsxWhitespace) {
|
|||||||
|
|
||||||
const stripped = textLine.replace(/^\s+|\s+$/g, "");
|
const stripped = textLine.replace(/^\s+|\s+$/g, "");
|
||||||
if (stripped) {
|
if (stripped) {
|
||||||
|
|
||||||
// Split text into words separated by "line"s.
|
// Split text into words separated by "line"s.
|
||||||
stripped.split(/(\s+)/).forEach(word => {
|
stripped.split(/(\s+)/).forEach(word => {
|
||||||
const space = /\s+/.test(word);
|
const space = /\s+/.test(word);
|
||||||
@@ -3529,7 +3482,7 @@ function printJSXChildren(path, options, print, jsxWhitespace) {
|
|||||||
// we need to include an empty content part between each JSX
|
// we need to include an empty content part between each JSX
|
||||||
// whitespace.
|
// whitespace.
|
||||||
if (i + 1 < value.length) {
|
if (i + 1 < value.length) {
|
||||||
children.push('');
|
children.push("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3598,7 +3551,10 @@ function printJSXElement(path, options, print) {
|
|||||||
let forcedBreak = willBreak(openingLines);
|
let forcedBreak = willBreak(openingLines);
|
||||||
|
|
||||||
const rawJsxWhitespace = options.singleQuote ? "{' '}" : '{" "}';
|
const rawJsxWhitespace = options.singleQuote ? "{' '}" : '{" "}';
|
||||||
const jsxWhitespace = ifBreak(concat([softline, rawJsxWhitespace, softline]), " ")
|
const jsxWhitespace = ifBreak(
|
||||||
|
concat([softline, rawJsxWhitespace, softline]),
|
||||||
|
" "
|
||||||
|
);
|
||||||
|
|
||||||
const children = printJSXChildren(path, options, print, jsxWhitespace);
|
const children = printJSXChildren(path, options, print, jsxWhitespace);
|
||||||
|
|
||||||
@@ -3634,7 +3590,6 @@ function printJSXElement(path, options, print) {
|
|||||||
// Also detect whether we will force this element to output over multiple lines.
|
// Also detect whether we will force this element to output over multiple lines.
|
||||||
let multilineChildren = [];
|
let multilineChildren = [];
|
||||||
children.forEach((child, i) => {
|
children.forEach((child, i) => {
|
||||||
|
|
||||||
// Ensure that we display leading, trailing, and solitary whitespace as
|
// Ensure that we display leading, trailing, and solitary whitespace as
|
||||||
// `{" "}` when outputting this element over multiple lines.
|
// `{" "}` when outputting this element over multiple lines.
|
||||||
if (child === jsxWhitespace) {
|
if (child === jsxWhitespace) {
|
||||||
@@ -3714,11 +3669,17 @@ function shouldInlineLogicalExpression(node) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node.right.type === "ObjectExpression" && node.right.properties.length !== 0) {
|
if (
|
||||||
|
node.right.type === "ObjectExpression" &&
|
||||||
|
node.right.properties.length !== 0
|
||||||
|
) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node.right.type === "ArrayExpression" && node.right.elements.length !== 0) {
|
if (
|
||||||
|
node.right.type === "ArrayExpression" &&
|
||||||
|
node.right.elements.length !== 0
|
||||||
|
) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3733,7 +3694,13 @@ function shouldInlineLogicalExpression(node) {
|
|||||||
// precedence level and the AST is structured based on precedence
|
// precedence level and the AST is structured based on precedence
|
||||||
// level, things are naturally broken up correctly, i.e. `&&` is
|
// level, things are naturally broken up correctly, i.e. `&&` is
|
||||||
// broken before `+`.
|
// broken before `+`.
|
||||||
function printBinaryishExpressions(path, print, options, isNested, isInsideParenthesis) {
|
function printBinaryishExpressions(
|
||||||
|
path,
|
||||||
|
print,
|
||||||
|
options,
|
||||||
|
isNested,
|
||||||
|
isInsideParenthesis
|
||||||
|
) {
|
||||||
let parts = [];
|
let parts = [];
|
||||||
let node = path.getValue();
|
let node = path.getValue();
|
||||||
|
|
||||||
@@ -3750,7 +3717,8 @@ function printBinaryishExpressions(path, print, options, isNested, isInsideParen
|
|||||||
// which is unique in that it is right-associative.)
|
// which is unique in that it is right-associative.)
|
||||||
if (
|
if (
|
||||||
util.getPrecedence(node.left.operator) ===
|
util.getPrecedence(node.left.operator) ===
|
||||||
util.getPrecedence(node.operator) && node.operator !== "**"
|
util.getPrecedence(node.operator) &&
|
||||||
|
node.operator !== "**"
|
||||||
) {
|
) {
|
||||||
// Flatten them out by recursively calling this function.
|
// Flatten them out by recursively calling this function.
|
||||||
parts = parts.concat(
|
parts = parts.concat(
|
||||||
@@ -3825,13 +3793,12 @@ function printAssignment(
|
|||||||
return printedLeft;
|
return printedLeft;
|
||||||
}
|
}
|
||||||
|
|
||||||
const canBreak = (
|
const canBreak =
|
||||||
(isBinaryish(rightNode) && !shouldInlineLogicalExpression(rightNode)) ||
|
(isBinaryish(rightNode) && !shouldInlineLogicalExpression(rightNode)) ||
|
||||||
(leftNode.type === "Identifier" || leftNode.type === "MemberExpression") &&
|
((leftNode.type === "Identifier" || leftNode.type === "MemberExpression") &&
|
||||||
(rightNode.type === "StringLiteral" ||
|
(rightNode.type === "StringLiteral" ||
|
||||||
(rightNode.type === "Literal" && typeof rightNode.value === "string") ||
|
(rightNode.type === "Literal" && typeof rightNode.value === "string") ||
|
||||||
isMemberExpressionChain(rightNode))
|
isMemberExpressionChain(rightNode)));
|
||||||
);
|
|
||||||
|
|
||||||
const printed = printAssignmentRight(
|
const printed = printAssignmentRight(
|
||||||
rightNode,
|
rightNode,
|
||||||
@@ -3953,7 +3920,7 @@ function makeString(rawContent, enclosingQuote) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function printRegex(node) {
|
function printRegex(node) {
|
||||||
const flags = node.flags.split('').sort().join('');
|
const flags = node.flags.split("").sort().join("");
|
||||||
return `/${node.pattern}/${flags}`;
|
return `/${node.pattern}/${flags}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4009,7 +3976,14 @@ function getLeftSide(node) {
|
|||||||
if (node.expressions) {
|
if (node.expressions) {
|
||||||
return node.expressions[0];
|
return node.expressions[0];
|
||||||
}
|
}
|
||||||
return node.left || node.test || node.callee || node.object || node.tag || node.argument;
|
return (
|
||||||
|
node.left ||
|
||||||
|
node.test ||
|
||||||
|
node.callee ||
|
||||||
|
node.object ||
|
||||||
|
node.tag ||
|
||||||
|
node.argument
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function exprNeedsASIProtection(node) {
|
function exprNeedsASIProtection(node) {
|
||||||
@@ -4095,7 +4069,10 @@ function classChildNeedsASIProtection(node) {
|
|||||||
const isAsync = node.value ? node.value.async : node.async;
|
const isAsync = node.value ? node.value.async : node.async;
|
||||||
const isGenerator = node.value ? node.value.generator : node.generator;
|
const isGenerator = node.value ? node.value.generator : node.generator;
|
||||||
if (
|
if (
|
||||||
isAsync || node.static || node.kind === "get" || node.kind === "set"
|
isAsync ||
|
||||||
|
node.static ||
|
||||||
|
node.kind === "get" ||
|
||||||
|
node.kind === "set"
|
||||||
) {
|
) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -4202,26 +4179,24 @@ function shouldHugArguments(fun) {
|
|||||||
(fun.params[0].type === "Identifier" &&
|
(fun.params[0].type === "Identifier" &&
|
||||||
fun.params[0].typeAnnotation &&
|
fun.params[0].typeAnnotation &&
|
||||||
fun.params[0].typeAnnotation.type === "TypeAnnotation" &&
|
fun.params[0].typeAnnotation.type === "TypeAnnotation" &&
|
||||||
fun.params[0].typeAnnotation.typeAnnotation.type === "ObjectTypeAnnotation") ||
|
fun.params[0].typeAnnotation.typeAnnotation.type ===
|
||||||
fun.params[0].type === "FunctionTypeParam" &&
|
"ObjectTypeAnnotation") ||
|
||||||
fun.params[0].typeAnnotation.type === "ObjectTypeAnnotation") &&
|
(fun.params[0].type === "FunctionTypeParam" &&
|
||||||
|
fun.params[0].typeAnnotation.type === "ObjectTypeAnnotation")) &&
|
||||||
!fun.rest
|
!fun.rest
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function templateLiteralHasNewLines(template) {
|
function templateLiteralHasNewLines(template) {
|
||||||
return template.quasis.some(quasi => quasi.value.raw.includes('\n'));
|
return template.quasis.some(quasi => quasi.value.raw.includes("\n"));
|
||||||
}
|
}
|
||||||
|
|
||||||
function isTemplateOnItsOwnLine(n, text) {
|
function isTemplateOnItsOwnLine(n, text) {
|
||||||
return (
|
return (
|
||||||
(n.type === "TemplateLiteral" && templateLiteralHasNewLines(n) ||
|
((n.type === "TemplateLiteral" && templateLiteralHasNewLines(n)) ||
|
||||||
n.type === "TaggedTemplateExpression" && templateLiteralHasNewLines(n.quasi)) &&
|
(n.type === "TaggedTemplateExpression" &&
|
||||||
!util.hasNewline(
|
templateLiteralHasNewLines(n.quasi))) &&
|
||||||
text,
|
!util.hasNewline(text, util.locStart(n), { backwards: true })
|
||||||
util.locStart(n),
|
|
||||||
{backwards: true}
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4246,8 +4221,10 @@ function printArrayItems(path, options, printPath, print) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function hasDanglingComments(node) {
|
function hasDanglingComments(node) {
|
||||||
return node.comments &&
|
return (
|
||||||
node.comments.some(comment => !comment.leading && !comment.trailing);
|
node.comments &&
|
||||||
|
node.comments.some(comment => !comment.leading && !comment.trailing)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeLines(doc) {
|
function removeLines(doc) {
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ module.exports = function(fork) {
|
|||||||
.build("modifiers", "name", "questionToken", "typeAnnotation")
|
.build("modifiers", "name", "questionToken", "typeAnnotation")
|
||||||
.field("name", def("Identifier"))
|
.field("name", def("Identifier"))
|
||||||
.field("typeAnnotation", def("TypeAnnotation"))
|
.field("typeAnnotation", def("TypeAnnotation"))
|
||||||
.bases("TSSignature")
|
.bases("TSSignature");
|
||||||
|
|
||||||
def("TSAsExpression").bases("Expression");
|
def("TSAsExpression").bases("Expression");
|
||||||
|
|
||||||
@@ -158,7 +158,7 @@ module.exports = function(fork) {
|
|||||||
|
|
||||||
def("TSInterfaceBody")
|
def("TSInterfaceBody")
|
||||||
.build("body")
|
.build("body")
|
||||||
.field("body",[ def("TSPropertySignature")])
|
.field("body", [def("TSPropertySignature")])
|
||||||
.bases("Node");
|
.bases("Node");
|
||||||
|
|
||||||
def("TSModuleDeclaration")
|
def("TSModuleDeclaration")
|
||||||
|
|||||||
+1
-2
@@ -257,7 +257,6 @@ function getPrecedence(op) {
|
|||||||
return PRECEDENCE[op];
|
return PRECEDENCE[op];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Tests if an expression starts with `{`, or (if forbidFunctionAndClass holds) `function` or `class`.
|
// Tests if an expression starts with `{`, or (if forbidFunctionAndClass holds) `function` or `class`.
|
||||||
// Will be overzealous if there's already necessary grouping parentheses.
|
// Will be overzealous if there's already necessary grouping parentheses.
|
||||||
function startsWithNoLookaheadToken(node, forbidFunctionAndClass) {
|
function startsWithNoLookaheadToken(node, forbidFunctionAndClass) {
|
||||||
@@ -325,7 +324,7 @@ function getAlignmentSize(value, tabWidth, startIndex) {
|
|||||||
|
|
||||||
let size = 0;
|
let size = 0;
|
||||||
for (let i = startIndex; i < value.length; ++i) {
|
for (let i = startIndex; i < value.length; ++i) {
|
||||||
if (value[i] === '\t') {
|
if (value[i] === "\t") {
|
||||||
// Tabs behave in a way that they are aligned to the nearest
|
// Tabs behave in a way that they are aligned to the nearest
|
||||||
// multiple of tabWidth:
|
// multiple of tabWidth:
|
||||||
// 0 -> 4, 1 -> 4, 2 -> 4, 3 -> 4
|
// 0 -> 4, 1 -> 4, 2 -> 4, 3 -> 4
|
||||||
|
|||||||
Reference in new issue
Block a user