prettier/src/fast-path.js

644 lines
18 KiB
JavaScript

"use strict";
const assert = require("assert");
const util = require("./util");
const startsWithNoLookaheadToken = util.startsWithNoLookaheadToken;
function FastPath(value) {
assert.ok(this instanceof FastPath);
this.stack = [value];
}
// The name of the current property is always the penultimate element of
// this.stack, and always a String.
FastPath.prototype.getName = function getName() {
const s = this.stack;
const len = s.length;
if (len > 1) {
return s[len - 2];
}
// Since the name is always a string, null is a safe sentinel value to
// return if we do not know the name of the (root) value.
return null;
};
// The value of the current property is always the final element of
// this.stack.
FastPath.prototype.getValue = function getValue() {
const s = this.stack;
return s[s.length - 1];
};
function getNodeHelper(path, count) {
const s = path.stack;
for (let i = s.length - 1; i >= 0; i -= 2) {
const value = s[i];
if (value && !Array.isArray(value) && --count < 0) {
return value;
}
}
return null;
}
FastPath.prototype.getNode = function getNode(count) {
return getNodeHelper(this, ~~count);
};
FastPath.prototype.getParentNode = function getParentNode(count) {
return getNodeHelper(this, ~~count + 1);
};
// Temporarily push properties named by string arguments given after the
// callback function onto this.stack, then call the callback with a
// reference to this (modified) FastPath object. Note that the stack will
// be restored to its original state after the callback is finished, so it
// is probably a mistake to retain a reference to the path.
FastPath.prototype.call = function call(callback /*, name1, name2, ... */) {
const s = this.stack;
const origLen = s.length;
let value = s[origLen - 1];
const argc = arguments.length;
for (let i = 1; i < argc; ++i) {
const name = arguments[i];
value = value[name];
s.push(name, value);
}
const result = callback(this);
s.length = origLen;
return result;
};
// Similar to FastPath.prototype.call, except that the value obtained by
// accessing this.getValue()[name1][name2]... should be array-like. The
// callback will be called with a reference to this path object for each
// element of the array.
FastPath.prototype.each = function each(callback /*, name1, name2, ... */) {
const s = this.stack;
const origLen = s.length;
let value = s[origLen - 1];
const argc = arguments.length;
for (let i = 1; i < argc; ++i) {
const name = arguments[i];
value = value[name];
s.push(name, value);
}
for (let i = 0; i < value.length; ++i) {
if (i in value) {
s.push(i, value[i]);
// If the callback needs to know the value of i, call
// path.getName(), assuming path is the parameter name.
callback(this);
s.length -= 2;
}
}
s.length = origLen;
};
// Similar to FastPath.prototype.each, except that the results of the
// callback function invocations are stored in an array and returned at
// the end of the iteration.
FastPath.prototype.map = function map(callback /*, name1, name2, ... */) {
const s = this.stack;
const origLen = s.length;
let value = s[origLen - 1];
const argc = arguments.length;
for (let i = 1; i < argc; ++i) {
const name = arguments[i];
value = value[name];
s.push(name, value);
}
const result = new Array(value.length);
for (let i = 0; i < value.length; ++i) {
if (i in value) {
s.push(i, value[i]);
result[i] = callback(this, i);
s.length -= 2;
}
}
s.length = origLen;
return result;
};
FastPath.prototype.needsParens = function(options) {
const parent = this.getParentNode();
if (!parent) {
return false;
}
const name = this.getName();
const node = this.getNode();
// If the value of this path is some child of a Node and not a Node
// itself, then it doesn't need parentheses. Only Node objects (in
// fact, only Expression nodes) need parentheses.
if (this.getValue() !== node) {
return false;
}
// Only statements don't need parentheses.
if (isStatement(node)) {
return false;
}
// Identifiers never need parentheses.
if (node.type === "Identifier") {
return false;
}
if (parent.type === "ParenthesizedExpression") {
return false;
}
// Add parens around the extends clause of a class. It is needed for almost
// all expressions.
if (
(parent.type === "ClassDeclaration" || parent.type === "ClassExpression") &&
parent.superClass === node &&
(node.type === "ArrowFunctionExpression" ||
node.type === "AssignmentExpression" ||
node.type === "AwaitExpression" ||
node.type === "BinaryExpression" ||
node.type === "ConditionalExpression" ||
node.type === "LogicalExpression" ||
node.type === "NewExpression" ||
node.type === "ObjectExpression" ||
node.type === "ParenthesizedExpression" ||
node.type === "SequenceExpression" ||
node.type === "TaggedTemplateExpression" ||
node.type === "UnaryExpression" ||
node.type === "UpdateExpression" ||
node.type === "YieldExpression")
) {
return true;
}
if (
(parent.type === "ArrowFunctionExpression" &&
parent.body === node &&
node.type !== "SequenceExpression" && // these have parens added anyway
startsWithNoLookaheadToken(node, /* forbidFunctionAndClass */ false)) ||
(parent.type === "ExpressionStatement" &&
startsWithNoLookaheadToken(node, /* forbidFunctionAndClass */ true))
) {
return true;
}
switch (node.type) {
case "CallExpression": {
let firstParentNotMemberExpression = parent;
let i = 0;
while (
firstParentNotMemberExpression &&
firstParentNotMemberExpression.type === "MemberExpression"
) {
firstParentNotMemberExpression = this.getParentNode(++i);
}
if (
firstParentNotMemberExpression.type === "NewExpression" &&
firstParentNotMemberExpression.callee === this.getParentNode(i - 1)
) {
return true;
}
return false;
}
case "SpreadElement":
case "SpreadProperty":
return (
parent.type === "MemberExpression" &&
name === "object" &&
parent.object === node
);
case "UpdateExpression":
if (parent.type === "UnaryExpression") {
return (
node.prefix &&
((node.operator === "++" && parent.operator === "+") ||
(node.operator === "--" && parent.operator === "-"))
);
}
// else fallthrough
case "UnaryExpression":
switch (parent.type) {
case "UnaryExpression":
return (
node.operator === parent.operator &&
(node.operator === "+" || node.operator === "-")
);
case "MemberExpression":
return name === "object" && parent.object === node;
case "TaggedTemplateExpression":
return true;
case "NewExpression":
case "CallExpression":
return name === "callee" && parent.callee === node;
case "BinaryExpression":
return parent.operator === "**" && name === "left";
default:
return false;
}
case "BinaryExpression": {
if (parent.type === "UpdateExpression") {
return true;
}
const isLeftOfAForStatement = node => {
let i = 0;
while (node) {
const parent = this.getParentNode(i++);
if (!parent) {
return false;
}
if (parent.type === "ForStatement" && parent.init === node) {
return true;
}
node = parent;
}
return false;
};
if (node.operator === "in" && isLeftOfAForStatement(node)) {
return true;
}
}
// fallthrough
case "TSTypeAssertionExpression":
case "TSAsExpression":
case "LogicalExpression":
switch (parent.type) {
case "CallExpression":
case "NewExpression":
return name === "callee" && parent.callee === node;
case "ClassDeclaration":
return name === "superClass" && parent.superClass === node;
case "TSTypeAssertionExpression":
case "TaggedTemplateExpression":
case "UnaryExpression":
case "SpreadElement":
case "SpreadProperty":
case "BindExpression":
case "AwaitExpression":
case "TSAsExpression":
case "TSNonNullExpression":
return true;
case "MemberExpression":
return name === "object" && parent.object === node;
case "BinaryExpression":
case "LogicalExpression": {
if (!node.operator && node.type !== "TSTypeAssertionExpression") {
return true;
}
const po = parent.operator;
const pp = util.getPrecedence(po);
const no = node.operator;
const np = util.getPrecedence(no);
if (pp > np) {
return true;
}
if (po === "||" && no === "&&") {
return true;
}
if (pp === np && name === "right") {
assert.strictEqual(parent.right, node);
return true;
}
if (pp === np && !util.shouldFlatten(po, no)) {
return true;
}
// Add parenthesis when working with binary operators
// It's not stricly needed but helps with code understanding
if (util.isBitwiseOperator(po)) {
return true;
}
return false;
}
default:
return false;
}
case "TSParenthesizedType": {
const grandParent = this.getParentNode(1);
if (
(parent.type === "TypeParameter" ||
parent.type === "VariableDeclarator" ||
parent.type === "TypeAnnotation" ||
parent.type === "GenericTypeAnnotation") &&
(node.typeAnnotation.type === "TypeAnnotation" &&
node.typeAnnotation.typeAnnotation.type !== "TSFunctionType" &&
grandParent.type !== "TSTypeOperator")
) {
return false;
}
// Delegate to inner TSParenthesizedType
if (node.typeAnnotation.type === "TSParenthesizedType") {
return false;
}
return true;
}
case "SequenceExpression":
switch (parent.type) {
case "ReturnStatement":
return false;
case "ForStatement":
// Although parentheses wouldn't hurt around sequence
// expressions in the head of for loops, traditional style
// dictates that e.g. i++, j++ should not be wrapped with
// parentheses.
return false;
case "ExpressionStatement":
return name !== "expression";
case "ArrowFunctionExpression":
// We do need parentheses, but SequenceExpressions are handled
// specially when printing bodies of arrow functions.
return name !== "body";
default:
// Otherwise err on the side of overparenthesization, adding
// explicit exceptions above if this proves overzealous.
return true;
}
case "YieldExpression":
if (
parent.type === "UnaryExpression" ||
parent.type === "AwaitExpression" ||
parent.type === "TSAsExpression" ||
parent.type === "TSNonNullExpression"
) {
return true;
}
// else fallthrough
case "AwaitExpression":
switch (parent.type) {
case "TaggedTemplateExpression":
case "BinaryExpression":
case "LogicalExpression":
case "SpreadElement":
case "SpreadProperty":
case "TSAsExpression":
case "TSNonNullExpression":
return true;
case "MemberExpression":
return parent.object === node;
case "NewExpression":
case "CallExpression":
return parent.callee === node;
case "ConditionalExpression":
return parent.test === node;
default:
return false;
}
case "ArrayTypeAnnotation":
return parent.type === "NullableTypeAnnotation";
case "IntersectionTypeAnnotation":
case "UnionTypeAnnotation":
return (
parent.type === "ArrayTypeAnnotation" ||
parent.type === "NullableTypeAnnotation" ||
parent.type === "IntersectionTypeAnnotation" ||
parent.type === "UnionTypeAnnotation"
);
case "NullableTypeAnnotation":
return parent.type === "ArrayTypeAnnotation";
case "FunctionTypeAnnotation":
return (
parent.type === "UnionTypeAnnotation" ||
parent.type === "IntersectionTypeAnnotation"
);
case "StringLiteral":
case "NumericLiteral":
case "Literal":
if (
typeof node.value === "string" &&
parent.type === "ExpressionStatement" &&
// TypeScript workaround for eslint/typescript-eslint-parser#267
// See corresponding workaround in printer.js case: "Literal"
((options.parser !== "typescript" && !parent.directive) ||
(options.parser === "typescript" &&
options.originalText.substr(util.locStart(node) - 1, 1) === "("))
) {
// To avoid becoming a directive
const grandParent = this.getParentNode(1);
return (
grandParent.type === "Program" ||
grandParent.type === "BlockStatement"
);
}
return (
parent.type === "MemberExpression" &&
typeof node.value === "number" &&
name === "object" &&
parent.object === node
);
case "AssignmentExpression": {
const grandParent = this.getParentNode(1);
if (parent.type === "ArrowFunctionExpression" && parent.body === node) {
return true;
} else if (
parent.type === "ClassProperty" &&
parent.key === node &&
parent.computed
) {
return false;
} else if (
parent.type === "TSPropertySignature" &&
parent.name === node
) {
return false;
} else if (
parent.type === "ForStatement" &&
(parent.init === node || parent.update === node)
) {
return false;
} else if (parent.type === "ExpressionStatement") {
return node.left.type === "ObjectPattern";
} else if (parent.type === "TSPropertySignature" && parent.key === node) {
return false;
} else if (parent.type === "AssignmentExpression") {
return false;
} else if (
parent.type === "SequenceExpression" &&
grandParent &&
grandParent.type === "ForStatement" &&
(grandParent.init === parent || grandParent.update === parent)
) {
return false;
}
return true;
}
case "ConditionalExpression":
switch (parent.type) {
case "TaggedTemplateExpression":
case "UnaryExpression":
case "SpreadElement":
case "SpreadProperty":
case "BinaryExpression":
case "LogicalExpression":
case "ExportDefaultDeclaration":
case "AwaitExpression":
case "JSXSpreadAttribute":
case "TSTypeAssertionExpression":
case "TSAsExpression":
case "TSNonNullExpression":
return true;
case "NewExpression":
case "CallExpression":
return name === "callee" && parent.callee === node;
case "ConditionalExpression":
return name === "test" && parent.test === node;
case "MemberExpression":
return name === "object" && parent.object === node;
default:
return false;
}
case "FunctionExpression":
switch (parent.type) {
case "CallExpression":
return name === "callee"; // Not strictly necessary, but it's clearer to the reader if IIFEs are wrapped in parentheses.
case "TaggedTemplateExpression":
return true; // This is basically a kind of IIFE.
case "ExportDefaultDeclaration":
return true;
default:
return false;
}
case "ArrowFunctionExpression":
switch (parent.type) {
case "CallExpression":
return name === "callee";
case "NewExpression":
return name === "callee";
case "MemberExpression":
return name === "object";
case "TSAsExpression":
case "BindExpression":
case "TaggedTemplateExpression":
case "UnaryExpression":
case "LogicalExpression":
case "BinaryExpression":
case "AwaitExpression":
case "TSTypeAssertionExpression":
return true;
case "ConditionalExpression":
return name === "test";
default:
return false;
}
case "ClassExpression":
return parent.type === "ExportDefaultDeclaration";
}
return false;
};
function isStatement(node) {
return (
node.type === "BlockStatement" ||
node.type === "BreakStatement" ||
node.type === "ClassBody" ||
node.type === "ClassDeclaration" ||
node.type === "ClassMethod" ||
node.type === "ClassProperty" ||
node.type === "ContinueStatement" ||
node.type === "DebuggerStatement" ||
node.type === "DeclareClass" ||
node.type === "DeclareExportAllDeclaration" ||
node.type === "DeclareExportDeclaration" ||
node.type === "DeclareFunction" ||
node.type === "DeclareInterface" ||
node.type === "DeclareModule" ||
node.type === "DeclareModuleExports" ||
node.type === "DeclareVariable" ||
node.type === "DoWhileStatement" ||
node.type === "ExportAllDeclaration" ||
node.type === "ExportDefaultDeclaration" ||
node.type === "ExportNamedDeclaration" ||
node.type === "ExpressionStatement" ||
node.type === "ForAwaitStatement" ||
node.type === "ForInStatement" ||
node.type === "ForOfStatement" ||
node.type === "ForStatement" ||
node.type === "FunctionDeclaration" ||
node.type === "IfStatement" ||
node.type === "ImportDeclaration" ||
node.type === "InterfaceDeclaration" ||
node.type === "LabeledStatement" ||
node.type === "MethodDefinition" ||
node.type === "ReturnStatement" ||
node.type === "SwitchStatement" ||
node.type === "ThrowStatement" ||
node.type === "TryStatement" ||
node.type === "TSAbstractClassDeclaration" ||
node.type === "TSEnumDeclaration" ||
node.type === "TSImportEqualsDeclaration" ||
node.type === "TSInterfaceDeclaration" ||
node.type === "TSModuleDeclaration" ||
node.type === "TSNamespaceExportDeclaration" ||
node.type === "TSNamespaceFunctionDeclaration" ||
node.type === "TypeAlias" ||
node.type === "VariableDeclaration" ||
node.type === "WhileStatement" ||
node.type === "WithStatement"
);
}
module.exports = FastPath;