New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(eslint-plugin): [no-magic-numbers] ignoreTypeIndexes option #4789
Changes from 2 commits
d0cabc2
d1e65be
8ec2104
af1ddb7
88bd7e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,42 @@ class Foo { | |
`, | ||
options: [{ ignoreReadonlyClassProperties: true }], | ||
}, | ||
{ | ||
code: 'type Foo = Bar[0];', | ||
options: [{ ignoreTypeIndexes: true }], | ||
}, | ||
{ | ||
code: 'type Foo = Bar[-1];', | ||
options: [{ ignoreTypeIndexes: true }], | ||
}, | ||
{ | ||
code: 'type Foo = Bar[0xab];', | ||
options: [{ ignoreTypeIndexes: true }], | ||
}, | ||
{ | ||
code: 'type Foo = Bar[5.6e1];', | ||
options: [{ ignoreTypeIndexes: true }], | ||
}, | ||
{ | ||
code: 'type Foo = Bar[10n];', | ||
options: [{ ignoreTypeIndexes: true }], | ||
}, | ||
{ | ||
code: 'type Foo = Bar[1 | -2];', | ||
options: [{ ignoreTypeIndexes: true }], | ||
}, | ||
{ | ||
code: 'type Foo = Parameters<Bar>[2];', | ||
options: [{ ignoreTypeIndexes: true }], | ||
}, | ||
{ | ||
code: "type Foo = Bar['baz'];", | ||
options: [{ ignoreTypeIndexes: true }], | ||
}, | ||
{ | ||
code: "type Foo = Bar['baz'];", | ||
options: [{ ignoreTypeIndexes: false }], | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A few edge cases we'll also want to test for: type Foo = Bar[1 & number]; type Other = {
[0]: 3;
}
type Foo = {
[K in keyof Other]: `${K & number}`;
}; type Foo = {
[K in 0 | 1 | 2]: 0;
}; type Others = [ ['a'], ['b'] ];
type Foo = {
[K in keyof Others[0]]: Others[K];
}; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JoshuaKGoldberg couple of quick questions on those:
Sure, I check for union types so intersection should be covered too! Thanks.
Since the Also, the
As above,
I’ll add a test to ensure the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great questions!
I'm not requesting changes to logic, just that it be covered. Agreed that they're different things -- the tests should validate that
Yes. That's already covered though; I'm really more interested in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JoshuaKGoldberg Thank you for clarifying. In af1ddb7 I added support for intersection types, nesting of union/intersection types, and the additional invalid test cases you suggested. |
||
], | ||
|
||
invalid: [ | ||
|
@@ -268,5 +304,111 @@ class Foo { | |
}, | ||
], | ||
}, | ||
{ | ||
code: 'type Foo = Bar[0];', | ||
options: [{ ignoreTypeIndexes: false }], | ||
errors: [ | ||
{ | ||
messageId: 'noMagic', | ||
data: { | ||
raw: '0', | ||
}, | ||
line: 1, | ||
column: 16, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'type Foo = Bar[-1];', | ||
options: [{ ignoreTypeIndexes: false }], | ||
errors: [ | ||
{ | ||
messageId: 'noMagic', | ||
data: { | ||
raw: '-1', | ||
}, | ||
line: 1, | ||
column: 16, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'type Foo = Bar[0xab];', | ||
options: [{ ignoreTypeIndexes: false }], | ||
errors: [ | ||
{ | ||
messageId: 'noMagic', | ||
data: { | ||
raw: '0xab', | ||
}, | ||
line: 1, | ||
column: 16, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'type Foo = Bar[5.6e1];', | ||
options: [{ ignoreTypeIndexes: false }], | ||
errors: [ | ||
{ | ||
messageId: 'noMagic', | ||
data: { | ||
raw: '5.6e1', | ||
}, | ||
line: 1, | ||
column: 16, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'type Foo = Bar[10n];', | ||
options: [{ ignoreTypeIndexes: false }], | ||
errors: [ | ||
{ | ||
messageId: 'noMagic', | ||
data: { | ||
raw: '10n', | ||
}, | ||
line: 1, | ||
column: 16, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'type Foo = Bar[1 | -2];', | ||
options: [{ ignoreTypeIndexes: false }], | ||
errors: [ | ||
{ | ||
messageId: 'noMagic', | ||
data: { | ||
raw: '1', | ||
}, | ||
line: 1, | ||
column: 16, | ||
}, | ||
{ | ||
messageId: 'noMagic', | ||
data: { | ||
raw: '-2', | ||
}, | ||
line: 1, | ||
column: 20, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'type Foo = Parameters<Bar>[2];', | ||
options: [{ ignoreTypeIndexes: false }], | ||
errors: [ | ||
{ | ||
messageId: 'noMagic', | ||
data: { | ||
raw: '2', | ||
}, | ||
line: 1, | ||
column: 28, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh, it's a pity this docs page doesn't use the newer tabbed format others do. No need to change in this PR though; I'm going to try to make docs a bit more automated soon.