Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): add no-unsafe-declaration-merging (#5840)
* feat(eslint-plugin): add no-unsafe-declaration-merging * Apply reviews * fix * Apply suggestions from code review Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>
- Loading branch information
1 parent
0be356b
commit 3728031
Showing
6 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
packages/eslint-plugin/docs/rules/no-unsafe-declaration-merging.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
--- | ||
description: 'Disallow unsafe declaration merging.' | ||
--- | ||
|
||
> 🛑 This file is source code, not the primary documentation location! 🛑 | ||
> | ||
> See **https://typescript-eslint.io/rules/no-unsafe-declaration-merging** for documentation. | ||
TypeScript's "declaration merging" supports merging separate declarations with the same name. | ||
|
||
Declaration merging between classes and interfaces is unsafe. | ||
The TypeScript compiler doesn't check whether properties are initialized, which can cause lead to TypeScript not detecting code that will cause runtime errors. | ||
|
||
```ts | ||
interface Foo { | ||
nums: number[]; | ||
} | ||
|
||
class Foo {} | ||
|
||
const foo = new Foo(); | ||
|
||
foo.nums.push(1); // Runtime Error: Cannot read properties of undefined. | ||
``` | ||
|
||
## Examples | ||
|
||
<!--tabs--> | ||
|
||
### ❌ Incorrect | ||
|
||
```ts | ||
interface Foo {} | ||
|
||
class Foo {} | ||
``` | ||
|
||
### ✅ Correct | ||
|
||
```ts | ||
interface Foo {} | ||
class Bar implements Foo {} | ||
|
||
namespace Baz {} | ||
namespace Baz {} | ||
enum Baz {} | ||
|
||
namespace Qux {} | ||
function Qux() {} | ||
``` | ||
|
||
## Further Reading | ||
|
||
- [Declaration Merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
packages/eslint-plugin/src/rules/no-unsafe-declaration-merging.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { TSESTree } from '@typescript-eslint/utils'; | ||
import * as ts from 'typescript'; | ||
|
||
import * as util from '../util'; | ||
|
||
export default util.createRule({ | ||
name: 'no-unsafe-declaration-merging', | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Disallow unsafe declaration merging', | ||
recommended: 'strict', | ||
requiresTypeChecking: true, | ||
}, | ||
messages: { | ||
unsafeMerging: | ||
'Unsafe declaration merging between classes and interfaces.', | ||
}, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
const parserServices = util.getParserServices(context); | ||
const checker = parserServices.program.getTypeChecker(); | ||
|
||
function checkUnsafeDeclaration( | ||
node: TSESTree.Identifier, | ||
unsafeKind: ts.SyntaxKind, | ||
): void { | ||
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node); | ||
const type = checker.getTypeAtLocation(tsNode); | ||
const symbol = type.getSymbol(); | ||
if (symbol?.declarations?.some(decl => decl.kind === unsafeKind)) { | ||
context.report({ | ||
node, | ||
messageId: 'unsafeMerging', | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
ClassDeclaration(node): void { | ||
if (node.id) { | ||
checkUnsafeDeclaration(node.id, ts.SyntaxKind.InterfaceDeclaration); | ||
} | ||
}, | ||
TSInterfaceDeclaration(node): void { | ||
checkUnsafeDeclaration(node.id, ts.SyntaxKind.ClassDeclaration); | ||
}, | ||
}; | ||
}, | ||
}); |
124 changes: 124 additions & 0 deletions
124
packages/eslint-plugin/tests/rules/no-unsafe-declaration-merging.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import rule from '../../src/rules/no-unsafe-declaration-merging'; | ||
import { getFixturesRootDir, RuleTester } from '../RuleTester'; | ||
|
||
const rootPath = getFixturesRootDir(); | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
parserOptions: { | ||
sourceType: 'module', | ||
tsconfigRootDir: rootPath, | ||
project: './tsconfig.json', | ||
}, | ||
}); | ||
|
||
ruleTester.run('no-unsafe-declaration-merging', rule, { | ||
valid: [ | ||
` | ||
interface Foo {} | ||
class Bar implements Foo {} | ||
`, | ||
` | ||
namespace Foo {} | ||
namespace Foo {} | ||
`, | ||
` | ||
enum Foo {} | ||
namespace Foo {} | ||
`, | ||
` | ||
namespace Fooo {} | ||
function Foo() {} | ||
`, | ||
` | ||
const Foo = class {}; | ||
`, | ||
` | ||
interface Foo { | ||
props: string; | ||
} | ||
function bar() { | ||
return class Foo {}; | ||
} | ||
`, | ||
` | ||
interface Foo { | ||
props: string; | ||
} | ||
(function bar() { | ||
class Foo {} | ||
})(); | ||
`, | ||
` | ||
declare global { | ||
interface Foo {} | ||
} | ||
class Foo {} | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
interface Foo {} | ||
class Foo {} | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'unsafeMerging', | ||
line: 2, | ||
column: 11, | ||
}, | ||
{ | ||
messageId: 'unsafeMerging', | ||
line: 3, | ||
column: 7, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
namespace Foo { | ||
export interface Bar {} | ||
} | ||
namespace Foo { | ||
export class Bar {} | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'unsafeMerging', | ||
line: 3, | ||
column: 20, | ||
}, | ||
{ | ||
messageId: 'unsafeMerging', | ||
line: 6, | ||
column: 16, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
declare global { | ||
interface Foo {} | ||
class Foo {} | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'unsafeMerging', | ||
line: 3, | ||
column: 13, | ||
}, | ||
{ | ||
messageId: 'unsafeMerging', | ||
line: 4, | ||
column: 9, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |