Skip to content

Commit

Permalink
fix: Enable strict and fix problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Mar 19, 2021
1 parent 20eb236 commit 826953c
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 63 deletions.
7 changes: 2 additions & 5 deletions source/rules/no-assign-mutated-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ import {
isExpressionStatement,
isIdentifier,
isMemberExpression,
isNewExpression,
} from "eslint-etc";
import { ruleCreator } from "../utils";

function isNewExpression(node: es.Node): node is es.NewExpression {
return node.type === "NewExpression";
}

const mutatorRegExp = /^(fill|reverse|sort)$/;
const creatorRegExp = /^(concat|entries|filter|keys|map|slice|splice|values)$/;

Expand Down Expand Up @@ -46,7 +43,7 @@ const rule = ruleCreator({
) => {
const callExpression = getParent(memberExpression) as es.CallExpression;
const parent = getParent(callExpression);
if (!isExpressionStatement(parent)) {
if (parent && !isExpressionStatement(parent)) {
if (
couldBeType(memberExpression.object, "Array") &&
mutatesReferencedArray(callExpression)
Expand Down
2 changes: 1 addition & 1 deletion source/rules/no-const-enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as tsutils from "tsutils";
import * as ts from "typescript";
import { ruleCreator } from "../utils";

const defaultOptions: {
const defaultOptions: readonly {
allowLocal?: boolean;
}[] = [];

Expand Down
12 changes: 5 additions & 7 deletions source/rules/no-deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ruleCreator } from "../utils";

const deprecatedNamesByProgram = new WeakMap<ts.Program, Set<string>>();

const defaultOptions: {
const defaultOptions: readonly {
ignored?: Record<string, string>;
}[] = [];

Expand Down Expand Up @@ -61,16 +61,14 @@ const rule = ruleCreator({
const type = typeChecker.getTypeAtLocation(identifier);
return typeChecker.getFullyQualifiedName(type.symbol);
};
let deprecatedNames: Set<string>;
if (deprecatedNamesByProgram.has(program)) {
deprecatedNames = deprecatedNamesByProgram.get(program);
} else {
let deprecatedNames = deprecatedNamesByProgram.get(program);
if (!deprecatedNames) {
deprecatedNames = findTaggedNames("deprecated", program);
deprecatedNamesByProgram.set(program, deprecatedNames);
}
return {
Identifier: (node: es.Identifier) => {
switch (getParent(node).type) {
switch (getParent(node)?.type) {
case "ExportSpecifier":
case "ImportDefaultSpecifier":
case "ImportNamespaceSpecifier":
Expand All @@ -80,7 +78,7 @@ const rule = ruleCreator({
break;
}
const identifier = esTreeNodeToTSNodeMap.get(node) as ts.Identifier;
if (!deprecatedNames.has(identifier.text)) {
if (!deprecatedNames?.has(identifier.text)) {
return;
}
if (isDeclaration(identifier)) {
Expand Down
2 changes: 1 addition & 1 deletion source/rules/no-foreach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TSESTree as es } from "@typescript-eslint/experimental-utils";
import { getTypeServices, isMemberExpression } from "eslint-etc";
import { ruleCreator } from "../utils";

const defaultOptions: {
const defaultOptions: readonly {
types?: string[];
}[] = [];

Expand Down
5 changes: 3 additions & 2 deletions source/rules/no-implicit-any-catch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ function isParenthesised(
const before = sourceCode.getTokenBefore(node);
const after = sourceCode.getTokenAfter(node);
return (
Boolean(before && after) &&
before &&
after &&
before.value === "(" &&
before.range[1] <= node.range[0] &&
after.value === ")" &&
after.range[0] >= node.range[1]
);
}

const defaultOptions: {
const defaultOptions: readonly {
allowExplicitAny?: boolean;
}[] = [];

Expand Down
12 changes: 5 additions & 7 deletions source/rules/no-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ruleCreator } from "../utils";

const internalNamesByProgram = new WeakMap<ts.Program, Set<string>>();

const defaultOptions: {
const defaultOptions: readonly {
ignored?: Record<string, string>;
}[] = [];

Expand Down Expand Up @@ -63,16 +63,14 @@ const rule = ruleCreator({
const type = typeChecker.getTypeAtLocation(identifier);
return typeChecker.getFullyQualifiedName(type.symbol);
};
let internalNames: Set<string>;
if (internalNamesByProgram.has(program)) {
internalNames = internalNamesByProgram.get(program);
} else {
let internalNames = internalNamesByProgram.get(program);
if (!internalNames) {
internalNames = findTaggedNames("internal", program);
internalNamesByProgram.set(program, internalNames);
}
return {
Identifier: (node: es.Identifier) => {
switch (getParent(node).type) {
switch (getParent(node)?.type) {
case "ExportSpecifier":
case "ImportDefaultSpecifier":
case "ImportNamespaceSpecifier":
Expand All @@ -82,7 +80,7 @@ const rule = ruleCreator({
break;
}
const identifier = esTreeNodeToTSNodeMap.get(node) as ts.Identifier;
if (!internalNames.has(identifier.text)) {
if (!internalNames?.has(identifier.text)) {
return;
}
if (isDeclaration(identifier)) {
Expand Down
2 changes: 1 addition & 1 deletion source/rules/no-t.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { TSESTree as es } from "@typescript-eslint/experimental-utils";
import { ruleCreator } from "../utils";

const defaultOptions: {
const defaultOptions: readonly {
prefix?: string;
}[] = [];

Expand Down
29 changes: 12 additions & 17 deletions source/rules/prefer-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,14 @@ import {
getParent,
getParserServices,
getTypeServices,
isExportNamedDeclaration,
isIdentifier,
isTSTypeLiteral,
isTSTypeReference,
} from "eslint-etc";
import { ruleCreator } from "../utils";

function isExportNamedDeclaration(
node: es.Node
): node is es.ExportNamedDeclaration {
return node.type === "ExportNamedDeclaration";
}

function isTSTypeLiteral(node: es.Node): node is es.TSTypeLiteral {
return node.type === "TSTypeLiteral";
}

function isTSTypeReference(node: es.Node): node is es.TSTypeReference {
return node.type === "TSTypeReference";
}

const defaultOptions: {
const defaultOptions: readonly {
allowIntersection?: boolean;
allowLocal?: boolean;
}[] = [];
Expand Down Expand Up @@ -94,7 +83,10 @@ const rule = ruleCreator({
const typeAliasNode = getParent(
functionTypeNode
) as es.TSTypeAliasDeclaration;
if (allowLocal && !isExportNamedDeclaration(getParent(typeAliasNode))) {
if (
allowLocal &&
!isExportNamedDeclaration(getParent(typeAliasNode) as es.Node)
) {
return;
}
function fix(fixer: eslint.RuleFixer) {
Expand Down Expand Up @@ -216,7 +208,10 @@ const rule = ruleCreator({
const typeAliasNode = getParent(
typeLiteralNode
) as es.TSTypeAliasDeclaration;
if (allowLocal && !isExportNamedDeclaration(getParent(typeAliasNode))) {
if (
allowLocal &&
!isExportNamedDeclaration(getParent(typeAliasNode) as es.Node)
) {
return;
}
function fix(fixer: eslint.RuleFixer) {
Expand Down
3 changes: 2 additions & 1 deletion source/rules/throw-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ const rule = ruleCreator({
return;
}
variable.references.forEach(({ identifier }) => {
const parent = getParent(identifier);
const parent = getParent(identifier) as es.Node;
if (isCallExpression(parent) && identifier === parent.callee) {
checkRejection(parent);
}
});
},
ThrowStatement: (node: es.ThrowStatement) => {
if (
node.argument &&
!isAny(node.argument) &&
!couldBeType(node.argument, /^(Error|DOMException)$/)
) {
Expand Down
10 changes: 7 additions & 3 deletions source/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ export function findTaggedNames(
if (ts.isConstructorDeclaration(node)) {
const { parent } = node;
const { name } = parent;
taggedNames.add(name.text);
if (name?.text) {
taggedNames.add(name.text);
}
} else {
const { name } = node as ts.Node & { name: ts.Identifier };
taggedNames.add(name.text);
const { name } = node as ts.Node & { name?: ts.Identifier };
if (name?.text) {
taggedNames.add(name.text);
}
}
});
});
Expand Down
1 change: 0 additions & 1 deletion source/tslint-tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ function getDeprecationsFromDeclaration(
if (comment.tags === undefined) {
continue;
}
console.log(comment.tags);
for (const tag of comment.tags) {
if (tag.tagName.text === tagName) {
result.push(tag.comment === undefined ? "" : tag.comment);
Expand Down
10 changes: 6 additions & 4 deletions tests/modules/deprecated.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/no-inferrable-types */

// Not deprecated

export interface NotDeprecatedInterface {
Expand All @@ -12,7 +14,7 @@ export type NotDeprecatedType = {

export class NotDeprecatedClass {
static notDeprecatedStaticMethod(): void {}
notDeprecatedProperty: string;
notDeprecatedProperty: string = "";
get notDeprecatedGetter(): string {
return "";
}
Expand Down Expand Up @@ -45,7 +47,7 @@ export type DeprecatedType = {
/** @deprecated Don't use this */
export class DeprecatedClass {
static notDeprecatedStaticMethod(): void {}
notDeprecatedProperty: string;
notDeprecatedProperty: string = "";
get notDeprecatedGetter(): string {
return "";
}
Expand Down Expand Up @@ -89,8 +91,8 @@ export class SomeDeprecatedClass {
static deprecatedStaticMethod(): void {}
static notDeprecatedStaticMethod(): void {}
/** @deprecated Don't use this */
deprecatedProperty: string;
notDeprecatedProperty: string;
deprecatedProperty: string = "";
notDeprecatedProperty: string = "";
/** @deprecated Don't use this */
get deprecatedGetter(): string {
return "";
Expand Down
10 changes: 6 additions & 4 deletions tests/modules/internal.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/no-inferrable-types */

// Not internal

export interface NotInternalInterface {
Expand All @@ -12,7 +14,7 @@ export type NotInternalType = {

export class NotInternalClass {
static notInternalStaticMethod(): void {}
notInternalProperty: string;
notInternalProperty: string = "";
get notInternalGetter(): string {
return "";
}
Expand Down Expand Up @@ -45,7 +47,7 @@ export type InternalType = {
/** @internal */
export class InternalClass {
static notInternalStaticMethod(): void {}
notInternalProperty: string;
notInternalProperty: string = "";
get notInternalGetter(): string {
return "";
}
Expand Down Expand Up @@ -89,8 +91,8 @@ export class SomeInternalClass {
static internalStaticMethod(): void {}
static notInternalStaticMethod(): void {}
/** @internal */
internalProperty: string;
notInternalProperty: string;
internalProperty: string = "";
notInternalProperty: string = "";
/** @internal */
get internalGetter(): string {
return "";
Expand Down
8 changes: 2 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@
"module": "commonjs",
"moduleResolution": "node",
"noEmitHelpers": true,
"noImplicitAny": true,
"outDir": "build",
"removeComments": true,
"skipLibCheck": true,
"sourceMap": false,
"suppressImplicitAnyIndexErrors": true,
"strict": true,
"target": "es2018"
},
"exclude": [],
"include": [
"source/**/*.ts",
"tests/**/*.ts"
]
"include": ["source/**/*.ts", "tests/**/*.ts"]
}
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -693,9 +693,9 @@ escape-string-regexp@^1.0.5:
integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=

eslint-etc@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/eslint-etc/-/eslint-etc-4.0.1.tgz#2ccb5901326757ae9f7c13c671457390b9712f00"
integrity sha512-07fX1e/LDbBEBjRIeiLo8hk2Kgii8IZNh0aKdI7lr8TaP3R3n9qXvzF/6lVso3pulKjD2zzfLN7QSVnYKp0yhg==
version "4.0.3"
resolved "https://registry.yarnpkg.com/eslint-etc/-/eslint-etc-4.0.3.tgz#584b7fb877dd018b3b8728a60449a99ad6309956"
integrity sha512-Zbqqb3f5t8aSfTC8Sye2wU4SlpkV/b5BMksVTYTwF2ZsQUuxBbOIB4mLZhOHz7vCVVFcfbQxBD6V3RP0PbgqZw==
dependencies:
"@typescript-eslint/experimental-utils" "^4.0.0"
tsutils "^3.17.1"
Expand Down

0 comments on commit 826953c

Please sign in to comment.