Skip to content

Commit

Permalink
feat!: @typescript-eslint/no-magic-numbers
Browse files Browse the repository at this point in the history
Co-authored-by: Rostislav Simonik <rostislav.simonik@technologystudio.sk>
  • Loading branch information
mightyiam and rostislav-simonik committed Sep 14, 2024
1 parent c38d02e commit c187e98
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 15 deletions.
5 changes: 4 additions & 1 deletion src/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ export interface Boo {
* https://github.com/mightyiam/eslint-config-love/issues/110
*/
// Inline callbacks don't need return types:
const ONE_MILLISECOND = 1
setTimeout(() => {
console.log()
}, 1)
}, ONE_MILLISECOND)

// The return type is clear from the left side of the assignment:
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
const double: (n: number) => number = (n) => n * 2
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
;[1, 2].map(double)
17 changes: 17 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const rules = {
'ts-ignore': true,
'ts-nocheck': true,
'ts-check': false,
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
minimumDescriptionLength: 3,
},
],
Expand Down Expand Up @@ -76,6 +77,7 @@ const rules = {
},
],
'@typescript-eslint/init-declarations': ['error', 'always'],
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
'@typescript-eslint/max-params': ['error', { max: 4 }],
'@typescript-eslint/method-signature-style': ['error'],
'@typescript-eslint/naming-convention': [
Expand Down Expand Up @@ -132,6 +134,21 @@ const rules = {
],
'@typescript-eslint/no-invalid-void-type': ['error'],
'@typescript-eslint/no-loop-func': ['error'],
'@typescript-eslint/no-magic-numbers': [
'error',
{
ignore: [],
ignoreArrayIndexes: false,
ignoreDefaultValues: false,
ignoreClassFieldInitialValues: false,
enforceConst: true,
detectObjects: true,
ignoreEnums: true,
ignoreNumericLiteralTypes: false,
ignoreReadonlyClassProperties: true,
ignoreTypeIndexes: false,
},
],
'@typescript-eslint/no-misused-new': ['error'],
'@typescript-eslint/no-misused-promises': ['error'],
'@typescript-eslint/no-namespace': ['error'],
Expand Down
18 changes: 18 additions & 0 deletions src/test/_expected-exported-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const expectedExportedValue: TSESLint.FlatConfig.Config = {
promise: promisePlugin,
},
rules: {
'no-magic-numbers': ['off'],
'no-var': ['error'],
'object-shorthand': ['warn', 'properties'],

Expand Down Expand Up @@ -175,6 +176,7 @@ export const expectedExportedValue: TSESLint.FlatConfig.Config = {
'ts-ignore': true,
'ts-nocheck': true,
'ts-check': false,
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
minimumDescriptionLength: 3,
},
],
Expand Down Expand Up @@ -236,6 +238,7 @@ export const expectedExportedValue: TSESLint.FlatConfig.Config = {
},
],
'@typescript-eslint/init-declarations': ['error', 'always'],
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
'@typescript-eslint/max-params': ['error', { max: 4 }],
'@typescript-eslint/method-signature-style': ['error'],
'@typescript-eslint/naming-convention': [
Expand Down Expand Up @@ -292,6 +295,21 @@ export const expectedExportedValue: TSESLint.FlatConfig.Config = {
],
'@typescript-eslint/no-invalid-void-type': ['error'],
'@typescript-eslint/no-loop-func': ['error'],
'@typescript-eslint/no-magic-numbers': [
'error',
{
ignore: [],
ignoreArrayIndexes: false,
ignoreDefaultValues: false,
ignoreClassFieldInitialValues: false,
enforceConst: true,
detectObjects: true,
ignoreEnums: true,
ignoreNumericLiteralTypes: false,
ignoreReadonlyClassProperties: true,
ignoreTypeIndexes: false,
},
],
'@typescript-eslint/no-misused-new': ['error'],
'@typescript-eslint/no-misused-promises': ['error'],
'@typescript-eslint/no-namespace': ['error'],
Expand Down
2 changes: 0 additions & 2 deletions src/test/_rules_to_consider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export const rulesToConsider = [
'@typescript-eslint/no-magic-numbers',
'@typescript-eslint/no-meaningless-void-operator',
'@typescript-eslint/no-mixed-enums',
'@typescript-eslint/no-non-null-asserted-nullish-coalescing',
Expand Down Expand Up @@ -151,7 +150,6 @@ export const rulesToConsider = [
'no-invalid-this',
'no-label-var',
'no-lonely-if',
'no-magic-numbers',
'no-multi-assign',
'no-negated-condition',
'no-nested-ternary',
Expand Down
23 changes: 12 additions & 11 deletions src/test/_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,25 @@ export const getPkgDetails = async (): Promise<PkgDetails> => {

export const isSingleCaretRange = (rangeStr: string): boolean => {
const range = new semver.Range(rangeStr)
return (
range.set.length === 1 &&
range.set[0].length === 2 &&
range.set[0][0].operator === '>=' &&
range.set[0][1].operator === '<'
)
/* eslint-disable @typescript-eslint/no-magic-numbers */
if (range.set.length !== 1) return false
const comparators = range.set[0]
if (comparators.length !== 2) return false
return comparators[0].operator === '>=' && comparators[1].operator === '<'
/* eslint-enable @typescript-eslint/no-magic-numbers */
}

export const isPinnedRange = (rangeStr: string): boolean => {
const range = new semver.Range(rangeStr)
return (
range.set.length === 1 &&
range.set[0].length === 1 &&
range.set[0][0].operator === ''
)
/* eslint-disable @typescript-eslint/no-magic-numbers */
if (range.set.length !== 1) return false
const comparators = range.set[0]
return comparators.length === 1 && comparators[0].operator === ''
/* eslint-enable @typescript-eslint/no-magic-numbers */
}

export const extractVersionRange = (spec: string): string =>
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
spec.split('@').slice(-1)[0]

const ourRules_ = exported.rules
Expand Down
1 change: 1 addition & 0 deletions src/test/compatibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ test('our configuration is compatible with the plugin and parser at bottom of de

const results = await eslint.lintText('', { filePath: 'src/index.ts' })

// eslint-disable-next-line @typescript-eslint/no-magic-numbers
t.true(results.length > 0)
results.forEach((result) => t.deepEqual(result.messages, [], result.filePath))
})
7 changes: 6 additions & 1 deletion src/test/resolved-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ test('rules', async (t) => {
if (!Array.isArray(value)) throw new Error()
const [level, ...options] = value
if (typeof level === 'number') throw new Error()
return [name, [{ error: 2, warn: 1, off: 0 }[level], ...options]]
enum Level {
error = 2,
warn = 1,
off = 0,
}
return [name, [Level[level], ...options]]
}),
)
t.deepEqual(actual.rules, normalized)
Expand Down
2 changes: 2 additions & 0 deletions src/test/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ test('no intersection between lists', (t) => {
}, {})

const intersection = Object.fromEntries(
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
Object.entries(counts).filter(([_rule, inLists]) => inLists.length > 1),
)

Expand Down Expand Up @@ -115,6 +116,7 @@ test('JS equivalent rules are off', async (t) => {

if (baseRuleConfig === undefined) return false
if (!Array.isArray(baseRuleConfig)) throw new Error()
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
const severity = baseRuleConfig[0]
if (typeof severity !== 'string') throw new Error()
return severity !== 'off'
Expand Down

0 comments on commit c187e98

Please sign in to comment.