diff --git a/.eslintrc.yml b/.eslintrc.yml index 5279c7ef0..16808d630 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -14,7 +14,6 @@ settings: import/internal-regex: ^linguist-languages/ rules: prettier-internal-rules/require-json-extensions: error - prettier-internal-rules/better-parent-property-check-in-needs-parens: error curly: error dot-notation: error eqeqeq: @@ -100,3 +99,9 @@ overrides: - error - patterns: - ".." + - files: src/language-js/needs-parens.js + rules: + prettier-internal-rules/better-parent-property-check-in-needs-parens: error + - files: src/**/*.js + rules: + prettier-internal-rules/prefer-fast-path-each: error diff --git a/scripts/tools/eslint-plugin-prettier-internal-rules/index.js b/scripts/tools/eslint-plugin-prettier-internal-rules/index.js index da954fddf..f7627723d 100644 --- a/scripts/tools/eslint-plugin-prettier-internal-rules/index.js +++ b/scripts/tools/eslint-plugin-prettier-internal-rules/index.js @@ -2,7 +2,8 @@ module.exports = { rules: { - "require-json-extensions": require("./require-json-extensions"), "better-parent-property-check-in-needs-parens": require("./better-parent-property-check-in-needs-parens"), + "prefer-fast-path-each": require("./prefer-fast-path-each"), + "require-json-extensions": require("./require-json-extensions"), }, }; diff --git a/scripts/tools/eslint-plugin-prettier-internal-rules/prefer-fast-path-each.js b/scripts/tools/eslint-plugin-prettier-internal-rules/prefer-fast-path-each.js new file mode 100644 index 000000000..f4d6ad97d --- /dev/null +++ b/scripts/tools/eslint-plugin-prettier-internal-rules/prefer-fast-path-each.js @@ -0,0 +1,42 @@ +"use strict"; + +const selector = [ + "ExpressionStatement", + ">", + "CallExpression.expression", + "[optional=false]", + ">", + "MemberExpression.callee", + "[computed=false]", + "[optional=false]", + ">", + "Identifier.property", + '[name="map"]', +].join(""); + +const messageId = "prefer-fast-path-each"; + +module.exports = { + meta: { + type: "suggestion", + docs: { + url: + "https://github.com/prettier/prettier/blob/master/scripts/eslint-plugin-prettier-internal-rules/require-json-extensions.js", + }, + messages: { + [messageId]: "Prefer `FastPath#each()` over `FastPath#map()`.", + }, + fixable: "code", + }, + create(context) { + return { + [selector](node) { + context.report({ + node, + messageId, + fix: (fixer) => fixer.replaceText(node, "each"), + }); + }, + }; + }, +}; diff --git a/src/common/fast-path.js b/src/common/fast-path.js index 61b5883d6..2b14c7ff2 100644 --- a/src/common/fast-path.js +++ b/src/common/fast-path.js @@ -93,9 +93,7 @@ class FastPath { for (let i = 0; i < value.length; ++i) { if (i in value) { stack.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); + callback(this, i); stack.length -= 2; } } diff --git a/src/language-css/printer-postcss.js b/src/language-css/printer-postcss.js index 79c09530c..c1f3c892b 100644 --- a/src/language-css/printer-postcss.js +++ b/src/language-css/printer-postcss.js @@ -987,8 +987,7 @@ function genericPrint(path, options, print) { function printNodeSequence(path, options, print) { const node = path.getValue(); const parts = []; - let i = 0; - path.map((pathChild) => { + path.each((pathChild, i) => { const prevNode = node.nodes[i - 1]; if ( prevNode && @@ -1034,7 +1033,6 @@ function printNodeSequence(path, options, print) { } } } - i++; }, "nodes"); return concat(parts); diff --git a/src/language-graphql/printer-graphql.js b/src/language-graphql/printer-graphql.js index 5320a063a..c008d7dc8 100644 --- a/src/language-graphql/printer-graphql.js +++ b/src/language-graphql/printer-graphql.js @@ -26,7 +26,7 @@ function genericPrint(path, options, print) { switch (n.kind) { case "Document": { const parts = []; - path.map((pathChild, index) => { + path.each((pathChild, index) => { parts.push(concat([pathChild.call(print)])); if (index !== n.definitions.length - 1) { parts.push(hardline); diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 1a28d00b8..4b2118911 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -294,7 +294,7 @@ function printPathNoParens(path, options, print, args) { // Babel 6 if (n.directives) { const directivesCount = n.directives.length; - path.map((childPath, index) => { + path.each((childPath, index) => { parts.push(print(childPath), semi, hardline); if ( (index < directivesCount - 1 || hasContents) && @@ -3573,7 +3573,7 @@ function printStatementSequence(path, options, print) { const bodyNode = path.getNode(); const isClass = bodyNode.type === "ClassBody"; - path.map((stmtPath, i) => { + path.each((stmtPath, i) => { const stmt = stmtPath.getValue(); // Just in case the AST has been modified to contain falsy @@ -4455,8 +4455,7 @@ function printJSXChildren( const n = path.getValue(); const children = []; - // using `map` instead of `each` because it provides `i` - path.map((childPath, i) => { + path.each((childPath, i) => { const child = childPath.getValue(); if (isLiteral(child)) { const text = rawText(child); diff --git a/src/language-markdown/printer-markdown.js b/src/language-markdown/printer-markdown.js index c370b67a3..5b743215f 100644 --- a/src/language-markdown/printer-markdown.js +++ b/src/language-markdown/printer-markdown.js @@ -577,19 +577,16 @@ function printLine(path, value, options) { function printTable(path, options, print) { const hardlineWithoutBreakParent = hardline.parts[0]; const node = path.getValue(); - const contents = []; // { [rowIndex: number]: { [columnIndex: number]: string } } - path.map((rowPath) => { - const rowContents = []; - - rowPath.map((cellPath) => { - rowContents.push( - printDocToString(cellPath.call(print), options).formatted - ); - }, "children"); - - contents.push(rowContents); - }, "children"); + // { [rowIndex: number]: { [columnIndex: number]: string } } + const contents = path.map( + (rowPath) => + rowPath.map( + (cellPath) => printDocToString(cellPath.call(print), options).formatted, + "children" + ), + "children" + ); // Get the width of each column const columnMaxWidths = contents.reduce( @@ -762,7 +759,7 @@ function printChildren(path, options, print, events) { let lastChildNode; - path.map((childPath, index) => { + path.each((childPath, index) => { const childNode = childPath.getValue(); const result = processor(childPath, index);