Skip to content

Commit 28195d6

Browse files
Replace TSLint with ESLint (#119)
1 parent 0812f39 commit 28195d6

File tree

18 files changed

+89
-58
lines changed

18 files changed

+89
-58
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
source/test/fixtures

.eslintrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": ["xo", "xo-typescript"],
3+
"parserOptions": {
4+
"project": "tsconfig.tsd.json"
5+
},
6+
"rules": {
7+
"@typescript-eslint/comma-dangle": "off"
8+
}
9+
}

package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"test": "npm run lint && ava",
2222
"build": "npm run clean && tsc --project tsconfig.tsd.json && chmod +x dist/cli.js",
2323
"clean": "del-cli dist",
24-
"lint": "tslint -p tsconfig.tsd.json --format stylish"
24+
"lint": "eslint 'source/**/*'",
25+
"lint:fix": "eslint --fix 'source/**/*'"
2526
},
2627
"files": [
2728
"dist/**/*.js",
@@ -49,15 +50,18 @@
4950
"@ava/typescript": "^1.1.1",
5051
"@types/node": "^14.0.0",
5152
"@types/react": "^16.9.2",
53+
"@typescript-eslint/eslint-plugin": "^4.26.0",
54+
"@typescript-eslint/parser": "^4.26.0",
5255
"ava": "^3.8.2",
5356
"cpy-cli": "^3.0.0",
5457
"del-cli": "^3.0.0",
58+
"eslint": "^7.27.0",
59+
"eslint-config-xo": "^0.36.0",
60+
"eslint-config-xo-typescript": "^0.41.1",
5561
"execa": "^5.0.0",
5662
"react": "^16.9.0",
5763
"rxjs": "^6.5.3",
58-
"tslint": "^5.11.0",
59-
"tslint-xo": "^0.9.0",
60-
"typescript": "^4.1.5"
64+
"typescript": "^4.3.2"
6165
},
6266
"ava": {
6367
"timeout": "2m",

source/cli.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const cli = meow(`
1616
✖ 10:20 Argument of type string is not assignable to parameter of type number.
1717
`);
1818

19-
(async () => { // tslint:disable-line:no-floating-promises
19+
(async () => {
2020
try {
2121
const options = cli.input.length > 0 ? {cwd: cli.input[0]} : undefined;
2222

@@ -25,8 +25,11 @@ const cli = meow(`
2525
if (diagnostics.length > 0) {
2626
throw new Error(formatter(diagnostics));
2727
}
28-
} catch (error) {
29-
console.error(error.message);
28+
} catch (error: unknown) {
29+
if (error && typeof (error as Error).message === 'string') {
30+
console.error((error as Error).message);
31+
}
32+
3033
process.exit(1);
3134
}
3235
})();

source/lib/assertions/assert.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
/* eslint-disable @typescript-eslint/no-unused-vars */
2+
13
/**
24
* Check that the type of `value` is identical to type `T`.
35
*
46
* @param value - Value that should be identical to type `T`.
57
*/
6-
// @ts-ignore
7-
export const expectType = <T>(value: T) => { // tslint:disable-line:no-unused
8+
// @ts-expect-error
9+
export const expectType = <T>(value: T) => {
810
// Do nothing, the TypeScript compiler handles this for us
911
};
1012

@@ -13,8 +15,9 @@ export const expectType = <T>(value: T) => { // tslint:disable-line:no-unused
1315
*
1416
* @param value - Value that should be identical to type `T`.
1517
*/
16-
// @ts-ignore
17-
export const expectNotType = <T>(value: any) => { // tslint:disable-line:no-unused
18+
// @ts-expect-error
19+
export const expectNotType = <T>(value: any) => {
20+
// eslint-disable-next-line no-warning-comments
1821
// TODO Use a `not T` type when possible https://github.com/microsoft/TypeScript/pull/29317
1922
// Do nothing, the TypeScript compiler handles this for us
2023
};
@@ -24,8 +27,8 @@ export const expectNotType = <T>(value: any) => { // tslint:disable-line:no-unu
2427
*
2528
* @param value - Value that should be assignable to type `T`.
2629
*/
27-
// @ts-ignore
28-
export const expectAssignable = <T>(value: T) => { // tslint:disable-line:no-unused
30+
// @ts-expect-error
31+
export const expectAssignable = <T>(value: T) => {
2932
// Do nothing, the TypeScript compiler handles this for us
3033
};
3134

@@ -34,8 +37,8 @@ export const expectAssignable = <T>(value: T) => { // tslint:disable-line:no-un
3437
*
3538
* @param value - Value that should not be assignable to type `T`.
3639
*/
37-
// @ts-ignore
38-
export const expectNotAssignable = <T>(value: any) => { // tslint:disable-line:no-unused
40+
// @ts-expect-error
41+
export const expectNotAssignable = <T>(value: any) => {
3942
// Do nothing, the TypeScript compiler handles this for us
4043
};
4144

@@ -44,8 +47,8 @@ export const expectNotAssignable = <T>(value: any) => { // tslint:disable-line:
4447
*
4548
* @param value - Value that should be checked.
4649
*/
47-
// @ts-ignore
48-
export const expectError = <T = any>(value: T) => { // tslint:disable-line:no-unused
50+
// @ts-expect-error
51+
export const expectError = <T = any>(value: T) => {
4952
// Do nothing, the TypeScript compiler handles this for us
5053
};
5154

@@ -54,8 +57,8 @@ export const expectError = <T = any>(value: T) => { // tslint:disable-line:no-u
5457
*
5558
* @param expression - Expression that should be marked as `@deprecated`.
5659
*/
57-
// @ts-ignore
58-
export const expectDeprecated = (expression: any) => { // tslint:disable-line:no-unused
60+
// @ts-expect-error
61+
export const expectDeprecated = (expression: any) => {
5962
// Do nothing, the TypeScript compiler handles this for us
6063
};
6164

@@ -64,8 +67,8 @@ export const expectDeprecated = (expression: any) => { // tslint:disable-line:n
6467
*
6568
* @param expression - Expression that should not be marked as `@deprecated`.
6669
*/
67-
// @ts-ignore
68-
export const expectNotDeprecated = (expression: any) => { // tslint:disable-line:no-unused
70+
// @ts-expect-error
71+
export const expectNotDeprecated = (expression: any) => {
6972
// Do nothing, the TypeScript compiler handles this for us
7073
};
7174

@@ -74,7 +77,7 @@ export const expectNotDeprecated = (expression: any) => { // tslint:disable-lin
7477
*
7578
* @param expression - Expression whose type should be printed as a warning.
7679
*/
77-
// @ts-ignore
78-
export const printType = (expression: any) => { // tslint:disable-line:no-unused
80+
// @ts-expect-error
81+
export const printType = (expression: any) => {
7982
// Do nothing, the TypeScript compiler handles this for us
8083
};

source/lib/assertions/handlers/expect-deprecated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const expectDeprecatedHelper = (options: Options): Handler => {
2929

3030
const message = tsutils.expressionToString(checker, argument);
3131

32-
diagnostics.push(makeDiagnostic(node, options.message(message || '?')));
32+
diagnostics.push(makeDiagnostic(node, options.message(message ?? '?')));
3333
}
3434

3535
return diagnostics;

source/lib/compiler.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import {
22
flattenDiagnosticMessageText,
33
createProgram,
4-
Diagnostic as TSDiagnostic,
5-
SourceFile
4+
Diagnostic as TSDiagnostic
65
} from '@tsd/typescript';
76
import {ExpectedError, extractAssertions, parseErrorAssertionToLocation} from './parser';
87
import {Diagnostic, DiagnosticCode, Context, Location} from './interfaces';
@@ -63,10 +62,12 @@ const ignoreDiagnostic = (
6362
return 'preserve';
6463
}
6564

66-
const diagnosticFileName = (diagnostic.file as SourceFile).fileName;
65+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
66+
const diagnosticFileName = diagnostic.file!.fileName;
6767

6868
for (const [location] of expectedErrors) {
69-
const start = diagnostic.start as number;
69+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
70+
const start = diagnostic.start!;
7071

7172
if (diagnosticFileName === location.fileName && start > location.start && start < location.end) {
7273
return location;
@@ -116,7 +117,8 @@ export const getDiagnostics = (context: Context): Diagnostic[] => {
116117
continue;
117118
}
118119

119-
const position = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start as number);
120+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
121+
const position = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start!);
120122

121123
diagnostics.push({
122124
fileName: diagnostic.file.fileName,

source/lib/config.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ import {
1010
parseJsonSourceFileConfigFileContent,
1111
ModuleKind
1212
} from '@tsd/typescript';
13-
import {Config, RawConfig, RawCompilerOptions} from './interfaces';
13+
import {Config, PackageJsonWithTsdConfig, RawCompilerOptions} from './interfaces';
1414

1515
/**
1616
* Load the configuration settings.
1717
*
1818
* @param pkg - The package.json object.
1919
* @returns The config object.
2020
*/
21-
export default (pkg: {tsd?: RawConfig}, cwd: string): Config => {
22-
const pkgConfig = pkg.tsd || {};
21+
export default (pkg: PackageJsonWithTsdConfig, cwd: string): Config => {
22+
const pkgConfig = pkg.tsd ?? {};
2323

2424
const tsConfigCompilerOptions = getOptionsFromTsConfig(cwd);
2525
const packageJsonCompilerOptions = parseCompilerConfigObject(
26-
pkgConfig.compilerOptions || {},
26+
pkgConfig.compilerOptions ?? {},
2727
cwd
2828
);
2929

@@ -70,5 +70,5 @@ function parseCompilerConfigObject(compilerOptions: RawCompilerOptions, cwd: str
7070
}
7171

7272
function parseRawLibs(libs: string[], cwd: string): string[] {
73-
return parseCompilerConfigObject({lib: libs}, cwd).lib || [];
73+
return parseCompilerConfigObject({lib: libs}, cwd).lib ?? [];
7474
}

source/lib/formatter.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
import * as formatter from 'eslint-formatter-pretty';
22
import {Diagnostic} from './interfaces';
33

4+
interface FileWithDiagnostics {
5+
filePath: string;
6+
errorCount: number;
7+
warningCount: number;
8+
messages: Diagnostic[];
9+
}
10+
411
/**
512
* Format the TypeScript diagnostics to a human readable output.
613
*
714
* @param diagnostics - List of TypeScript diagnostics.
815
* @returns Beautiful diagnostics output
916
*/
10-
export default (diagnostics: Diagnostic[]) => {
11-
const fileMap = new Map<string, any>();
17+
export default (diagnostics: Diagnostic[]): string => {
18+
const fileMap = new Map<string, FileWithDiagnostics>();
1219

1320
for (const diagnostic of diagnostics) {
1421
let entry = fileMap.get(diagnostic.fileName);
@@ -28,5 +35,6 @@ export default (diagnostics: Diagnostic[]) => {
2835
entry.messages.push(diagnostic);
2936
}
3037

31-
return formatter(Array.from(fileMap.values()));
38+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
39+
return String(formatter([...fileMap.values()]));
3240
};

source/lib/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import * as globby from 'globby';
55
import {getDiagnostics as getTSDiagnostics} from './compiler';
66
import loadConfig from './config';
77
import getCustomDiagnostics from './rules';
8-
import {Context, Config} from './interfaces';
8+
import {Context, Config, Diagnostic, PackageJsonWithTsdConfig} from './interfaces';
99

1010
export interface Options {
1111
cwd: string;
1212
typingsFile?: string;
1313
testFiles?: readonly string[];
1414
}
1515

16-
const findTypingsFile = async (pkg: any, options: Options) => {
16+
const findTypingsFile = async (pkg: PackageJsonWithTsdConfig, options: Options): Promise<string> => {
1717
const typings =
1818
options.typingsFile ||
1919
pkg.types ||
@@ -78,15 +78,15 @@ const findTestFiles = async (typingsFilePath: string, options: Options & {config
7878
*
7979
* @returns A promise which resolves the diagnostics of the type definition.
8080
*/
81-
export default async (options: Options = {cwd: process.cwd()}) => {
81+
export default async (options: Options = {cwd: process.cwd()}): Promise<Diagnostic[]> => {
8282
const pkgResult = await readPkgUp({cwd: options.cwd});
8383

8484
if (!pkgResult) {
8585
throw new Error('No `package.json` file found. Make sure you are running the command in a Node.js project.');
8686
}
8787

88-
const pkg = pkgResult.packageJson;
89-
const config = loadConfig(pkg as any, options.cwd);
88+
const pkg = pkgResult.packageJson as PackageJsonWithTsdConfig;
89+
const config = loadConfig(pkg, options.cwd);
9090

9191
// Look for a typings file, otherwise use `index.d.ts` in the root directory. If the file is not found, throw an error.
9292
const typingsFile = await findTypingsFile(pkg, options);

0 commit comments

Comments
 (0)