Skip to content
This repository was archived by the owner on Jan 18, 2019. It is now read-only.

Commit 20b498b

Browse files
author
roman.vasilev
committed
fix: Fixed bug when file not defined in tsconfig
1 parent 2e54c42 commit 20b498b

File tree

8 files changed

+73
-9
lines changed

8 files changed

+73
-9
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"test": "npm run tscheck && npm run test:r",
2424
"tscheck": "echo tscheck... && tsc --noEmit",
2525
"test:r": "jest --runInBand --verbose",
26+
"test:w": "jest --watch --runInBand",
2627
"semantic-release": "semantic-release",
2728
"prepublishOnly": "npm run build && sed -i -e 's/devDependencies/_devDependencies/g' package.json",
2829
"build": "tsc",

src/custom-linter.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as ts from 'typescript';
2+
import { ILinterOptions, LintResult } from 'tslint';
3+
import { typescriptService } from './typescript-service';
4+
const { Linter: TSLintLinter } = require('tslint');
5+
6+
export class CustomLinter extends TSLintLinter {
7+
8+
constructor(options: ILinterOptions, program?: ts.Program | undefined) {
9+
super(options, program);
10+
}
11+
12+
getResult(): LintResult {
13+
return super.getResult();
14+
}
15+
16+
getSourceFile(fileName: string, source: string) {
17+
if (this.program === undefined) {
18+
return super.getSourceFile(fileName, source);
19+
}
20+
const service = typescriptService();
21+
return service.getSourceFile(fileName);
22+
}
23+
}

src/index.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
// Requirements
88
//------------------------------------------------------------------------------
99

10-
import { Linter as TSLintLinter, RuleSeverity, Configuration } from 'tslint';
10+
import { RuleSeverity, Configuration } from 'tslint';
1111
import * as ts from 'typescript';
1212
import { Rule } from 'eslint';
13-
import { createService } from 'typescript-service';
13+
import { typescriptService } from './typescript-service';
14+
import { CustomLinter } from './custom-linter';
15+
1416

1517
//------------------------------------------------------------------------------
1618
// Plugin Definition
@@ -35,8 +37,6 @@ interface TSLintPluginOptions {
3537
rules?: RawRulesConfig;
3638
}
3739

38-
let languageService: ReturnType<typeof createService>;
39-
4040
export const rules = {
4141
/**
4242
* Expose a single rule called "config", which will be accessed in the user's eslint config files
@@ -110,16 +110,14 @@ export const rules = {
110110
let program: ts.Program | undefined = undefined;
111111

112112
if (fileName !== '<input>' && configFile) {
113-
if (!languageService) {
114-
languageService = createService({ configFile, compilerOptions });
115-
}
116-
program = languageService.getProgram();
113+
const service = typescriptService({ configFile, compilerOptions });
114+
program = service.getProgram();
117115
}
118116

119117
/**
120118
* Create an instance of TSLint
121119
*/
122-
const tslint = new TSLintLinter(tslintOptions, program);
120+
const tslint = new CustomLinter(tslintOptions, program);
123121

124122
/**
125123
* Lint the source code using the configured TSLint instance, and the rules which have been

src/typescript-service.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { createService } from 'typescript-service';
2+
import * as ts from 'typescript';
3+
4+
let service: ReturnType<typeof createService>;
5+
6+
type createServiceOptions = {
7+
configFile: string;
8+
compilerOptions?: ts.CompilerOptions;
9+
};
10+
11+
export function typescriptService(options?: createServiceOptions) {
12+
if (service === undefined) {
13+
if (options === undefined) {
14+
throw new Error('Service is not yet created');
15+
}
16+
service = createService(options);
17+
}
18+
return service;
19+
}

test/index.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ ruleTester.run('tslint/config', rules.config, {
4747
parserOptions,
4848
options: [tslintRulesConfig],
4949
},
50+
{
51+
filename: './test/test-project/file-spec.ts',
52+
code: '/* file not defined in tsconfig */',
53+
parser: 'typescript-eslint-parser',
54+
parserOptions,
55+
options: [
56+
{
57+
...tslintRulesConfig,
58+
configFile: `${__dirname}/test-project/tsconfig-files.json`,
59+
},
60+
],
61+
},
5062
],
5163

5264
invalid: [

test/test-project/file-spec.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { x } from './file';
2+
console.assert(x);

test/test-project/file.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as number from './number';
2+
export const x = 1;
3+
console.log(number);

test/test-project/tsconfig-files.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"lib": ["dom"],
3+
"files": [
4+
"file.ts"
5+
]
6+
}

0 commit comments

Comments
 (0)