Enable unicorn/prefer-number-properties (#9935)

This commit is contained in:
fisker Cheung authored and GitHub committed 2020-12-21 17:09:20 +08:00
1 parent 6843b51113
commit f0f18d3090
18 files changed
+35 -28

No files matched your search

+1
View File
@@ -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:
@@ -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"
);
+2 -2
View File
@@ -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;
}
+1 -1
View File
@@ -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) + ")"
+1 -1
View File
@@ -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();
}
+1 -1
View File
@@ -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();
}
);
}
+2 -2
View File
@@ -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)) {
+6 -3
View File
@@ -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 +
"}"
+1 -1
View File
@@ -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;
+6 -6
View File
@@ -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",
+2 -2
View File
@@ -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
) {
+1 -1
View File
@@ -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" });
+1 -1
View File
@@ -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" });
+1 -1
View File
@@ -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(", ")}]`
@@ -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
);
+4 -1
View File
@@ -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) {
+1 -1
View File
@@ -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;
+1 -1
View File
@@ -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))}
/>
</label>
);