Skip to content

Commit

Permalink
Merge branch 'main' into fix/4476
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaKGoldberg committed Oct 24, 2022
2 parents 9d10030 + e70a10a commit 2d24a1b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 26 deletions.
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/no-base-to-string.ts
Expand Up @@ -48,7 +48,7 @@ export default util.createRule<Options, MessageIds>({
},
defaultOptions: [
{
ignoredTypeNames: ['RegExp'],
ignoredTypeNames: ['Error', 'RegExp', 'URL', 'URLSearchParams'],
},
],
create(context, [option]) {
Expand Down
45 changes: 33 additions & 12 deletions packages/eslint-plugin/src/rules/no-unsafe-declaration-merging.ts
@@ -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
3 changes: 0 additions & 3 deletions packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts
Expand Up @@ -48,9 +48,6 @@ export default util.createRule<Options, MessageIds>({
ignoreMixedLogicalExpressions: {
type: 'boolean',
},
forceSuggestionFixer: {
type: 'boolean',
},
},
additionalProperties: false,
},
Expand Down
3 changes: 3 additions & 0 deletions packages/eslint-plugin/tests/rules/no-base-to-string.test.ts
Expand Up @@ -113,6 +113,9 @@ tag\`\${{}}\`;
return \`\${v}\`;
}
`,
"'' += new Error();",
"'' += new URL();",
"'' += new URLSearchParams();",
],
invalid: [
{
Expand Down
Expand Up @@ -80,23 +80,19 @@ class Foo {}
},
{
code: `
namespace Foo {
export interface Bar {}
}
namespace Foo {
export class Bar {}
}
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

0 comments on commit 2d24a1b

Please sign in to comment.