From 8556df16833ba53459f0eaa39a8bd6a065e86063 Mon Sep 17 00:00:00 2001 From: Norman Rusch Date: Wed, 13 Oct 2021 12:31:01 +0200 Subject: [PATCH] Add missing no-shadow rule for typescript --- rules/__fixtures__/typescript/no-shadow.ts | 12 ++++++++++ rules/typescript.js | 8 +++++++ rules/typescript.test.js | 27 ++++++++++++++++++++-- 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 rules/__fixtures__/typescript/no-shadow.ts diff --git a/rules/__fixtures__/typescript/no-shadow.ts b/rules/__fixtures__/typescript/no-shadow.ts new file mode 100644 index 0000000..b3df102 --- /dev/null +++ b/rules/__fixtures__/typescript/no-shadow.ts @@ -0,0 +1,12 @@ +enum DisplayType { + CARDS = 'cards', + LIST = 'list', + ICONS = 'icons' +} + +export const display: DisplayType = DisplayType.ICONS; + +export function toggleType(next = DisplayType): DisplayType { + const display = next; + return display; +} diff --git a/rules/typescript.js b/rules/typescript.js index 03cc56d..e023cb9 100644 --- a/rules/typescript.js +++ b/rules/typescript.js @@ -32,6 +32,14 @@ module.exports = { }, }, rules: { + // Enforce no variable declarations from shadowing variables declared in + // the outer scope: This rule extends the base eslint/no-shadow rule. It + // adds support for TypeScript's this parameters and global augmentation, + // and adds options for TypeScript features. + // https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-shadow.md + '@typescript-eslint/no-shadow': 'error', + 'no-shadow': 'off', + // Enforce no unused vars: This rule extends the base "eslint/no-unused-vars" // rule. It adds support for TypeScript features, such as types. // https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-unused-vars.md diff --git a/rules/typescript.test.js b/rules/typescript.test.js index 4dc55ff..b116404 100644 --- a/rules/typescript.test.js +++ b/rules/typescript.test.js @@ -20,6 +20,30 @@ describe('typescript rules', () => { linter = null; }); + describe('no-shadow', () => { + it('should error', async () => { + const filePath = join(__dirname, '__fixtures__', 'typescript', 'no-shadow.ts'); + const [{ messages, ...rest }] = await linter.lintFiles([filePath]); + + expect(messages).toEqual([ + expect.objectContaining({ + line: 10, + messageId: 'noShadow', + ruleId: '@typescript-eslint/no-shadow', + }), + ]); + + expect(rest).toEqual( + expect.objectContaining({ + errorCount: 1, + warningCount: 0, + fixableErrorCount: 0, + fixableWarningCount: 0, + }), + ); + }); + }); + describe('no-unused-vars', () => { it('should error', async () => { const filePath = join(__dirname, '__fixtures__', 'typescript', 'no-unused-vars.ts'); @@ -67,12 +91,11 @@ describe('typescript rules', () => { messageId: 'noUseBeforeDefine', ruleId: '@typescript-eslint/no-use-before-define', }), - expect.any(Object), ]); expect(rest).toEqual( expect.objectContaining({ - errorCount: 2, + errorCount: 1, warningCount: 0, fixableErrorCount: 0, fixableWarningCount: 0,