Skip to content

Commit

Permalink
feat(eslint-plugin): add support for valid number and bigint intersec…
Browse files Browse the repository at this point in the history
…tions in restrict-plus-operands rule (#4795)

* feat: support number and bigint intersections in restrict-plus-operands rule

* fix: tests

* style: formatting
  • Loading branch information
Leon Si committed Apr 8, 2022
1 parent dbcd9e0 commit 19c600a
Show file tree
Hide file tree
Showing 2 changed files with 236 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/eslint-plugin/src/rules/restrict-plus-operands.ts
Expand Up @@ -88,7 +88,20 @@ 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';
}

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

0 comments on commit 19c600a

Please sign in to comment.