8000 feat: Support for tslint property · uniqueiniquity/typescript-eslint@2e6776e · GitHub
[go: up one dir, main page]

Skip to content

Commit 2e6776e

Browse files
romanJamesHenry
authored andcommitted
feat: Support for tslint property
#40
1 parent 3fc7e87 commit 2e6776e

File tree

6 files changed

+58
-32
lines changed

6 files changed

+58
-32
lines changed

packages/eslint-plugin-tslint/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Configure in your eslint config file:
2525
rules: { /* tslint rules */ },
2626
rulesDirectory: [ /* array of paths to directories with rules, e.g. 'node_modules/tslint/lib/rules' */ ],
2727
configFile: '/* path to tsconfig.json of your project */',
28+
lintFile: '/* path to tslint.json of your project */',
2829
compilerOptions: { /* ability to override TypeScript compilers options defined in tsconfig.json */ }
2930
}],
3031
}

packages/eslint-plugin-tslint/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"cz": "git-cz"
3535
},
3636
"dependencies": {
37+
"lodash.memoize": "^4.1.2",
3738
"typescript-service": "^2.0.3"
3839
},
3940
"peerDependencies": {
@@ -43,6 +44,7 @@
4344
"@semantic-release/changelog": "^3.0.0",
4445
"@semantic-release/git": "^7.0.3",
4546
"@types/eslint": "^4.16.3",
47+
"@types/lodash.memoize": "^4.1.4",
4648
"@types/mocha": "^5.2.5",
4749
"@types/node": "^10.9.3",
4850
"cz-conventional-changelog": "^2.1.0",

packages/eslint-plugin-tslint/src/custom-linter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class CustomLinter extends TSLintLinter {
1818
return super.getSourceFile(fileName, source);
1919
}
2020
const service = typescriptService();
21-
const result = service.getSourceFile(fileName, source);
21+
const result = service.getSourceFile(fileName, source);
2222
this.program = service.getProgram();
2323
return result;
2424
}

packages/eslint-plugin-tslint/src/index.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import * as ts from 'typescript';
1212
import { Rule } from 'eslint';
1313
import { typescriptService } from './typescript-service';
1414
import { CustomLinter } from './custom-linter';
15-
15+
import memoize = require('lodash.memoize');
1616

1717
//------------------------------------------------------------------------------
1818
// Plugin Definition
@@ -32,10 +32,19 @@ interface RawRulesConfig {
3232
[key: string]: RawRuleConfig;
3333
}
3434

35-
interface TSLintPluginOptions {
36-
rulesDirectory?: string[];
37-
rules?: RawRulesConfig;
38-
}
35+
36+
/**
37+
* Construct a configFile for TSLint
38+
*/
39+
const tslintConfig = memoize((lintFile: string, tslintRules: RawRulesConfig, tslintRulesDirectory: string[]) => {
40+
if (lintFile != null) {
41+
return Configuration.loadConfigurationFromPath(lintFile);
42+
}
43+
return Configuration.parseConfigFile({
44+
rules: tslintRules || {},
45+
rulesDirectory: tslintRulesDirectory || [],
46+
});
47+
}, (lintFile, tslintRules = {}, tslintRulesDirectory = []) => `${lintFile}_${Object.keys(tslintRules).join(',')}_${tslintRulesDirectory.length}`);
3948

