Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(eslint-plugin): [no-unsafe-declaration-merging] switch to use scope analysis instead of type information#5865

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
bradzacher merged 1 commit intomainfrom5854-no-unsafe-decl-merge-scope-analysis
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
import type { Scope } from '@typescript-eslint/scope-manager';
import type { TSESTree } from '@typescript-eslint/utils';
import* as ts from 'typescript';
import{ AST_NODE_TYPES } from '@typescript-eslint/utils';

import * as util from '../util';

Expand All@@ -10,7 +11,7 @@ export default util.createRule({
docs: {
description: 'Disallow unsafe declaration merging',
recommended: 'strict',
requiresTypeChecking:true,
requiresTypeChecking:false,
},
messages: {
unsafeMerging:
Expand All@@ -20,17 +21,22 @@ export default util.createRule({
},
defaultOptions: [],
create(context) {
const parserServices = util.getParserServices(context);
const checker = parserServices.program.getTypeChecker();

function checkUnsafeDeclaration(
scope: Scope,
node: TSESTree.Identifier,
unsafeKind:ts.SyntaxKind,
unsafeKind:AST_NODE_TYPES,
): void {
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
const type = checker.getTypeAtLocation(tsNode);
const symbol = type.getSymbol();
if (symbol?.declarations?.some(decl => decl.kind === unsafeKind)) {
const variable = scope.set.get(node.name);
if (!variable) {
return;
}

const defs = variable.defs;
if (defs.length <= 1) {
return;
}

if (defs.some(def => def.node.type === unsafeKind)) {
context.report({
node,
messageId: 'unsafeMerging',
Expand All@@ -41,11 +47,26 @@ export default util.createRule({
return {
ClassDeclaration(node): void {
if (node.id) {
checkUnsafeDeclaration(node.id, ts.SyntaxKind.InterfaceDeclaration);
// by default eslint returns the inner class scope for the ClassDeclaration node
// but we want the outer scope within which merged variables will sit
const currentScope = context.getScope().upper;
if (currentScope == null) {
return;
}

checkUnsafeDeclaration(
currentScope,
node.id,
AST_NODE_TYPES.TSInterfaceDeclaration,
);
}
},
TSInterfaceDeclaration(node): void {
checkUnsafeDeclaration(node.id, ts.SyntaxKind.ClassDeclaration);
checkUnsafeDeclaration(
context.getScope(),
node.id,
AST_NODE_TYPES.ClassDeclaration,
);
},
};
},
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -80,23 +80,19 @@ class Foo {}
},
{
code: `
namespace Foo {
export interface Bar {}
}
namespace Foo {
export class Bar {}
}
Comment on lines -83 to -88
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

note - we can no longer detect cases like this (as I mentioned here#5854 (comment)).

I think this is an okay trade-off as it should be really rare that someone is declaration merging via a declaration merged namespace.

yeonjuan reacted with thumbs up emoji
Copy link
Contributor

@yeonjuanyeonjuanOct 24, 2022
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

it seems good trade off. i tried to handle these kinds of cases but i couldn't finish until now 😅

bradzacher reacted with thumbs up emoji
class Foo {}
interface Foo {}
`,
errors: [
{
messageId: 'unsafeMerging',
line:3,
column:20,
line:2,
column:7,
},
{
messageId: 'unsafeMerging',
line:6,
column:16,
line:3,
column:11,
},
],
},
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp