Style: use object destructuring (#7325)

* Style: use object destructuring

* Update versions.js

* Update sync-flow-tests.js

* Update core.js

Co-authored-by: Georgii Dolzhykov <thorn.mailbox@gmail.com>
This commit is contained in:
fisker CheungandGeorgii Dolzhykov authored and Georgii Dolzhykov committed 2020-01-14 17:30:18 +02:00
1 parent 493df07019
commit d456b158e0
40 files changed
+76 -96

No files matched your search

+9
View File
@@ -32,6 +32,15 @@ rules:
- never
prefer-arrow-callback: error
prefer-const: error
prefer-destructuring:
- error
- VariableDeclarator:
array: false
object: true
AssignmentExpression:
array: false
object: false
- enforceForRenamedProperties: false
prefer-spread: error
react/display-name: off
react/no-deprecated: off
@@ -18,7 +18,7 @@ module.exports = function(babel) {
return {
visitor: {
CallExpression(path) {
const node = path.node;
const { node } = path;
if (isEvalRequire(node.callee) && node.arguments.length === 1) {
let arg = node.arguments[0];
if (t.isLiteral(arg) && arg.value.startsWith(".")) {
@@ -29,7 +29,7 @@ module.exports = function(babel) {
}
},
MemberExpression(path) {
const node = path.node;
const { node } = path;
if (isEvalRequire(node.object)) {
path.replaceWith(
t.memberExpression(
+1 -1
View File
@@ -1,7 +1,7 @@
"use strict";
const fs = require("fs");
const promisify = require("util").promisify;
const { promisify } = require("util");
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
+1 -1
View File
@@ -2,7 +2,7 @@
const fs = require("fs");
const globby = require("globby");
const format = require("../src/cli-util").format;
const { format } = require("../src/cli-util");
function tryFormat(file) {
const content = fs.readFileSync(file, "utf8");
+2 -3
View File
@@ -19,9 +19,8 @@ function tryParse(file, content) {
});
if (ast.errors.length > 0) {
const line = ast.errors[0].loc.start.line;
const column = ast.errors[0].loc.start.column;
const message = ast.errors[0].message;
const { line, column } = ast.errors[0].loc.start;
const { message } = ast.errors[0];
return `${file}:${line}:${column}: ${message}`;
}
+2 -2
View File
@@ -24,7 +24,7 @@ const runInBand = process.env.CI ? "--runInBand" : "";
const testPath = process.env.TEST_STANDALONE ? "tests/" : "";
const cmd = `yarn test --color ${runInBand} ${testPath}`;
const code = shell.exec(cmd, {
const { code } = shell.exec(cmd, {
cwd: rootDir,
env: Object.assign({}, process.env, {
NODE_ENV: "production",
@@ -32,6 +32,6 @@ const code = shell.exec(cmd, {
PRETTIER_DIR: path.join(tmpDir, "node_modules/prettier")
}),
shell: true
}).code;
});
process.exit(code);
+1 -1
View File
@@ -56,7 +56,7 @@ function skip(chars) {
return false;
}
const length = text.length;
const { length } = text;
let cursor = index;
while (cursor >= 0 && cursor < length) {
const c = text.charAt(cursor);
+1 -1
View File
@@ -378,7 +378,7 @@ function printDocToString(doc, options) {
case "fill": {
const rem = width - pos;
const parts = doc.parts;
const { parts } = doc;
if (parts.length === 0) {
break;
}
+3 -3
View File
@@ -1,9 +1,9 @@
"use strict";
const version = require("../package.json").version;
const { version } = require("../package.json");
const core = require("./main/core");
const getSupportInfo = require("./main/support").getSupportInfo;
const { getSupportInfo } = require("./main/support");
const getFileInfo = require("./common/get-file-info");
const sharedUtil = require("./common/util-shared");
const loadPlugins = require("./common/load-plugins");
@@ -43,7 +43,7 @@ module.exports = {
},
check(text, opts) {
const formatted = formatWithCursor(text, opts).formatted;
const { formatted } = formatWithCursor(text, opts);
return formatted === text;
},
+2 -7
View File
@@ -4,12 +4,7 @@ const createError = require("../common/parser-create-error");
const parseFrontMatter = require("../utils/front-matter");
const lineColumnToIndex = require("../utils/line-column-to-index");
const { hasPragma } = require("./pragma");
// utils
const utils = require("./utils");
const isSCSS = utils.isSCSS;
const isSCSSNestedPropertyNode = utils.isSCSSNestedPropertyNode;
const { isSCSS, isSCSSNestedPropertyNode } = require("./utils");
function parseValueNodes(nodes) {
let parenGroup = {
@@ -391,7 +386,7 @@ function parseNestedCSS(node, options) {
}
if (node.type === "css-atrule" && params.length > 0) {
const name = node.name;
const { name } = node;
const lowercasedName = node.name.toLowerCase();
if (name === "warn" || name === "error") {
+1 -1
View File
@@ -146,7 +146,7 @@ function isLastNode(path, node) {
if (!parentNode) {
return false;
}
const nodes = parentNode.nodes;
const { nodes } = parentNode;
return nodes && nodes.indexOf(node) === nodes.length - 1;
}
+3 -3
View File
@@ -5,8 +5,8 @@ const { hasPragma } = require("./pragma");
function parseComments(ast) {
const comments = [];
const startToken = ast.loc.startToken;
let next = startToken.next;
const { startToken } = ast.loc;
let { next } = startToken;
while (next.kind !== "<EOF>") {
if (next.kind === "Comment") {
Object.assign(next, {
@@ -57,7 +57,7 @@ function parse(text /*, parsers, opts*/) {
removeTokens(ast);
return ast;
} catch (error) {
const GraphQLError = require("graphql/error").GraphQLError;
const { GraphQLError } = require("graphql/error");
if (error instanceof GraphQLError) {
throw createError(error.message, {
start: {
+1 -1
View File
@@ -263,7 +263,7 @@ function print(path, options, print) {
const parentNode = path.getParentNode(0);
const isConcat = parentNode.type === "ConcatStatement";
if (isConcat) {
const parts = parentNode.parts;
const { parts } = parentNode;
const partIndex = parts.indexOf(n);
if (partIndex > 0) {
const partType = parts[partIndex - 1].type;
+2 -2
View File
@@ -266,7 +266,7 @@ function replacePlaceholders(quasisDoc, expressionDocs) {
if (!doc || !doc.parts || !doc.parts.length) {
return doc;
}
let parts = doc.parts;
let { parts } = doc;
const atIndex = parts.indexOf("@");
const placeholderIndex = atIndex + 1;
if (
@@ -431,7 +431,7 @@ function isStyledComponents(path) {
return false;
}
const tag = parent.tag;
const { tag } = parent;
switch (tag.type) {
case "MemberExpression":
+1 -1
View File
@@ -4,7 +4,7 @@
// However, it should be named parser-babel.js in the next major release.
const createError = require("../common/parser-create-error");
const hasPragma = require("./pragma").hasPragma;
const { hasPragma } = require("./pragma");
const locFns = require("./loc");
const postprocess = require("./postprocess");
+2 -2
View File
@@ -2,7 +2,7 @@
const createError = require("../common/parser-create-error");
const includeShebang = require("../common/parser-include-shebang");
const hasPragma = require("./pragma").hasPragma;
const { hasPragma } = require("./pragma");
const locFns = require("./loc");
const postprocess = require("./postprocess");
@@ -22,7 +22,7 @@ function parse(text, parsers, opts) {
});
if (ast.errors.length > 0) {
const loc = ast.errors[0].loc;
const { loc } = ast.errors[0];
throw createError(ast.errors[0].message, {
start: { line: loc.start.line, column: loc.start.column + 1 },
end: { line: loc.end.line, column: loc.end.column + 1 }
+1 -1
View File
@@ -2,7 +2,7 @@
const createError = require("../common/parser-create-error");
const includeShebang = require("../common/parser-include-shebang");
const hasPragma = require("./pragma").hasPragma;
const { hasPragma } = require("./pragma");
const locFns = require("./loc");
const postprocess = require("./postprocess");
+5 -5
View File
@@ -29,7 +29,7 @@ const {
} = require("../common/util-shared");
const embed = require("./embed");
const clean = require("./clean");
const insertPragma = require("./pragma").insertPragma;
const { insertPragma } = require("./pragma");
const handleComments = require("./comments");
const pathNeedsParens = require("./needs-parens");
const {
@@ -2440,7 +2440,7 @@ function printPathNoParens(path, options, print, args) {
// quasi literal), therefore we want to indent the JavaScript
// expression inside at the beginning of ${ instead of the beginning
// of the `.
const tabWidth = options.tabWidth;
const { tabWidth } = options;
const quasi = childPath.getValue();
const indentSize = getIndentSize(quasi.value.raw, tabWidth);
@@ -3724,7 +3724,7 @@ function printPropertyKey(path, options, print) {
}
const parent = path.getParentNode();
const key = node.key;
const { key } = node;
if (options.quoteProps === "consistent" && !needsQuoteProps.has(parent)) {
const objectHasStringProp = (
@@ -3771,7 +3771,7 @@ function printPropertyKey(path, options, print) {
function printMethod(path, options, print) {
const node = path.getNode();
const kind = node.kind;
const { kind } = node;
const value = node.value || node;
const parts = [];
@@ -4812,7 +4812,7 @@ function printMemberChain(path, options, print) {
// Here we try to retain one typed empty line after each call expression or
// the first group whether it is in parentheses or not
function shouldInsertEmptyLineAfter(node) {
const originalText = options.originalText;
const { originalText } = options;
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(
originalText,
node,
+1 -1
View File
@@ -50,7 +50,7 @@ tokenizeEsSyntax.locator = (value /*, fromIndex*/) => {
};
function esSyntax() {
const Parser = this.Parser;
const { Parser } = this;
const tokenizers = Parser.prototype.blockTokenizers;
const methods = Parser.prototype.blockMethods;
+1 -1
View File
@@ -116,7 +116,7 @@ function splitTextIntoSentences(ast, options) {
return node;
}
let value = node.value;
let { value } = node;
if (parentNode.type === "paragraph") {
if (index === 0) {
+1 -1
View File
@@ -660,7 +660,7 @@ function printRoot(path, options, print) {
/** @type {IgnorePosition | null} */
let ignoreStart = null;
const children = path.getValue().children;
const { children } = path.getValue();
children.forEach((childNode, index) => {
switch (isPrettierIgnore(childNode)) {
case "start":
+3 -5
View File
@@ -7,9 +7,7 @@ const multiparser = require("./multiparser");
const doc = require("../doc");
const docBuilders = doc.builders;
const concat = docBuilders.concat;
const hardline = docBuilders.hardline;
const addAlignmentToDoc = docBuilders.addAlignmentToDoc;
const { concat, hardline, addAlignmentToDoc } = docBuilders;
const docUtils = doc.utils;
/**
@@ -34,7 +32,7 @@ const docUtils = doc.utils;
* the path to the current node through the Abstract Syntax Tree.
*/
function printAstToDoc(ast, options, alignmentSize = 0) {
const printer = options.printer;
const { printer } = options;
if (printer.preprocess) {
ast = printer.preprocess(ast, options);
@@ -95,7 +93,7 @@ function callPluginPrintFunction(path, options, printPath, args) {
assert.ok(path instanceof FastPath);
const node = path.getValue();
const printer = options.printer;
const { printer } = options;
// Escape hatch
if (printer.hasPrettierIgnore && printer.hasPrettierIgnore(path)) {
+1 -1
View File
@@ -139,7 +139,7 @@ function decorateComment(node, comment, options) {
comment.enclosingNode &&
comment.enclosingNode.type === "TemplateLiteral"
) {
const quasis = comment.enclosingNode.quasis;
const { quasis } = comment.enclosingNode;
const commentIndex = findExpressionIndexForComment(
quasis,
comment,
+5 -6
View File
@@ -72,7 +72,7 @@ function coreFormat(text, opts, addAlignmentSize) {
addAlignmentSize = addAlignmentSize || 0;
const parsed = parser.parse(text, opts);
const ast = parsed.ast;
const { ast } = parsed;
text = parsed.text;
if (opts.cursorOffset >= 0) {
@@ -183,12 +183,11 @@ function coreFormat(text, opts, addAlignmentSize) {
function formatRange(text, opts) {
const parsed = parser.parse(text, opts);
const ast = parsed.ast;
const { ast } = parsed;
text = parsed.text;
const range = rangeUtil.calculateRange(text, opts, ast);
const rangeStart = range.rangeStart;
const rangeEnd = range.rangeEnd;
const { rangeStart, rangeEnd } = range;
const rangeString = text.slice(rangeStart, rangeEnd);
// Try to extend the range backwards to the beginning of the line.
@@ -225,7 +224,7 @@ function formatRange(text, opts) {
const rangeLeft = text.slice(0, rangeStart);
const rangeRight = text.slice(rangeEnd);
let cursorOffset = opts.cursorOffset;
let { cursorOffset } = opts;
if (opts.cursorOffset >= rangeEnd) {
// handle the case where the cursor was past the end of the range
cursorOffset =
@@ -398,7 +397,7 @@ module.exports = {
printToDoc(text, opts) {
opts = normalizeOptions(opts);
const parsed = parser.parse(text, opts);
const ast = parsed.ast;
const { ast } = parsed;
text = parsed.text;
attachComments(text, ast, opts);
return printAstToDoc(ast, opts);
+2 -2
View File
@@ -1,6 +1,6 @@
"use strict";
const normalize = require("./options").normalize;
const { normalize } = require("./options");
const comments = require("./comments");
function printSubtree(path, print, options, printAstToDoc) {
@@ -32,7 +32,7 @@ function textToDoc(text, partialNextOptions, parentOptions, printAstToDoc) {
);
const result = require("./parser").parse(text, nextOptions);
const ast = result.ast;
const { ast } = result;
text = result.text;
const astComments = ast.comments;
+3 -3
View File
@@ -3,10 +3,10 @@
const fs = require("fs");
const normalizePath = require("normalize-path");
const readlines = require("n-readlines");
const UndefinedParserError = require("../common/errors").UndefinedParserError;
const getSupportInfo = require("../main/support").getSupportInfo;
const { UndefinedParserError } = require("../common/errors");
const { getSupportInfo } = require("../main/support");
const normalizer = require("./options-normalizer");
const resolveParser = require("./parser").resolveParser;
const { resolveParser } = require("./parser");
const hiddenDefaults = {
astFormat: "estree",
+2 -2
View File
@@ -1,7 +1,7 @@
"use strict";
const path = require("path");
const ConfigError = require("../common/errors").ConfigError;
const { ConfigError } = require("../common/errors");
const jsLoc = require("../language-js/loc");
const { locStart, locEnd } = jsLoc;
@@ -92,7 +92,7 @@ function parse(text, opts) {
ast: parser.parse(text, parsersForCustomParserApi, opts)
};
} catch (error) {
const loc = error.loc;
const { loc } = error;
if (loc) {
const codeFrame = require("@babel/code-frame");
+1 -1
View File
@@ -26,7 +26,7 @@ function getSupportInfo(version, opts) {
version = currentVersion.split("-", 1)[0];
}
const plugins = opts.plugins;
const { plugins } = opts;
const options = arrayify(
Object.assign(
+3 -3
View File
@@ -1,9 +1,9 @@
"use strict";
const version = require("../package.json").version;
const { version } = require("../package.json");
const core = require("./main/core");
const getSupportInfo = require("./main/support").getSupportInfo;
const { getSupportInfo } = require("./main/support");
const sharedUtil = require("./common/util-shared");
const doc = require("./doc");
@@ -45,7 +45,7 @@ module.exports = {
},
check(text, opts) {
const formatted = formatWithCursor(text, opts).formatted;
const { formatted } = formatWithCursor(text, opts);
return formatted === text;
},
+1 -3
View File
@@ -4,9 +4,7 @@ const fs = require("fs");
const path = require("path");
const raw = require("jest-snapshot-serializer-raw").wrap;
const AST_COMPARE = process.env.AST_COMPARE;
const TEST_STANDALONE = process.env.TEST_STANDALONE;
const TEST_CRLF = process.env.TEST_CRLF;
const { AST_COMPARE, TEST_STANDALONE, TEST_CRLF } = process.env;
const CURSOR_PLACEHOLDER = "<|>";
const RANGE_START_PLACEHOLDER = "<<<PRETTIER_RANGE_START>>>";
@@ -4,13 +4,8 @@ const prettier = require("prettier/local");
const docPrinter = prettier.doc.printer;
const docBuilders = prettier.doc.builders;
const printDocToString = docPrinter.printDocToString;
const concat = docBuilders.concat;
const hardline = docBuilders.hardline;
const literalline = docBuilders.literalline;
const trim = docBuilders.trim;
const indent = docBuilders.indent;
const markAsRoot = docBuilders.markAsRoot;
const { printDocToString } = docPrinter;
const { concat, hardline, literalline, trim, indent, markAsRoot } = docBuilders;
describe("markAsRoot", () => {
test.each([
+2 -6
View File
@@ -4,12 +4,8 @@ const prettier = require("prettier/local");
const docPrinter = prettier.doc.printer;
const docBuilders = prettier.doc.builders;
const printDocToString = docPrinter.printDocToString;
const concat = docBuilders.concat;
const line = docBuilders.line;
const trim = docBuilders.trim;
const group = docBuilders.group;
const indent = docBuilders.indent;
const { printDocToString } = docPrinter;
const { concat, line, trim, group, indent } = docBuilders;
// These tests don't use `runPrettier` because `trim` is not used by any
// bundled parser (only third-party plugins).
@@ -1,7 +1,7 @@
"use strict";
const prettier = require("prettier/local");
const concat = prettier.doc.builders.concat;
const { concat } = prettier.doc.builders;
module.exports = {
languages: [
+1 -1
View File
@@ -3,7 +3,7 @@
const fs = require("fs");
const path = require("path");
const stripAnsi = require("strip-ansi");
const SynchronousPromise = require("synchronous-promise").SynchronousPromise;
const { SynchronousPromise } = require("synchronous-promise");
const isProduction = process.env.NODE_ENV === "production";
const prettierRootDir = isProduction ? process.env.PRETTIER_DIR : "../";
+1 -4
View File
@@ -1,10 +1,7 @@
"use strict";
const React = require("react");
const CompLibrary = require("../../../core/CompLibrary.js");
const Container = CompLibrary.Container;
const GridBlock = CompLibrary.GridBlock;
const { Container, GridBlock } = require("../../../core/CompLibrary.js");
class Help extends React.Component {
render() {
+1 -4
View File
@@ -2,10 +2,7 @@
const React = require("react");
const PropTypes = require("prop-types");
const CompLibrary = require("../../core/CompLibrary.js");
const MarkdownBlock = CompLibrary.MarkdownBlock;
const Container = CompLibrary.Container;
const { MarkdownBlock, Container } = require("../../core/CompLibrary.js");
const AnimatedLogo = require("@sandhose/prettier-animated-logo");
const siteConfig = require(process.cwd() + "/siteConfig.js");
+1 -3
View File
@@ -1,9 +1,7 @@
"use strict";
const React = require("react");
const CompLibrary = require("../../../core/CompLibrary.js");
const Container = CompLibrary.Container;
const { Container } = require("../../../core/CompLibrary.js");
const siteConfig = require(process.cwd() + "/siteConfig.js");
+1 -2
View File
@@ -8,8 +8,7 @@
*/
const React = require("react");
const CompLibrary = require("../../core/CompLibrary");
const Container = CompLibrary.Container;
const { Container } = require("../../core/CompLibrary");
const CWD = process.cwd();
+1 -1
View File
@@ -2,7 +2,7 @@ export function stateToggler(key) {
return state => ({ [key]: !state[key] });
}
const hasOwnProperty = Object.prototype.hasOwnProperty;
const { hasOwnProperty } = Object.prototype;
function is(x, y) {
// SameValue algorithm
+1 -1
View File
@@ -126,7 +126,7 @@ function getIndexPosition(text, indexes) {
function createOverlay(start, end) {
return {
token(stream) {
const line = stream.lineOracle.line;
const { line } = stream.lineOracle;
if (line < start.line || line > end.line) {
stream.skipToEnd();