Skip to content
This repository was archived by the owner on Mar 25, 2021. It is now read-only.

Commit 7a2bfbd

Browse files
ajafffadidahiya
authored andcommitted
no-unsafe-any: correctly handle types and type-only namespaces (#3487)
1 parent 7a9f177 commit 7a2bfbd

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

src/rules/noUnsafeAnyRule.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { isReassignmentTarget, isTokenKind, isTypeFlagSet, isTypeNodeKind } from "tsutils";
18+
import { isReassignmentTarget, isSymbolFlagSet, isTokenKind, isTypeFlagSet, isTypeNodeKind } from "tsutils";
1919
import * as ts from "typescript";
2020
import * as Lint from "../index";
2121

@@ -365,6 +365,19 @@ function isPropertyAny(node: ts.PropertyDeclaration, checker: ts.TypeChecker) {
365365
}
366366

367367
function isNodeAny(node: ts.Node, checker: ts.TypeChecker): boolean {
368+
let symbol = checker.getSymbolAtLocation(node);
369+
if (symbol !== undefined && isSymbolFlagSet(symbol, ts.SymbolFlags.Alias)) {
370+
symbol = checker.getAliasedSymbol(symbol);
371+
}
372+
if (symbol !== undefined) {
373+
// NamespaceModule is a type-only namespace without runtime value, its type is 'any' when used as 'ns.Type' -> avoid error
374+
if (isSymbolFlagSet(symbol, ts.SymbolFlags.NamespaceModule)) {
375+
return false;
376+
}
377+
if (isSymbolFlagSet(symbol, ts.SymbolFlags.Type)) {
378+
return isAny(checker.getDeclaredTypeOfSymbol(symbol));
379+
}
380+
}
368381
return isAny(checker.getTypeAtLocation(node));
369382
}
370383

test/rules/no-unsafe-any/es6Module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ const defaultExport: any = 0;
22
export default defaultExport;
33
export const namedExport: any = 0;
44
export type T = number;
5+
6+
export namespace NS {
7+
export interface ITest {}
8+
}

test/rules/no-unsafe-any/test.ts.lint

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import importAlias = importEquals;
33
namespace N { export const x: any = 0; }
44
import importQualifiedName = N.x;
55
import * as namespaceImport from "./es6Module";
6-
import defaultExport, { namedExport } from "./es6Module";
6+
import defaultExport, { namedExport, NS } from "./es6Module";
7+
import namedExportAlias = namespaceImport.namedExport;
78

89
const num: namespaceImport.T = 0;
910

@@ -30,11 +31,14 @@ label: while(true) {
3031
}
3132

3233
importAlias.property;
33-
~~~~~~~~~~~ [0]
3434

3535
namespaceImport.namedExport;
3636

37-
importAlias, importAlias;
37+
namedExport.foo;
38+
~~~~~~~~~~~ [0]
39+
namedExportAlias.foo;
40+
~~~~~~~~~~~~~~~~ [0]
41+
namedExport, namedExportAlias;
3842

3943

4044
declare const x: any;
@@ -319,4 +323,26 @@ function hasThisParameter(this: any) {
319323
~ [0]
320324
}
321325

326+
namespace TestNS {
327+
export interface ITest {}
328+
}
329+
namespace TestNS2 {
330+
"";
331+
export interface ITest {}
332+
}
333+
{
334+
class Test implements TestNS.ITest {}
335+
type T = any;
336+
class Test2 implements T {}
337+
~ [0]
338+
let C = null as any;
339+
class Test3 extends C {}
340+
~ [0]
341+
342+
interface I {}
343+
class Test4 implements I {}
344+
class Test5 implements TestNS2.ITest {}
345+
class Test6 implements NS.ITest {}
346+
}
347+
322348
[0]: Unsafe use of expression of type 'any'.

0 commit comments

Comments
 (0)