Skip to content

Commit

Permalink
Add missing no-shadow rule for typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
Norman Rusch committed Oct 13, 2021
1 parent 27a9cdb commit 8556df1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
12 changes: 12 additions & 0 deletions rules/__fixtures__/typescript/no-shadow.ts
Original file line number Diff line number Diff line change
@@ -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;
}
8 changes: 8 additions & 0 deletions rules/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 25 additions & 2 deletions rules/typescript.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 8556df1

Please sign in to comment.