oxlint-plugin-eslint 1.52.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +36 -0
- package/common/ast-utils.cjs +5843 -0
- package/common/chunk.cjs +9 -0
- package/common/eslint-utils.cjs +1633 -0
- package/common/fix-tracker.cjs +89 -0
- package/common/ignore.cjs +123 -0
- package/common/keywords.cjs +15 -0
- package/common/regexpp.cjs +1720 -0
- package/common/regular-expressions.cjs +40 -0
- package/common/string-utils.cjs +39 -0
- package/index.js +7477 -0
- package/package.json +34 -0
- package/rules/accessor-pairs.cjs +233 -0
- package/rules/array-bracket-newline.cjs +172 -0
- package/rules/array-bracket-spacing.cjs +184 -0
- package/rules/array-callback-return.cjs +262 -0
- package/rules/array-element-newline.cjs +178 -0
- package/rules/arrow-body-style.cjs +135 -0
- package/rules/arrow-parens.cjs +130 -0
- package/rules/arrow-spacing.cjs +129 -0
- package/rules/block-scoped-var.cjs +101 -0
- package/rules/block-spacing.cjs +125 -0
- package/rules/brace-style.cjs +141 -0
- package/rules/callback-return.cjs +117 -0
- package/rules/camelcase.cjs +194 -0
- package/rules/capitalized-comments.cjs +168 -0
- package/rules/class-methods-use-this.cjs +155 -0
- package/rules/comma-dangle.cjs +274 -0
- package/rules/comma-spacing.cjs +111 -0
- package/rules/comma-style.cjs +180 -0
- package/rules/complexity.cjs +100 -0
- package/rules/computed-property-spacing.cjs +155 -0
- package/rules/consistent-return.cjs +113 -0
- package/rules/consistent-this.cjs +109 -0
- package/rules/constructor-super.cjs +207 -0
- package/rules/curly.cjs +173 -0
- package/rules/default-case-last.cjs +36 -0
- package/rules/default-case.cjs +50 -0
- package/rules/default-param-last.cjs +61 -0
- package/rules/dot-location.cjs +87 -0
- package/rules/dot-notation.cjs +77 -0
- package/rules/eol-last.cjs +91 -0
- package/rules/eqeqeq.cjs +127 -0
- package/rules/for-direction.cjs +94 -0
- package/rules/func-call-spacing.cjs +127 -0
- package/rules/func-name-matching.cjs +138 -0
- package/rules/func-names.cjs +121 -0
- package/rules/func-style.cjs +103 -0
- package/rules/function-call-argument-newline.cjs +109 -0
- package/rules/function-paren-newline.cjs +173 -0
- package/rules/generator-star-spacing.cjs +174 -0
- package/rules/getter-return.cjs +129 -0
- package/rules/global-require.cjs +84 -0
- package/rules/grouped-accessor-pairs.cjs +146 -0
- package/rules/guard-for-in.cjs +39 -0
- package/rules/handle-callback-err.cjs +86 -0
- package/rules/id-blacklist.cjs +135 -0
- package/rules/id-denylist.cjs +126 -0
- package/rules/id-length.cjs +119 -0
- package/rules/id-match.cjs +139 -0
- package/rules/implicit-arrow-linebreak.cjs +76 -0
- package/rules/indent-legacy.cjs +520 -0
- package/rules/indent.cjs +842 -0
- package/rules/init-declarations.cjs +88 -0
- package/rules/jsx-quotes.cjs +84 -0
- package/rules/key-spacing.cjs +399 -0
- package/rules/keyword-spacing.cjs +464 -0
- package/rules/line-comment-position.cjs +75 -0
- package/rules/linebreak-style.cjs +89 -0
- package/rules/lines-around-comment.cjs +290 -0
- package/rules/lines-around-directive.cjs +129 -0
- package/rules/lines-between-class-members.cjs +212 -0
- package/rules/logical-assignment-operators.cjs +305 -0
- package/rules/max-classes-per-file.cjs +61 -0
- package/rules/max-depth.cjs +117 -0
- package/rules/max-len.cjs +256 -0
- package/rules/max-lines-per-function.cjs +116 -0
- package/rules/max-lines.cjs +109 -0
- package/rules/max-nested-callbacks.cjs +78 -0
- package/rules/max-params.cjs +78 -0
- package/rules/max-statements-per-line.cjs +147 -0
- package/rules/max-statements.cjs +119 -0
- package/rules/multiline-comment-style.cjs +319 -0
- package/rules/multiline-ternary.cjs +106 -0
- package/rules/new-cap.cjs +139 -0
- package/rules/new-parens.cjs +70 -0
- package/rules/newline-after-var.cjs +159 -0
- package/rules/newline-before-return.cjs +128 -0
- package/rules/newline-per-chained-call.cjs +100 -0
- package/rules/no-alert.cjs +84 -0
- package/rules/no-array-constructor.cjs +91 -0
- package/rules/no-async-promise-executor.cjs +33 -0
- package/rules/no-await-in-loop.cjs +75 -0
- package/rules/no-bitwise.cjs +106 -0
- package/rules/no-buffer-constructor.cjs +51 -0
- package/rules/no-caller.cjs +34 -0
- package/rules/no-case-declarations.cjs +56 -0
- package/rules/no-catch-shadow.cjs +57 -0
- package/rules/no-class-assign.cjs +55 -0
- package/rules/no-compare-neg-zero.cjs +52 -0
- package/rules/no-cond-assign.cjs +104 -0
- package/rules/no-confusing-arrow.cjs +87 -0
- package/rules/no-console.cjs +138 -0
- package/rules/no-const-assign.cjs +50 -0
- package/rules/no-constant-binary-expression.cjs +284 -0
- package/rules/no-constant-condition.cjs +119 -0
- package/rules/no-constructor-return.cjs +43 -0
- package/rules/no-continue.cjs +33 -0
- package/rules/no-control-regex.cjs +74 -0
- package/rules/no-debugger.cjs +33 -0
- package/rules/no-delete-var.cjs +32 -0
- package/rules/no-div-regex.cjs +39 -0
- package/rules/no-dupe-args.cjs +61 -0
- package/rules/no-dupe-class-members.cjs +77 -0
- package/rules/no-dupe-else-if.cjs +74 -0
- package/rules/no-dupe-keys.cjs +88 -0
- package/rules/no-duplicate-case.cjs +49 -0
- package/rules/no-duplicate-imports.cjs +166 -0
- package/rules/no-else-return.cjs +182 -0
- package/rules/no-empty-character-class.cjs +47 -0
- package/rules/no-empty-function.cjs +141 -0
- package/rules/no-empty-pattern.cjs +50 -0
- package/rules/no-empty-static-block.cjs +50 -0
- package/rules/no-empty.cjs +78 -0
- package/rules/no-eq-null.cjs +34 -0
- package/rules/no-eval.cjs +165 -0
- package/rules/no-ex-assign.cjs +45 -0
- package/rules/no-extend-native.cjs +103 -0
- package/rules/no-extra-bind.cjs +127 -0
- package/rules/no-extra-boolean-cast.cjs +159 -0
- package/rules/no-extra-label.cjs +116 -0
- package/rules/no-extra-parens.cjs +665 -0
- package/rules/no-extra-semi.cjs +103 -0
- package/rules/no-fallthrough.cjs +136 -0
- package/rules/no-floating-decimal.cjs +66 -0
- package/rules/no-func-assign.cjs +63 -0
- package/rules/no-global-assign.cjs +62 -0
- package/rules/no-implicit-coercion.cjs +219 -0
- package/rules/no-implicit-globals.cjs +77 -0
- package/rules/no-implied-eval.cjs +87 -0
- package/rules/no-import-assign.cjs +123 -0
- package/rules/no-inline-comments.cjs +57 -0
- package/rules/no-inner-declarations.cjs +82 -0
- package/rules/no-invalid-regexp.cjs +130 -0
- package/rules/no-invalid-this.cjs +90 -0
- package/rules/no-irregular-whitespace.cjs +157 -0
- package/rules/no-iterator.cjs +34 -0
- package/rules/no-label-var.cjs +46 -0
- package/rules/no-labels.cjs +106 -0
- package/rules/no-lone-blocks.cjs +75 -0
- package/rules/no-lonely-if.cjs +42 -0
- package/rules/no-loop-func.cjs +143 -0
- package/rules/no-loss-of-precision.cjs +160 -0
- package/rules/no-magic-numbers.cjs +222 -0
- package/rules/no-misleading-character-class.cjs +500 -0
- package/rules/no-mixed-operators.cjs +197 -0
- package/rules/no-mixed-requires.cjs +117 -0
- package/rules/no-mixed-spaces-and-tabs.cjs +87 -0
- package/rules/no-multi-assign.cjs +38 -0
- package/rules/no-multi-spaces.cjs +95 -0
- package/rules/no-multi-str.cjs +44 -0
- package/rules/no-multiple-empty-lines.cjs +112 -0
- package/rules/no-native-reassign.cjs +72 -0
- package/rules/no-negated-condition.cjs +77 -0
- package/rules/no-negated-in-lhs.cjs +43 -0
- package/rules/no-nested-ternary.cjs +33 -0
- package/rules/no-new-func.cjs +50 -0
- package/rules/no-new-native-nonconstructor.cjs +42 -0
- package/rules/no-new-object.cjs +47 -0
- package/rules/no-new-require.cjs +50 -0
- package/rules/no-new-symbol.cjs +48 -0
- package/rules/no-new-wrappers.cjs +44 -0
- package/rules/no-new.cjs +33 -0
- package/rules/no-nonoctal-decimal-escape.cjs +80 -0
- package/rules/no-obj-calls.cjs +64 -0
- package/rules/no-object-constructor.cjs +69 -0
- package/rules/no-octal-escape.cjs +35 -0
- package/rules/no-octal.cjs +32 -0
- package/rules/no-param-reassign.cjs +135 -0
- package/rules/no-path-concat.cjs +52 -0
- package/rules/no-plusplus.cjs +64 -0
- package/rules/no-process-env.cjs +51 -0
- package/rules/no-process-exit.cjs +50 -0
- package/rules/no-promise-executor-return.cjs +131 -0
- package/rules/no-proto.cjs +34 -0
- package/rules/no-prototype-builtins.cjs +85 -0
- package/rules/no-redeclare.cjs +103 -0
- package/rules/no-regex-spaces.cjs +115 -0
- package/rules/no-restricted-exports.cjs +117 -0
- package/rules/no-restricted-globals.cjs +147 -0
- package/rules/no-restricted-imports.cjs +421 -0
- package/rules/no-restricted-modules.cjs +144 -0
- package/rules/no-restricted-properties.cjs +123 -0
- package/rules/no-restricted-syntax.cjs +49 -0
- package/rules/no-return-assign.cjs +45 -0
- package/rules/no-return-await.cjs +88 -0
- package/rules/no-script-url.cjs +49 -0
- package/rules/no-self-assign.cjs +88 -0
- package/rules/no-self-compare.cjs +53 -0
- package/rules/no-sequences.cjs +81 -0
- package/rules/no-setter-return.cjs +110 -0
- package/rules/no-shadow-restricted-names.cjs +60 -0
- package/rules/no-shadow.cjs +302 -0
- package/rules/no-spaced-func.cjs +69 -0
- package/rules/no-sparse-arrays.cjs +40 -0
- package/rules/no-sync.cjs +58 -0
- package/rules/no-tabs.cjs +73 -0
- package/rules/no-template-curly-in-string.cjs +33 -0
- package/rules/no-ternary.cjs +33 -0
- package/rules/no-this-before-super.cjs +177 -0
- package/rules/no-throw-literal.cjs +40 -0
- package/rules/no-trailing-spaces.cjs +123 -0
- package/rules/no-unassigned-vars.cjs +54 -0
- package/rules/no-undef-init.cjs +46 -0
- package/rules/no-undef.cjs +51 -0
- package/rules/no-undefined.cjs +58 -0
- package/rules/no-underscore-dangle.cjs +211 -0
- package/rules/no-unexpected-multiline.cjs +76 -0
- package/rules/no-unmodified-loop-condition.cjs +849 -0
- package/rules/no-unneeded-ternary.cjs +112 -0
- package/rules/no-unreachable-loop.cjs +128 -0
- package/rules/no-unreachable.cjs +195 -0
- package/rules/no-unsafe-finally.cjs +67 -0
- package/rules/no-unsafe-negation.cjs +86 -0
- package/rules/no-unsafe-optional-chaining.cjs +164 -0
- package/rules/no-unused-expressions.cjs +163 -0
- package/rules/no-unused-labels.cjs +97 -0
- package/rules/no-unused-private-class-members.cjs +69 -0
- package/rules/no-unused-vars.cjs +639 -0
- package/rules/no-use-before-define.cjs +245 -0
- package/rules/no-useless-assignment.cjs +250 -0
- package/rules/no-useless-backreference.cjs +137 -0
- package/rules/no-useless-call.cjs +57 -0
- package/rules/no-useless-catch.cjs +38 -0
- package/rules/no-useless-computed-key.cjs +106 -0
- package/rules/no-useless-concat.cjs +78 -0
- package/rules/no-useless-constructor.cjs +152 -0
- package/rules/no-useless-escape.cjs +164 -0
- package/rules/no-useless-rename.cjs +94 -0
- package/rules/no-useless-return.cjs +229 -0
- package/rules/no-var.cjs +235 -0
- package/rules/no-void.cjs +39 -0
- package/rules/no-warning-comments.cjs +112 -0
- package/rules/no-whitespace-before-property.cjs +72 -0
- package/rules/no-with.cjs +32 -0
- package/rules/nonblock-statement-body-position.cjs +107 -0
- package/rules/object-curly-newline.cjs +194 -0
- package/rules/object-curly-spacing.cjs +225 -0
- package/rules/object-property-newline.cjs +81 -0
- package/rules/object-shorthand.cjs +284 -0
- package/rules/one-var-declaration-per-line.cjs +75 -0
- package/rules/one-var.cjs +378 -0
- package/rules/operator-assignment.cjs +146 -0
- package/rules/operator-linebreak.cjs +150 -0
- package/rules/padded-blocks.cjs +192 -0
- package/rules/padding-line-between-statements.cjs +377 -0
- package/rules/prefer-arrow-callback.cjs +185 -0
- package/rules/prefer-const.cjs +240 -0
- package/rules/prefer-destructuring.cjs +188 -0
- package/rules/prefer-exponentiation-operator.cjs +100 -0
- package/rules/prefer-named-capture-group.cjs +111 -0
- package/rules/prefer-numeric-literals.cjs +75 -0
- package/rules/prefer-object-has-own.cjs +54 -0
- package/rules/prefer-object-spread.cjs +170 -0
- package/rules/prefer-promise-reject-errors.cjs +62 -0
- package/rules/prefer-reflect.cjs +100 -0
- package/rules/prefer-regex-literals.cjs +256 -0
- package/rules/prefer-rest-params.cjs +80 -0
- package/rules/prefer-spread.cjs +57 -0
- package/rules/prefer-template.cjs +147 -0
- package/rules/preserve-caught-error.cjs +196 -0
- package/rules/quote-props.cjs +224 -0
- package/rules/quotes.cjs +187 -0
- package/rules/radix.cjs +124 -0
- package/rules/require-atomic-updates.cjs +197 -0
- package/rules/require-await.cjs +90 -0
- package/rules/require-unicode-regexp.cjs +101 -0
- package/rules/require-yield.cjs +56 -0
- package/rules/rest-spread-spacing.cjs +99 -0
- package/rules/semi-spacing.cjs +215 -0
- package/rules/semi-style.cjs +116 -0
- package/rules/semi.cjs +239 -0
- package/rules/sort-imports.cjs +140 -0
- package/rules/sort-keys.cjs +165 -0
- package/rules/sort-vars.cjs +50 -0
- package/rules/space-before-blocks.cjs +132 -0
- package/rules/space-before-function-paren.cjs +126 -0
- package/rules/space-in-parens.cjs +175 -0
- package/rules/space-infix-ops.cjs +133 -0
- package/rules/space-unary-ops.cjs +237 -0
- package/rules/spaced-comment.cjs +233 -0
- package/rules/strict.cjs +206 -0
- package/rules/switch-colon-spacing.cjs +114 -0
- package/rules/symbol-description.cjs +49 -0
- package/rules/template-curly-spacing.cjs +112 -0
- package/rules/template-tag-spacing.cjs +82 -0
- package/rules/unicode-bom.cjs +52 -0
- package/rules/use-isnan.cjs +144 -0
- package/rules/valid-typeof.cjs +104 -0
- package/rules/vars-on-top.cjs +94 -0
- package/rules/wrap-iife.cjs +131 -0
- package/rules/wrap-regex.cjs +57 -0
- package/rules/yield-star-spacing.cjs +115 -0
- package/rules/yoda.cjs +185 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
//#region ../../node_modules/.pnpm/eslint@9.39.3_jiti@2.6.1/node_modules/eslint/lib/rules/no-loss-of-precision.js
|
|
2
|
+
/**
|
|
3
|
+
* @fileoverview Rule to flag numbers that will lose significant figure precision at runtime
|
|
4
|
+
* @author Jacob Moore
|
|
5
|
+
*/
|
|
6
|
+
var require_no_loss_of_precision = /* @__PURE__ */ require("../common/chunk.cjs").t(((exports, module) => {
|
|
7
|
+
/** Class representing a number in scientific notation. */
|
|
8
|
+
var ScientificNotation = class {
|
|
9
|
+
/** @type {string} The digits of the coefficient. A decimal point is implied after the first digit. */
|
|
10
|
+
coefficient;
|
|
11
|
+
/** @type {number} The order of magnitude. */
|
|
12
|
+
magnitude;
|
|
13
|
+
constructor(coefficient, magnitude) {
|
|
14
|
+
this.coefficient = coefficient, this.magnitude = magnitude;
|
|
15
|
+
}
|
|
16
|
+
/* c8 ignore start -- debug only */
|
|
17
|
+
toString() {
|
|
18
|
+
return `${this.coefficient[0]}${this.coefficient.length > 1 ? `.${this.coefficient.slice(1)}` : ""}e${this.magnitude}`;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Returns whether the node is number literal
|
|
23
|
+
* @param {Node} node the node literal being evaluated
|
|
24
|
+
* @returns {boolean} true if the node is a number literal
|
|
25
|
+
*/
|
|
26
|
+
function isNumber(node) {
|
|
27
|
+
return typeof node.value == "number";
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Gets the source code of the given number literal. Removes `_` numeric separators from the result.
|
|
31
|
+
* @param {Node} node the number `Literal` node
|
|
32
|
+
* @returns {string} raw source code of the literal, without numeric separators
|
|
33
|
+
*/
|
|
34
|
+
function getRaw(node) {
|
|
35
|
+
return node.raw.replace(/_/gu, "");
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Checks whether the number is base ten
|
|
39
|
+
* @param {ASTNode} node the node being evaluated
|
|
40
|
+
* @returns {boolean} true if the node is in base ten
|
|
41
|
+
*/
|
|
42
|
+
function isBaseTen(node) {
|
|
43
|
+
return [
|
|
44
|
+
"0x",
|
|
45
|
+
"0X",
|
|
46
|
+
"0b",
|
|
47
|
+
"0B",
|
|
48
|
+
"0o",
|
|
49
|
+
"0O"
|
|
50
|
+
].every((prefix) => !node.raw.startsWith(prefix)) && !/^0[0-7]+$/u.test(node.raw);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Checks that the user-intended non-base ten number equals the actual number after is has been converted to the Number type
|
|
54
|
+
* @param {Node} node the node being evaluated
|
|
55
|
+
* @returns {boolean} true if they do not match
|
|
56
|
+
*/
|
|
57
|
+
function notBaseTenLosesPrecision(node) {
|
|
58
|
+
let rawString = getRaw(node).toUpperCase(), base;
|
|
59
|
+
return base = rawString.startsWith("0B") ? 2 : rawString.startsWith("0X") ? 16 : 8, !rawString.endsWith(node.value.toString(base).toUpperCase());
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Returns the number stripped of leading zeros
|
|
63
|
+
* @param {string} numberAsString the string representation of the number
|
|
64
|
+
* @returns {string} the stripped string
|
|
65
|
+
*/
|
|
66
|
+
function removeLeadingZeros(numberAsString) {
|
|
67
|
+
for (let i = 0; i < numberAsString.length; i++) if (numberAsString[i] !== "0") return numberAsString.slice(i);
|
|
68
|
+
return numberAsString;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Returns the number stripped of trailing zeros
|
|
72
|
+
* @param {string} numberAsString the string representation of the number
|
|
73
|
+
* @returns {string} the stripped string
|
|
74
|
+
*/
|
|
75
|
+
function removeTrailingZeros(numberAsString) {
|
|
76
|
+
for (let i = numberAsString.length - 1; i >= 0; i--) if (numberAsString[i] !== "0") return numberAsString.slice(0, i + 1);
|
|
77
|
+
return numberAsString;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Converts an integer to an object containing the integer's coefficient and order of magnitude
|
|
81
|
+
* @param {string} stringInteger the string representation of the integer being converted
|
|
82
|
+
* @returns {ScientificNotation} the object containing the integer's coefficient and order of magnitude
|
|
83
|
+
*/
|
|
84
|
+
function normalizeInteger(stringInteger) {
|
|
85
|
+
let trimmedInteger = removeLeadingZeros(stringInteger);
|
|
86
|
+
return new ScientificNotation(removeTrailingZeros(trimmedInteger), trimmedInteger.length - 1);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Converts a float to an object containing the float's coefficient and order of magnitude
|
|
90
|
+
* @param {string} stringFloat the string representation of the float being converted
|
|
91
|
+
* @returns {ScientificNotation} the object containing the float's coefficient and order of magnitude
|
|
92
|
+
*/
|
|
93
|
+
function normalizeFloat(stringFloat) {
|
|
94
|
+
let trimmedFloat = removeLeadingZeros(stringFloat), indexOfDecimalPoint = trimmedFloat.indexOf(".");
|
|
95
|
+
switch (indexOfDecimalPoint) {
|
|
96
|
+
case 0: {
|
|
97
|
+
let significantDigits = removeLeadingZeros(trimmedFloat.slice(1));
|
|
98
|
+
return new ScientificNotation(significantDigits, significantDigits.length - trimmedFloat.length);
|
|
99
|
+
}
|
|
100
|
+
case -1: return new ScientificNotation(trimmedFloat, trimmedFloat.length - 1);
|
|
101
|
+
default: return new ScientificNotation(trimmedFloat.replace(".", ""), indexOfDecimalPoint - 1);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Converts a base ten number to proper scientific notation
|
|
106
|
+
* @param {string} stringNumber the string representation of the base ten number to be converted
|
|
107
|
+
* @param {boolean} parseAsFloat if true, the coefficient will be always parsed as a float, regardless of whether a decimal point is present
|
|
108
|
+
* @returns {ScientificNotation} the object containing the number's coefficient and order of magnitude
|
|
109
|
+
*/
|
|
110
|
+
function convertNumberToScientificNotation(stringNumber, parseAsFloat) {
|
|
111
|
+
let splitNumber = stringNumber.split("e"), originalCoefficient = splitNumber[0], normalizedNumber = parseAsFloat || stringNumber.includes(".") ? normalizeFloat(originalCoefficient) : normalizeInteger(originalCoefficient);
|
|
112
|
+
return splitNumber.length > 1 && (normalizedNumber.magnitude += parseInt(splitNumber[1], 10)), normalizedNumber;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Checks that the user-intended base ten number equals the actual number after is has been converted to the Number type
|
|
116
|
+
* @param {Node} node the node being evaluated
|
|
117
|
+
* @returns {boolean} true if they do not match
|
|
118
|
+
*/
|
|
119
|
+
function baseTenLosesPrecision(node) {
|
|
120
|
+
let normalizedRawNumber = convertNumberToScientificNotation(getRaw(node).toLowerCase(), !1), requestedPrecision = normalizedRawNumber.coefficient.length;
|
|
121
|
+
if (requestedPrecision > 100) return !0;
|
|
122
|
+
let normalizedStoredNumber = convertNumberToScientificNotation(node.value.toPrecision(requestedPrecision), !0);
|
|
123
|
+
return normalizedRawNumber.magnitude !== normalizedStoredNumber.magnitude || normalizedRawNumber.coefficient !== normalizedStoredNumber.coefficient;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Checks that the user-intended number equals the actual number after is has been converted to the Number type
|
|
127
|
+
* @param {Node} node the node being evaluated
|
|
128
|
+
* @returns {boolean} true if they do not match
|
|
129
|
+
*/
|
|
130
|
+
function losesPrecision(node) {
|
|
131
|
+
return isBaseTen(node) ? baseTenLosesPrecision(node) : notBaseTenLosesPrecision(node);
|
|
132
|
+
}
|
|
133
|
+
/** @type {import('../types').Rule.RuleModule} */
|
|
134
|
+
module.exports = {
|
|
135
|
+
meta: {
|
|
136
|
+
type: "problem",
|
|
137
|
+
dialects: ["typescript", "javascript"],
|
|
138
|
+
language: "javascript",
|
|
139
|
+
docs: {
|
|
140
|
+
description: "Disallow literal numbers that lose precision",
|
|
141
|
+
recommended: !0,
|
|
142
|
+
url: "https://eslint.org/docs/latest/rules/no-loss-of-precision"
|
|
143
|
+
},
|
|
144
|
+
schema: [],
|
|
145
|
+
messages: { noLossOfPrecision: "This number literal will lose precision at runtime." }
|
|
146
|
+
},
|
|
147
|
+
create(context) {
|
|
148
|
+
return { Literal(node) {
|
|
149
|
+
node.value && isNumber(node) && losesPrecision(node) && context.report({
|
|
150
|
+
messageId: "noLossOfPrecision",
|
|
151
|
+
node
|
|
152
|
+
});
|
|
153
|
+
} };
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
}));
|
|
157
|
+
//#endregion
|
|
158
|
+
//#region src-js/generated/plugin-eslint/rules/no-loss-of-precision.cjs
|
|
159
|
+
module.exports = require_no_loss_of_precision().create;
|
|
160
|
+
//#endregion
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
const require_chunk = require("../common/chunk.cjs"), require_ast_utils$1 = require("../common/ast-utils.cjs");
|
|
2
|
+
//#region ../../node_modules/.pnpm/eslint@9.39.3_jiti@2.6.1/node_modules/eslint/lib/rules/no-magic-numbers.js
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview Rule to flag statements that use magic numbers (adapted from https://github.com/danielstjules/buddy.js)
|
|
5
|
+
* @author Vincent Lemeunier
|
|
6
|
+
*/
|
|
7
|
+
var require_no_magic_numbers = /* @__PURE__ */ require_chunk.t(((exports, module) => {
|
|
8
|
+
let astUtils = require_ast_utils$1.t(), MAX_ARRAY_LENGTH = 2 ** 32 - 1;
|
|
9
|
+
/**
|
|
10
|
+
* Convert the value to bigint if it's a string. Otherwise return the value as-is.
|
|
11
|
+
* @param {bigint|number|string} x The value to normalize.
|
|
12
|
+
* @returns {bigint|number} The normalized value.
|
|
13
|
+
*/
|
|
14
|
+
function normalizeIgnoreValue(x) {
|
|
15
|
+
return typeof x == "string" ? BigInt(x.slice(0, -1)) : x;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Checks if the node parent is a TypeScript enum member
|
|
19
|
+
* @param {ASTNode} node The node to be validated
|
|
20
|
+
* @returns {boolean} True if the node parent is a TypeScript enum member
|
|
21
|
+
*/
|
|
22
|
+
function isParentTSEnumDeclaration(node) {
|
|
23
|
+
return node.parent.type === "TSEnumMember";
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Checks if the node is a valid TypeScript numeric literal type.
|
|
27
|
+
* @param {ASTNode} node The node to be validated
|
|
28
|
+
* @returns {boolean} True if the node is a TypeScript numeric literal type
|
|
29
|
+
*/
|
|
30
|
+
function isTSNumericLiteralType(node) {
|
|
31
|
+
let ancestor = node.parent;
|
|
32
|
+
for (; ancestor.parent.type === "TSUnionType";) ancestor = ancestor.parent;
|
|
33
|
+
return ancestor.parent.type === "TSTypeAliasDeclaration";
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Checks if the node parent is a readonly class property
|
|
37
|
+
* @param {ASTNode} node The node to be validated
|
|
38
|
+
* @returns {boolean} True if the node parent is a readonly class property
|
|
39
|
+
*/
|
|
40
|
+
function isParentTSReadonlyPropertyDefinition(node) {
|
|
41
|
+
return !!(node.parent?.type === "PropertyDefinition" && node.parent.readonly);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Checks if the node is part of a type indexed access (eg. Foo[4])
|
|
45
|
+
* @param {ASTNode} node The node to be validated
|
|
46
|
+
* @returns {boolean} True if the node is part of an indexed access
|
|
47
|
+
*/
|
|
48
|
+
function isAncestorTSIndexedAccessType(node) {
|
|
49
|
+
let ancestor = node.parent;
|
|
50
|
+
for (; ancestor.parent.type === "TSUnionType" || ancestor.parent.type === "TSIntersectionType";) ancestor = ancestor.parent;
|
|
51
|
+
return ancestor.parent.type === "TSIndexedAccessType";
|
|
52
|
+
}
|
|
53
|
+
/** @type {import('../types').Rule.RuleModule} */
|
|
54
|
+
module.exports = {
|
|
55
|
+
meta: {
|
|
56
|
+
type: "suggestion",
|
|
57
|
+
dialects: ["typescript", "javascript"],
|
|
58
|
+
language: "javascript",
|
|
59
|
+
docs: {
|
|
60
|
+
description: "Disallow magic numbers",
|
|
61
|
+
recommended: !1,
|
|
62
|
+
frozen: !0,
|
|
63
|
+
url: "https://eslint.org/docs/latest/rules/no-magic-numbers"
|
|
64
|
+
},
|
|
65
|
+
schema: [{
|
|
66
|
+
type: "object",
|
|
67
|
+
properties: {
|
|
68
|
+
detectObjects: {
|
|
69
|
+
type: "boolean",
|
|
70
|
+
default: !1
|
|
71
|
+
},
|
|
72
|
+
enforceConst: {
|
|
73
|
+
type: "boolean",
|
|
74
|
+
default: !1
|
|
75
|
+
},
|
|
76
|
+
ignore: {
|
|
77
|
+
type: "array",
|
|
78
|
+
items: { anyOf: [{ type: "number" }, {
|
|
79
|
+
type: "string",
|
|
80
|
+
pattern: "^[+-]?(?:0|[1-9][0-9]*)n$"
|
|
81
|
+
}] },
|
|
82
|
+
uniqueItems: !0
|
|
83
|
+
},
|
|
84
|
+
ignoreArrayIndexes: {
|
|
85
|
+
type: "boolean",
|
|
86
|
+
default: !1
|
|
87
|
+
},
|
|
88
|
+
ignoreDefaultValues: {
|
|
89
|
+
type: "boolean",
|
|
90
|
+
default: !1
|
|
91
|
+
},
|
|
92
|
+
ignoreClassFieldInitialValues: {
|
|
93
|
+
type: "boolean",
|
|
94
|
+
default: !1
|
|
95
|
+
},
|
|
96
|
+
ignoreEnums: {
|
|
97
|
+
type: "boolean",
|
|
98
|
+
default: !1
|
|
99
|
+
},
|
|
100
|
+
ignoreNumericLiteralTypes: {
|
|
101
|
+
type: "boolean",
|
|
102
|
+
default: !1
|
|
103
|
+
},
|
|
104
|
+
ignoreReadonlyClassProperties: {
|
|
105
|
+
type: "boolean",
|
|
106
|
+
default: !1
|
|
107
|
+
},
|
|
108
|
+
ignoreTypeIndexes: {
|
|
109
|
+
type: "boolean",
|
|
110
|
+
default: !1
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
additionalProperties: !1
|
|
114
|
+
}],
|
|
115
|
+
messages: {
|
|
116
|
+
useConst: "Number constants declarations must use 'const'.",
|
|
117
|
+
noMagic: "No magic number: {{raw}}."
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
create(context) {
|
|
121
|
+
let config = context.options[0] || {}, detectObjects = !!config.detectObjects, enforceConst = !!config.enforceConst, ignore = new Set((config.ignore || []).map(normalizeIgnoreValue)), ignoreArrayIndexes = !!config.ignoreArrayIndexes, ignoreDefaultValues = !!config.ignoreDefaultValues, ignoreClassFieldInitialValues = !!config.ignoreClassFieldInitialValues, ignoreEnums = !!config.ignoreEnums, ignoreNumericLiteralTypes = !!config.ignoreNumericLiteralTypes, ignoreReadonlyClassProperties = !!config.ignoreReadonlyClassProperties, ignoreTypeIndexes = !!config.ignoreTypeIndexes, okTypes = detectObjects ? [] : [
|
|
122
|
+
"ObjectExpression",
|
|
123
|
+
"Property",
|
|
124
|
+
"AssignmentExpression"
|
|
125
|
+
];
|
|
126
|
+
/**
|
|
127
|
+
* Returns whether the rule is configured to ignore the given value
|
|
128
|
+
* @param {bigint|number} value The value to check
|
|
129
|
+
* @returns {boolean} true if the value is ignored
|
|
130
|
+
*/
|
|
131
|
+
function isIgnoredValue(value) {
|
|
132
|
+
return ignore.has(value);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Returns whether the number is a default value assignment.
|
|
136
|
+
* @param {ASTNode} fullNumberNode `Literal` or `UnaryExpression` full number node
|
|
137
|
+
* @returns {boolean} true if the number is a default value
|
|
138
|
+
*/
|
|
139
|
+
function isDefaultValue(fullNumberNode) {
|
|
140
|
+
let parent = fullNumberNode.parent;
|
|
141
|
+
return parent.type === "AssignmentPattern" && parent.right === fullNumberNode;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Returns whether the number is the initial value of a class field.
|
|
145
|
+
* @param {ASTNode} fullNumberNode `Literal` or `UnaryExpression` full number node
|
|
146
|
+
* @returns {boolean} true if the number is the initial value of a class field.
|
|
147
|
+
*/
|
|
148
|
+
function isClassFieldInitialValue(fullNumberNode) {
|
|
149
|
+
let parent = fullNumberNode.parent;
|
|
150
|
+
return parent.type === "PropertyDefinition" && parent.value === fullNumberNode;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Returns whether the given node is used as a radix within parseInt() or Number.parseInt()
|
|
154
|
+
* @param {ASTNode} fullNumberNode `Literal` or `UnaryExpression` full number node
|
|
155
|
+
* @returns {boolean} true if the node is radix
|
|
156
|
+
*/
|
|
157
|
+
function isParseIntRadix(fullNumberNode) {
|
|
158
|
+
let parent = fullNumberNode.parent;
|
|
159
|
+
return parent.type === "CallExpression" && fullNumberNode === parent.arguments[1] && (astUtils.isSpecificId(parent.callee, "parseInt") || astUtils.isSpecificMemberAccess(parent.callee, "Number", "parseInt"));
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Returns whether the given node is a direct child of a JSX node.
|
|
163
|
+
* In particular, it aims to detect numbers used as prop values in JSX tags.
|
|
164
|
+
* Example: <input maxLength={10} />
|
|
165
|
+
* @param {ASTNode} fullNumberNode `Literal` or `UnaryExpression` full number node
|
|
166
|
+
* @returns {boolean} true if the node is a JSX number
|
|
167
|
+
*/
|
|
168
|
+
function isJSXNumber(fullNumberNode) {
|
|
169
|
+
return fullNumberNode.parent.type.indexOf("JSX") === 0;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Returns whether the given node is used as an array index.
|
|
173
|
+
* Value must coerce to a valid array index name: "0", "1", "2" ... "4294967294".
|
|
174
|
+
*
|
|
175
|
+
* All other values, like "-1", "2.5", or "4294967295", are just "normal" object properties,
|
|
176
|
+
* which can be created and accessed on an array in addition to the array index properties,
|
|
177
|
+
* but they don't affect array's length and are not considered by methods such as .map(), .forEach() etc.
|
|
178
|
+
*
|
|
179
|
+
* The maximum array length by the specification is 2 ** 32 - 1 = 4294967295,
|
|
180
|
+
* thus the maximum valid index is 2 ** 32 - 2 = 4294967294.
|
|
181
|
+
*
|
|
182
|
+
* All notations are allowed, as long as the value coerces to one of "0", "1", "2" ... "4294967294".
|
|
183
|
+
*
|
|
184
|
+
* Valid examples:
|
|
185
|
+
* a[0], a[1], a[1.2e1], a[0xAB], a[0n], a[1n]
|
|
186
|
+
* a[-0] (same as a[0] because -0 coerces to "0")
|
|
187
|
+
* a[-0n] (-0n evaluates to 0n)
|
|
188
|
+
*
|
|
189
|
+
* Invalid examples:
|
|
190
|
+
* a[-1], a[-0xAB], a[-1n], a[2.5], a[1.23e1], a[12e-1]
|
|
191
|
+
* a[4294967295] (above the max index, it's an access to a regular property a["4294967295"])
|
|
192
|
+
* a[999999999999999999999] (even if it wasn't above the max index, it would be a["1e+21"])
|
|
193
|
+
* a[1e310] (same as a["Infinity"])
|
|
194
|
+
* @param {ASTNode} fullNumberNode `Literal` or `UnaryExpression` full number node
|
|
195
|
+
* @param {bigint|number} value Value expressed by the fullNumberNode
|
|
196
|
+
* @returns {boolean} true if the node is a valid array index
|
|
197
|
+
*/
|
|
198
|
+
function isArrayIndex(fullNumberNode, value) {
|
|
199
|
+
let parent = fullNumberNode.parent;
|
|
200
|
+
return parent.type === "MemberExpression" && parent.property === fullNumberNode && (Number.isInteger(value) || typeof value == "bigint") && value >= 0 && value < MAX_ARRAY_LENGTH;
|
|
201
|
+
}
|
|
202
|
+
return { Literal(node) {
|
|
203
|
+
if (!astUtils.isNumericLiteral(node)) return;
|
|
204
|
+
let fullNumberNode, value, raw;
|
|
205
|
+
node.parent.type === "UnaryExpression" && ["-", "+"].includes(node.parent.operator) ? (fullNumberNode = node.parent, value = node.parent.operator === "-" ? -node.value : node.value, raw = `${node.parent.operator}${node.raw}`) : (fullNumberNode = node, value = node.value, raw = node.raw);
|
|
206
|
+
let parent = fullNumberNode.parent;
|
|
207
|
+
isIgnoredValue(value) || ignoreDefaultValues && isDefaultValue(fullNumberNode) || ignoreClassFieldInitialValues && isClassFieldInitialValue(fullNumberNode) || ignoreEnums && isParentTSEnumDeclaration(fullNumberNode) || ignoreNumericLiteralTypes && isTSNumericLiteralType(fullNumberNode) || ignoreTypeIndexes && isAncestorTSIndexedAccessType(fullNumberNode) || ignoreReadonlyClassProperties && isParentTSReadonlyPropertyDefinition(fullNumberNode) || isParseIntRadix(fullNumberNode) || isJSXNumber(fullNumberNode) || ignoreArrayIndexes && isArrayIndex(fullNumberNode, value) || (parent.type === "VariableDeclarator" ? enforceConst && parent.parent.kind !== "const" && context.report({
|
|
208
|
+
node: fullNumberNode,
|
|
209
|
+
messageId: "useConst"
|
|
210
|
+
}) : (!okTypes.includes(parent.type) || parent.type === "AssignmentExpression" && parent.left.type === "Identifier") && context.report({
|
|
211
|
+
node: fullNumberNode,
|
|
212
|
+
messageId: "noMagic",
|
|
213
|
+
data: { raw }
|
|
214
|
+
}));
|
|
215
|
+
} };
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
}));
|
|
219
|
+
//#endregion
|
|
220
|
+
//#region src-js/generated/plugin-eslint/rules/no-magic-numbers.cjs
|
|
221
|
+
module.exports = require_no_magic_numbers().create;
|
|
222
|
+
//#endregion
|