4049
export const rules = {
4150
/**
@@ -69,6 +78,9 @@ export const rules = {
6978
configFile: {
7079
type: 'string',
7180
},
81+
lintFile: {
82+
type: 'string',
83+
},
7284
compilerOptions: {
7385
type: 'object',
7486
additionalProperties: true,
@@ -89,24 +101,10 @@ export const rules = {
89101
rules: tslintRules,
90102
rulesDirectory: tslintRulesDirectory,
91103
configFile,
104+
lintFile,
92105
compilerOptions,
93106
} = context.options[0];
94107

95-
const tslintOptions = {
96-
formatter: 'json',
97-
fix: false,
98-
rulesDirectory: tslintRulesDirectory,
99-
};
100-
101-
/**
102-
* Manually construct a configFile for TSLint
103-
*/
104-
const rawConfig: TSLintPluginOptions = {};
105-
rawConfig.rules = tslintRules || {};
106-
rawConfig.rulesDirectory = tslintRulesDirectory || [];
107-
108-
const tslintConfig = Configuration.parseConfigFile(rawConfig);
109-
110108
let program: ts.Program | undefined = undefined;
111109

112110
if (fileName !== '<input>' && configFile) {
@@ -116,14 +114,16 @@ export const rules = {
116114

117115
/**
118116
* Create an instance of TSLint
119-
*/
120-
const tslint = new CustomLinter(tslintOptions, program);
121-
122-
/**
123117
* Lint the source code using the configured TSLint instance, and the rules which have been
124118
* passed via the ESLint rule options for this rule (using "tslint/config")
125119
*/
126-
tslint.lint(fileName, sourceCode, tslintConfig);
120+
const tslintOptions = {
121+
formatter: 'json',
122+
fix: false,
123+
};
124+
const tslint = new CustomLinter(tslintOptions, program);
125+
const configuration = tslintConfig(lintFile, tslintRules, tslintRulesDirectory);
126+
tslint.lint(fileName, sourceCode, configuration);
127127

128128
const result = tslint.getResult();
129129

packages/eslint-plugin-tslint/test/index.spec.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ ruleTester.run('tslint/config', rules.config, {
5050
},
5151
{
5252
filename: './test/test-project/file-spec.ts',
53-
code: readFileSync('./test/test-project/file-spec.ts', 'utf8'),
53+
code: readFileSync('./test/test-project/file-spec.ts', 'utf8').replace(/\n/g, ' '),
5454
parser: 'typescript-eslint-parser',
5555
parserOptions,
5656
options: [
@@ -60,15 +60,30 @@ ruleTester.run('tslint/config', rules.config, {
6060
},
6161
],
6262
},
63+
{
64+
code: 'throw "should be ok because rule is not loaded";',
65+
parser: 'typescript-eslint-parser',
66+
parserOptions,
67+
options: [tslintRulesConfig],
68+
},
6369
],
6470

6571
invalid: [
6672
{
67-
code: 'var foo = true',
73+
options: [{ lintFile: './test/test-project/tslint.json' }],
74+
parser: 'typescript-eslint-parser',
75+
parserOptions,
76+
code: 'throw "err" // no-string-throw',
77+
errors: [
78+
{ message: 'Throwing plain strings (not instances of Error) gives no stack traces (tslint:no-string-throw)' },
79+
],
80+
},
81+
{
82+
code: 'var foo = true // semicolon',
6883
parser: 'typescript-eslint-parser',
6984
parserOptions,
7085
options: [tslintRulesConfig],
71-
output: 'var foo = true',
86+
output: 'var foo = true // semicolon',
7287
errors: [
7388
{
7489
message: 'Missing semicolon (tslint:semicolon)',
@@ -78,11 +93,11 @@ ruleTester.run('tslint/config', rules.config, {
7893
],
7994
},
8095
{
81-
code: 'var foo = true',
96+
code: 'var foo = true // fail',
8297
parser: 'typescript-eslint-parser',
8398
parserOptions,
8499
options: [tslintRulesDirectoryConfig],
85-
output: 'var foo = true',
100+
output: 'var foo = true // fail',
86101
errors: [
87102
{
88103
message: 'failure (tslint:always-fail)',
@@ -93,7 +108,7 @@ ruleTester.run('tslint/config', rules.config, {
93108
},
94109
{
95110
filename: './test/test-project/source.ts',
96-
code: readFileSync('./test/test-project/source.ts', 'utf8'),
111+
code: readFileSync('./test/test-project/source.ts', 'utf8').replace(/\n/g, ' '),
97112
parser: 'typescript-eslint-parser',
98113
parserOptions,
99114
options: [
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"rules": {
3+
"no-string-throw": true,
4+
"no-var-keyword": true,
5+
"prefer-const": true,
6+
"radix": true
7+
}
8+
}

0 commit comments

Comments
 (0)
0