From f04b2b991c48c626789a19eb9339a10925f5f399 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Mon, 1 Feb 2021 13:14:04 +0800 Subject: [PATCH] Enable `no-implicit-coercion` rule (#10186) --- .eslintrc.yml | 1 + jest.config.js | 2 +- src/cli/format.js | 7 +++---- src/config/resolve-config.js | 6 +++--- src/document/doc-builders.js | 2 +- src/language-css/utils.js | 2 +- src/language-graphql/printer-graphql.js | 2 +- src/language-js/embed/html.js | 2 +- src/language-js/needs-parens.js | 2 +- src/language-js/print/class.js | 4 ++-- src/language-js/print/jsx.js | 2 +- src/language-js/printer-estree.js | 4 ++-- src/language-js/utils.js | 2 +- src/language-markdown/printer-markdown.js | 2 +- src/main/multiparser.js | 10 +++++----- tests_integration/runPrettier.js | 6 +++--- 16 files changed, 28 insertions(+), 28 deletions(-) diff --git a/.eslintrc.yml b/.eslintrc.yml index 37c6964be..538619561 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -27,6 +27,7 @@ rules: - devDependencies: ["tests*/**", "scripts/**"] import/order: error no-else-return: error + no-implicit-coercion: error no-inner-declarations: error no-unneeded-ternary: error no-useless-return: error diff --git a/jest.config.js b/jest.config.js index 2ac2c033e..24e278d81 100644 --- a/jest.config.js +++ b/jest.config.js @@ -3,7 +3,7 @@ const installPrettier = require("./scripts/install-prettier"); const isProduction = process.env.NODE_ENV === "production"; -const ENABLE_CODE_COVERAGE = !!process.env.ENABLE_CODE_COVERAGE; +const ENABLE_CODE_COVERAGE = Boolean(process.env.ENABLE_CODE_COVERAGE); if (isProduction || process.env.INSTALL_PACKAGE) { process.env.PRETTIER_DIR = installPrettier(); } diff --git a/src/cli/format.js b/src/cli/format.js index 0af5fa202..d9fcc8d12 100644 --- a/src/cli/format.js +++ b/src/cli/format.js @@ -197,13 +197,12 @@ function format(context, input, opt) { repeat + " times." ); - // should be using `performance.now()`, but only `Date` is cross-platform enough - const now = Date.now ? () => Date.now() : () => +new Date(); let totalMs = 0; for (let i = 0; i < repeat; ++i) { - const startMs = now(); + // should be using `performance.now()`, but only `Date` is cross-platform enough + const startMs = Date.now(); prettier.formatWithCursor(input, opt); - totalMs += now() - startMs; + totalMs += Date.now() - startMs; } const averageMs = totalMs / repeat; const results = { diff --git a/src/config/resolve-config.js b/src/config/resolve-config.js index 011fb6a50..441e6ff9c 100644 --- a/src/config/resolve-config.js +++ b/src/config/resolve-config.js @@ -68,9 +68,9 @@ function getExplorer(opts) { function _resolveConfig(filePath, opts, sync) { opts = { useCache: true, ...opts }; const loadOpts = { - cache: !!opts.useCache, - sync: !!sync, - editorconfig: !!opts.editorconfig, + cache: Boolean(opts.useCache), + sync: Boolean(sync), + editorconfig: Boolean(opts.editorconfig), }; const { load, search } = getExplorer(loadOpts); const loadEditorConfig = resolveEditorConfig.getLoadFunction(loadOpts); diff --git a/src/document/doc-builders.js b/src/document/doc-builders.js index 3b3c90da8..31bff43cd 100644 --- a/src/document/doc-builders.js +++ b/src/document/doc-builders.js @@ -95,7 +95,7 @@ function group(contents, opts = {}) { type: "group", id: opts.id, contents, - break: !!opts.shouldBreak, + break: Boolean(opts.shouldBreak), expandedStates: opts.expandedStates, }; } diff --git a/src/language-css/utils.js b/src/language-css/utils.js index 4174f4c1d..62e38a4fe 100644 --- a/src/language-css/utils.js +++ b/src/language-css/utils.js @@ -96,7 +96,7 @@ function isSCSS(parser, text) { } function isSCSSVariable(node) { - return !!(node && node.type === "word" && node.value.startsWith("$")); + return Boolean(node && node.type === "word" && node.value.startsWith("$")); } function isWideKeywords(value) { diff --git a/src/language-graphql/printer-graphql.js b/src/language-graphql/printer-graphql.js index e25367de4..c39e67ae4 100644 --- a/src/language-graphql/printer-graphql.js +++ b/src/language-graphql/printer-graphql.js @@ -35,7 +35,7 @@ function genericPrint(path, options, print) { } case "OperationDefinition": { const hasOperation = options.originalText[locStart(n)] !== "{"; - const hasName = !!n.name; + const hasName = Boolean(n.name); return [ hasOperation ? n.operation : "", hasOperation && hasName ? [" ", path.call(print, "name")] : "", diff --git a/src/language-js/embed/html.js b/src/language-js/embed/html.js index cb2e41306..a7a014067 100644 --- a/src/language-js/embed/html.js +++ b/src/language-js/embed/html.js @@ -67,7 +67,7 @@ function format(path, print, textToDoc, options, { parser }) { continue; } - const placeholderIndex = +component; + const placeholderIndex = Number(component); parts.push(expressionDocs[placeholderIndex]); } diff --git a/src/language-js/needs-parens.js b/src/language-js/needs-parens.js index fccf92ca4..9ecb744b9 100644 --- a/src/language-js/needs-parens.js +++ b/src/language-js/needs-parens.js @@ -581,7 +581,7 @@ function needsParens(path, options) { case "ArrowFunctionExpression": switch (parent.type) { case "PipelineTopicExpression": - return !!(node.extra && node.extra.parenthesized); + return Boolean(node.extra && node.extra.parenthesized); case "BinaryExpression": return ( diff --git a/src/language-js/print/class.js b/src/language-js/print/class.js index 4f53b458c..f2d90dab8 100644 --- a/src/language-js/print/class.js +++ b/src/language-js/print/class.js @@ -101,8 +101,8 @@ function printHardlineAfterHeritage(node) { function hasMultipleHeritage(node) { return ( - ["superClass", "extends", "mixins", "implements"].filter( - (key) => !!node[key] + ["superClass", "extends", "mixins", "implements"].filter((key) => + Boolean(node[key]) ).length > 1 ); } diff --git a/src/language-js/print/jsx.js b/src/language-js/print/jsx.js index 1d619a6f6..366afb9e2 100644 --- a/src/language-js/print/jsx.js +++ b/src/language-js/print/jsx.js @@ -713,7 +713,7 @@ function printJsx(path, options, print) { case "JSXAttribute": return printJsxAttribute(path, options, print); case "JSXIdentifier": - return "" + n.name; + return String(n.name); case "JSXNamespacedName": return join(":", [ path.call(print, "namespace"), diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index a2fc6dbd5..2222cd9a4 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -490,7 +490,7 @@ function printPathNoParens(path, options, print, args) { return printNumber(n.raw); } if (typeof n.value !== "string") { - return "" + n.value; + return String(n.value); } return nodeStr(n, options); case "Directive": @@ -873,7 +873,7 @@ function printPathNoParens(path, options, print, args) { case "ArrayTypeAnnotation": return [path.call(print, "elementType"), "[]"]; case "BooleanLiteralTypeAnnotation": - return "" + n.value; + return String(n.value); case "EnumDeclaration": return ["enum ", path.call(print, "id"), " ", path.call(print, "body")]; diff --git a/src/language-js/utils.js b/src/language-js/utils.js index 744685678..e309db064 100644 --- a/src/language-js/utils.js +++ b/src/language-js/utils.js @@ -1160,7 +1160,7 @@ function getLeftMost(node) { function isBitwiseOperator(operator) { return ( - !!bitshiftOperators[operator] || + Boolean(bitshiftOperators[operator]) || operator === "|" || operator === "^" || operator === "&" diff --git a/src/language-markdown/printer-markdown.js b/src/language-markdown/printer-markdown.js index 179132dc2..15f7edd74 100644 --- a/src/language-markdown/printer-markdown.js +++ b/src/language-markdown/printer-markdown.js @@ -892,7 +892,7 @@ function clamp(value, min, max) { } function hasPrettierIgnore(path) { - const index = +path.getName(); + const index = Number(path.getName()); if (index === 0) { return false; diff --git a/src/main/multiparser.js b/src/main/multiparser.js index 5a5153f8b..50d4a440f 100644 --- a/src/main/multiparser.js +++ b/src/main/multiparser.js @@ -37,12 +37,12 @@ function textToDoc( ...parentOptions, ...partialNextOptions, parentParser: parentOptions.parser, - embeddedInHtml: !!( + embeddedInHtml: Boolean( parentOptions.embeddedInHtml || - parentOptions.parser === "html" || - parentOptions.parser === "vue" || - parentOptions.parser === "angular" || - parentOptions.parser === "lwc" + parentOptions.parser === "html" || + parentOptions.parser === "vue" || + parentOptions.parser === "angular" || + parentOptions.parser === "lwc" ), originalText: text, }, diff --git a/tests_integration/runPrettier.js b/tests_integration/runPrettier.js index 50848a9bd..7c83ae5de 100644 --- a/tests_integration/runPrettier.js +++ b/tests_integration/runPrettier.js @@ -63,8 +63,8 @@ function runPrettier(dir, args = [], options = {}) { const originalStdoutIsTTY = process.stdout.isTTY; process.chdir(normalizeDir(dir)); - process.stdin.isTTY = !!options.isTTY; - process.stdout.isTTY = !!options.stdoutIsTTY; + process.stdin.isTTY = Boolean(options.isTTY); + process.stdout.isTTY = Boolean(options.stdoutIsTTY); process.argv = ["path/to/node", "path/to/prettier/bin", ...args]; jest.resetModules(); @@ -77,7 +77,7 @@ function runPrettier(dir, args = [], options = {}) { .mockImplementation(() => SynchronousPromise.resolve(options.input || "")); jest .spyOn(require(thirdParty), "isCI") - .mockImplementation(() => !!options.ci); + .mockImplementation(() => Boolean(options.ci)); jest .spyOn(require(thirdParty), "cosmiconfig") .mockImplementation((moduleName, options) =>