diff --git a/.eslintrc.yml b/.eslintrc.yml index d0e2c6b1a..c111da02a 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -70,6 +70,7 @@ rules: unicorn/new-for-builtins: error unicorn/no-useless-undefined: error unicorn/prefer-includes: error + unicorn/prefer-number-properties: error unicorn/prefer-string-slice: error overrides: - files: diff --git a/scripts/release/steps/update-dependents-count.js b/scripts/release/steps/update-dependents-count.js index 1167c8182..1fb641bb6 100644 --- a/scripts/release/steps/update-dependents-count.js +++ b/scripts/release/steps/update-dependents-count.js @@ -15,7 +15,7 @@ async function update() { const dependentsCountNpm = Number( npmPage.match(/"dependentsCount":(\d+),/)[1] ); - if (isNaN(dependentsCountNpm)) { + if (Number.isNaN(dependentsCountNpm)) { throw new TypeError( "Invalid data from https://www.npmjs.com/package/prettier" ); @@ -35,7 +35,7 @@ async function update() { )[1] .replace(/,/g, "") ); - if (isNaN(dependentsCountNpm)) { + if (Number.isNaN(dependentsCountNpm)) { throw new TypeError( "Invalid data from https://github.com/prettier/prettier/network/dependents" ); diff --git a/src/document/doc-builders.js b/src/document/doc-builders.js index d6e64df5e..78bb870a8 100644 --- a/src/document/doc-builders.js +++ b/src/document/doc-builders.js @@ -94,7 +94,7 @@ function group(contents, opts) { * @returns Doc */ function dedentToRoot(contents) { - return align(-Infinity, contents); + return align(Number.NEGATIVE_INFINITY, contents); } /** @@ -219,7 +219,7 @@ function addAlignmentToDoc(doc, size, tabWidth) { aligned = align(size % tabWidth, aligned); // size is absolute from 0 and not relative to the current // indentation, so we use -Infinity to reset the indentation to 0 - aligned = align(-Infinity, aligned); + aligned = align(Number.NEGATIVE_INFINITY, aligned); } return aligned; } diff --git a/src/document/doc-debug.js b/src/document/doc-debug.js index 3d29a39a6..d9a9a010a 100644 --- a/src/document/doc-debug.js +++ b/src/document/doc-debug.js @@ -74,7 +74,7 @@ function printDoc(doc) { } if (doc.type === "align") { - return doc.n === -Infinity + return doc.n === Number.NEGATIVE_INFINITY ? "dedentToRoot(" + printDoc(doc.contents) + ")" : doc.n < 0 ? "dedent(" + printDoc(doc.contents) + ")" diff --git a/src/document/doc-printer.js b/src/document/doc-printer.js index 34f1521b9..354d3e68c 100644 --- a/src/document/doc-printer.js +++ b/src/document/doc-printer.js @@ -19,7 +19,7 @@ function makeIndent(ind, options) { } function makeAlign(indent, n, options) { - if (n === -Infinity) { + if (n === Number.NEGATIVE_INFINITY) { return indent.root || rootIndent(); } diff --git a/src/language-css/clean.js b/src/language-css/clean.js index 500742594..42cfa3ad6 100644 --- a/src/language-css/clean.js +++ b/src/language-css/clean.js @@ -144,7 +144,7 @@ function clean(ast, newObj, parent) { /([\d+.Ee-]+)([A-Za-z]*)/g, (match, numStr, unit) => { const num = Number(numStr); - return isNaN(num) ? match : num + unit.toLowerCase(); + return Number.isNaN(num) ? match : num + unit.toLowerCase(); } ); } diff --git a/src/language-html/utils.js b/src/language-html/utils.js index b5792fcf8..91c1cf9c0 100644 --- a/src/language-html/utils.js +++ b/src/language-html/utils.js @@ -557,7 +557,7 @@ function getNodeCssStyleWhiteSpace(node) { } function getMinIndentation(text) { - let minIndentation = Infinity; + let minIndentation = Number.POSITIVE_INFINITY; for (const lineText of text.split("\n")) { if (lineText.length === 0) { @@ -579,7 +579,7 @@ function getMinIndentation(text) { } } - return minIndentation === Infinity ? 0 : minIndentation; + return minIndentation === Number.POSITIVE_INFINITY ? 0 : minIndentation; } function dedentString(text, minIndent = getMinIndentation(text)) { diff --git a/src/language-js/print/template-literal.js b/src/language-js/print/template-literal.js index ab1289e0d..bf1902ce4 100644 --- a/src/language-js/print/template-literal.js +++ b/src/language-js/print/template-literal.js @@ -48,7 +48,10 @@ function printTemplateLiteral(path, print, options) { if (isSimple) { expressions = expressions.map( (doc) => - printDocToString(doc, { ...options, printWidth: Infinity }).formatted + printDocToString(doc, { + ...options, + printWidth: Number.POSITIVE_INFINITY, + }).formatted ); } @@ -96,7 +99,7 @@ function printTemplateLiteral(path, print, options) { const aligned = indentSize === 0 && quasi.value.raw.endsWith("\n") - ? align(-Infinity, printed) + ? align(Number.NEGATIVE_INFINITY, printed) : addAlignmentToDoc(printed, indentSize, tabWidth); parts.push(group(concat(["${", aligned, lineSuffixBoundary, "}"]))); @@ -130,7 +133,7 @@ function printJestEachTemplateLiteral(path, options, print) { "${" + printDocToString(doc, { ...options, - printWidth: Infinity, + printWidth: Number.POSITIVE_INFINITY, endOfLine: "lf", }).formatted + "}" diff --git a/src/language-yaml/utils.js b/src/language-yaml/utils.js index 5583ef694..a7e66520f 100644 --- a/src/language-yaml/utils.js +++ b/src/language-yaml/utils.js @@ -252,7 +252,7 @@ function getBlockValueLineContents( const leadingSpaceCount = node.indent === null - ? ((match) => (match ? match[1].length : Infinity))( + ? ((match) => (match ? match[1].length : Number.POSITIVE_INFINITY))( content.match(/^( *)\S/m) ) : node.indent - 1 + parentIndent; diff --git a/src/main/core-options.js b/src/main/core-options.js index 3f3039bd3..8f192daa4 100644 --- a/src/main/core-options.js +++ b/src/main/core-options.js @@ -54,7 +54,7 @@ const options = { category: CATEGORY_SPECIAL, type: "int", default: -1, - range: { start: -1, end: Infinity, step: 1 }, + range: { start: -1, end: Number.POSITIVE_INFINITY, step: 1 }, description: outdent` Print (to stderr) where a cursor at the given position would move to after formatting. This option cannot be used with --range-start and --range-end. @@ -195,14 +195,14 @@ const options = { type: "int", default: 80, description: "The line length where Prettier will try wrap.", - range: { start: 0, end: Infinity, step: 1 }, + range: { start: 0, end: Number.POSITIVE_INFINITY, step: 1 }, }, rangeEnd: { since: "1.4.0", category: CATEGORY_SPECIAL, type: "int", - default: Infinity, - range: { start: 0, end: Infinity, step: 1 }, + default: Number.POSITIVE_INFINITY, + range: { start: 0, end: Number.POSITIVE_INFINITY, step: 1 }, description: outdent` Format code ending at a given character offset (exclusive). The range will extend forwards to the end of the selected statement. @@ -215,7 +215,7 @@ const options = { category: CATEGORY_SPECIAL, type: "int", default: 0, - range: { start: 0, end: Infinity, step: 1 }, + range: { start: 0, end: Number.POSITIVE_INFINITY, step: 1 }, description: outdent` Format code starting at a given character offset. The range will extend backwards to the start of the first line containing the selected statement. @@ -239,7 +239,7 @@ const options = { category: CATEGORY_GLOBAL, default: 2, description: "Number of spaces per indentation level.", - range: { start: 0, end: Infinity, step: 1 }, + range: { start: 0, end: Number.POSITIVE_INFINITY, step: 1 }, }, useTabs: { since: "1.0.0", diff --git a/src/main/core.js b/src/main/core.js index e4e8cf99f..640a2a55e 100644 --- a/src/main/core.js +++ b/src/main/core.js @@ -162,7 +162,7 @@ function formatRange(originalText, opts) { { ...opts, rangeStart: 0, - rangeEnd: Infinity, + rangeEnd: Number.POSITIVE_INFINITY, // Track the cursor offset only if it's within our range cursorOffset: opts.cursorOffset > rangeStart && opts.cursorOffset < rangeEnd @@ -209,7 +209,7 @@ function formatRange(originalText, opts) { function ensureIndexInText(text, index, defaultValue) { if ( typeof index !== "number" || - isNaN(index) || + Number.isNaN(index) || index < 0 || index > text.length ) { diff --git a/tests/html/comments/jsfmt.spec.js b/tests/html/comments/jsfmt.spec.js index 27febda27..fb2a2d11b 100644 --- a/tests/html/comments/jsfmt.spec.js +++ b/tests/html/comments/jsfmt.spec.js @@ -1,5 +1,5 @@ run_spec(__dirname, ["html"]); run_spec(__dirname, ["html"], { printWidth: 1 }); -run_spec(__dirname, ["html"], { printWidth: Infinity }); +run_spec(__dirname, ["html"], { printWidth: Number.POSITIVE_INFINITY }); run_spec(__dirname, ["html"], { htmlWhitespaceSensitivity: "strict" }); run_spec(__dirname, ["html"], { htmlWhitespaceSensitivity: "ignore" }); diff --git a/tests/html/tags/jsfmt.spec.js b/tests/html/tags/jsfmt.spec.js index 27febda27..fb2a2d11b 100644 --- a/tests/html/tags/jsfmt.spec.js +++ b/tests/html/tags/jsfmt.spec.js @@ -1,5 +1,5 @@ run_spec(__dirname, ["html"]); run_spec(__dirname, ["html"], { printWidth: 1 }); -run_spec(__dirname, ["html"], { printWidth: Infinity }); +run_spec(__dirname, ["html"], { printWidth: Number.POSITIVE_INFINITY }); run_spec(__dirname, ["html"], { htmlWhitespaceSensitivity: "strict" }); run_spec(__dirname, ["html"], { htmlWhitespaceSensitivity: "ignore" }); diff --git a/tests_config/utils/create-snapshot.js b/tests_config/utils/create-snapshot.js index 32cf77d33..0db847e48 100644 --- a/tests_config/utils/create-snapshot.js +++ b/tests_config/utils/create-snapshot.js @@ -10,7 +10,7 @@ function printSeparator(width, description) { } function stringify(value) { - return value === Infinity + return value === Number.POSITIVE_INFINITY ? "Infinity" : Array.isArray(value) ? `[${value.map((v) => JSON.stringify(v)).join(", ")}]` diff --git a/tests_config/utils/stringify-options-for-title.js b/tests_config/utils/stringify-options-for-title.js index 2ea06ce77..dbc95627f 100644 --- a/tests_config/utils/stringify-options-for-title.js +++ b/tests_config/utils/stringify-options-for-title.js @@ -4,7 +4,7 @@ function stringifyOptions(options) { const string = JSON.stringify(options || {}, (key, value) => key === "plugins" || key === "errors" ? undefined - : value === Infinity + : value === Number.POSITIVE_INFINITY ? "Infinity" : value ); diff --git a/tests_config/utils/visualize-range.js b/tests_config/utils/visualize-range.js index d8125a06a..7fca537b0 100644 --- a/tests_config/utils/visualize-range.js +++ b/tests_config/utils/visualize-range.js @@ -2,7 +2,10 @@ const LinesAndColumns = require("lines-and-columns").default; const { codeFrameColumns } = require("@babel/code-frame"); -const codeFrameColumnsOptions = { linesAbove: Infinity, linesBelow: Infinity }; +const codeFrameColumnsOptions = { + linesAbove: Number.POSITIVE_INFINITY, + linesBelow: Number.POSITIVE_INFINITY, +}; const locationForRange = (text, rangeStart, rangeEnd) => { if (rangeStart > rangeEnd) { diff --git a/website/playground/Playground.js b/website/playground/Playground.js index 14771933a..01b41baad 100644 --- a/website/playground/Playground.js +++ b/website/playground/Playground.js @@ -118,7 +118,7 @@ class Playground extends React.Component { this.setState((state) => { const options = { ...state.options }; - if (option.type === "int" && isNaN(value)) { + if (option.type === "int" && Number.isNaN(value)) { delete options[option.name]; } else { options[option.name] = value; diff --git a/website/playground/sidebar/inputs.js b/website/playground/sidebar/inputs.js index eae51b615..b4c15099c 100644 --- a/website/playground/sidebar/inputs.js +++ b/website/playground/sidebar/inputs.js @@ -46,7 +46,7 @@ export function NumberInput({ max={max} step={step} value={value} - onChange={(ev) => onChange(parseInt(ev.target.value, 10))} + onChange={(ev) => onChange(Number.parseInt(ev.target.value, 10))} /> );