Skip to content
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): add support for valid number and bigint intersections in restrict-plus-operands rule #4795

Merged
merged 4 commits into from Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/eslint-plugin/src/rules/restrict-plus-operands.ts
Expand Up @@ -88,7 +88,12 @@ export default util.createRule<Options, MessageIds>({

if (type.isIntersection()) {
const types = type.types.map(getBaseTypeOfLiteralType);
return types.some(value => value === 'string') ? 'string' : 'invalid';

if (types.some(value => value === 'string')) return 'string';
leonsilicon marked this conversation as resolved.
Show resolved Hide resolved
if (types.some(value => value === 'number')) return 'number';
if (types.some(value => value === 'bigint')) return 'bigint';

return 'invalid';
}

const stringType = typeChecker.typeToString(type);
Expand Down
222 changes: 222 additions & 0 deletions packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts
Expand Up @@ -109,6 +109,46 @@ const x = a + b;
`
declare const a: 'string literal' & string;
declare const b: string;
const x = a + b;
`,
`
declare const a: {} & number;
declare const b: number;
const x = a + b;
`,
`
declare const a: unknown & number;
declare const b: number;
const x = a + b;
`,
`
declare const a: number & number;
declare const b: number;
const x = a + b;
`,
`
declare const a: 42 & number;
declare const b: number;
const x = a + b;
`,
`
declare const a: {} & bigint;
declare const b: bigint;
const x = a + b;
`,
`
declare const a: unknown & bigint;
declare const b: bigint;
const x = a + b;
`,
`
declare const a: bigint & bigint;
declare const b: bigint;
const x = a + b;
`,
`
declare const a: 42n & bigint;
declare const b: bigint;
const x = a + b;
`,
`
Expand Down Expand Up @@ -575,6 +615,188 @@ function foo<T extends 1>(a: T) {
},
],
},
{
code: `
declare const a: boolean & number;
declare const b: number;
const x = a + b;
`,
errors: [
{
messageId: 'notNumbers',
line: 4,
column: 19,
},
],
},
{
code: `
declare const a: symbol & number;
declare const b: number;
const x = a + b;
`,
errors: [
{
messageId: 'notNumbers',
line: 4,
column: 19,
},
],
},
{
code: `
declare const a: object & number;
declare const b: number;
const x = a + b;
`,
errors: [
{
messageId: 'notNumbers',
line: 4,
column: 19,
},
],
},
{
code: `
declare const a: never & number;
declare const b: number;
const x = a + b;
`,
errors: [
{
messageId: 'notNumbers',
line: 4,
column: 19,
},
],
},
{
code: `
declare const a: any & number;
declare const b: number;
const x = a + b;
`,
errors: [
{
messageId: 'notValidAnys',
line: 4,
column: 19,
},
],
},
{
code: `
declare const a: { a: 1 } & { b: 2 };
declare const b: number;
const x = a + b;
`,
errors: [
{
messageId: 'notNumbers',
line: 4,
column: 19,
},
],
},
{
code: `
declare const a: boolean & bigint;
declare const b: bigint;
const x = a + b;
`,
errors: [
{
messageId: 'notBigInts',
line: 4,
column: 19,
},
],
},
{
code: `
declare const a: number & bigint;
declare const b: bigint;
const x = a + b;
`,
errors: [
{
messageId: 'notBigInts',
line: 4,
column: 19,
},
],
},
{
code: `
declare const a: symbol & bigint;
declare const b: bigint;
const x = a + b;
`,
errors: [
{
messageId: 'notBigInts',
line: 4,
column: 19,
},
],
},
{
code: `
declare const a: object & bigint;
declare const b: bigint;
const x = a + b;
`,
errors: [
{
messageId: 'notBigInts',
line: 4,
column: 19,
},
],
},
{
code: `
declare const a: never & bigint;
declare const b: bigint;
const x = a + b;
`,
errors: [
{
messageId: 'notBigInts',
line: 4,
column: 19,
},
],
},
{
code: `
declare const a: any & bigint;
declare const b: bigint;
const x = a + b;
`,
errors: [
{
messageId: 'notValidAnys',
line: 4,
column: 19,
},
],
},
{
code: `
declare const a: { a: 1 } & { b: 2 };
declare const b: bigint;
const x = a + b;
`,
errors: [
{
messageId: 'notBigInts',
line: 4,
column: 19,
},
],
},
{
code: `
let foo: string | undefined;
Expand Down