From 5c8da94690e7f9843e71a9d54a55ab2d5b35bc1d Mon Sep 17 00:00:00 2001 From: Armano Date: Thu, 21 Feb 2019 00:31:21 +0100 Subject: [PATCH 1/2] test(plugin-tslint): missing test cases for eslint-plugin-tslint --- .eslintrc.json | 5 ++- .../eslint-plugin-tslint/src/custom-linter.ts | 3 +- packages/eslint-plugin-tslint/src/index.ts | 2 +- .../eslint-plugin-tslint/tests/index.spec.ts | 35 +++++++++++++++++-- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 56d5ce4c1a34..e3abc256901a 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -54,7 +54,10 @@ } }, { - "files": ["packages/eslint-plugin/test/**/*.ts"], + "files": [ + "packages/eslint-plugin/test/**/*.ts", + "packages/eslint-plugin-tslint/tests/**/*.spec.ts" + ], "rules": { "eslint-plugin/no-identical-tests": "error" } diff --git a/packages/eslint-plugin-tslint/src/custom-linter.ts b/packages/eslint-plugin-tslint/src/custom-linter.ts index 6775dc36f8d7..892e12f1adf0 100644 --- a/packages/eslint-plugin-tslint/src/custom-linter.ts +++ b/packages/eslint-plugin-tslint/src/custom-linter.ts @@ -13,7 +13,6 @@ export class CustomLinter extends TSLintLinter { } getSourceFile(fileName: string) { - const result = this.program.getSourceFile(fileName); - return result; + return this.program.getSourceFile(fileName); } } diff --git a/packages/eslint-plugin-tslint/src/index.ts b/packages/eslint-plugin-tslint/src/index.ts index 245e05eb385d..46574a106c8c 100644 --- a/packages/eslint-plugin-tslint/src/index.ts +++ b/packages/eslint-plugin-tslint/src/index.ts @@ -83,7 +83,7 @@ export const rules = { }, ], }, - create: function(context: Rule.RuleContext) { + create(context: Rule.RuleContext) { const fileName = context.getFilename(); const sourceCode = context.getSourceCode().text; const parserServices: ParserServices | undefined = context.parserServices; diff --git a/packages/eslint-plugin-tslint/tests/index.spec.ts b/packages/eslint-plugin-tslint/tests/index.spec.ts index 7d92fcf0c5c1..501ef0fc80d4 100644 --- a/packages/eslint-plugin-tslint/tests/index.spec.ts +++ b/packages/eslint-plugin-tslint/tests/index.spec.ts @@ -1,5 +1,5 @@ -import { rules } from '../src/index'; -import { RuleTester } from 'eslint'; +import { rules } from '../src'; +import { Linter, RuleTester } from 'eslint'; import { readFileSync } from 'fs'; const ruleTester = new RuleTester({ @@ -125,3 +125,34 @@ ruleTester.run('tslint/config', rules.config, { }, ], }); + +describe('tslint/error', () => { + function testOutput(code: string, config: Linter.Config): void { + const linter = new Linter(); + linter.defineRule('tslint/config', rules.config); + + expect(() => linter.verify(code, config)).toThrow( + `You must provide a value for the "parserOptions.project" property for @typescript-eslint/parser`, + ); + } + + it('should error on missing project', () => { + testOutput('foo;', { + rules: { + 'tslint/config': [2, tslintRulesConfig], + }, + parser: '@typescript-eslint/parser', + }); + }); + + it('should error on default parser', () => { + testOutput('foo;', { + parserOptions: { + project: `${__dirname}/test-project/tsconfig.json`, + }, + rules: { + 'tslint/config': [2, tslintRulesConfig], + }, + }); + }); +}); From a774e11a5bf16f2541a991da0fcec9dd20ef2865 Mon Sep 17 00:00:00 2001 From: Armano Date: Thu, 21 Feb 2019 00:55:17 +0100 Subject: [PATCH 2/2] test(plugin-tslint): add test case for missing rules --- .../eslint-plugin-tslint/tests/index.spec.ts | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/eslint-plugin-tslint/tests/index.spec.ts b/packages/eslint-plugin-tslint/tests/index.spec.ts index 501ef0fc80d4..ed5894ca045c 100644 --- a/packages/eslint-plugin-tslint/tests/index.spec.ts +++ b/packages/eslint-plugin-tslint/tests/index.spec.ts @@ -155,4 +155,28 @@ describe('tslint/error', () => { }, }); }); + + it('should not crash if there is no tslint rules specified', () => { + const linter = new Linter(); + jest.spyOn(console, 'warn').mockImplementation(); + linter.defineRule('tslint/config', rules.config); + expect(() => + linter.verify('foo;', { + parserOptions: { + project: `${__dirname}/test-project/tsconfig.json`, + }, + rules: { + 'tslint/config': [2, {}], + }, + parser: '@typescript-eslint/parser', + }), + ).not.toThrow(); + + expect(console.warn).toHaveBeenCalledWith( + expect.stringContaining( + 'No valid rules have been specified for TypeScript files', + ), + ); + jest.resetAllMocks(); + }); });