Skip to content

[Fixes #174] implement no-shadowed-variable converter #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/rules/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import { convertNoReference } from "./converters/no-reference";
import { convertNoRegexSpaces } from "./converters/no-regex-spaces";
import { convertNoRequireImports } from "./converters/no-require-imports";
import { convertNoReturnAwait } from "./converters/no-return-await";
import { convertNoShadowedVariable } from "./converters/no-shadowed-variable";
import { convertNoSparseArrays } from "./converters/no-sparse-arrays";
import { convertNoStringLiteral } from "./converters/no-string-literal";
import { convertNoStringThrow } from "./converters/no-string-throw";
Expand Down Expand Up @@ -186,6 +187,7 @@ export const converters = new Map([
["no-regex-spaces", convertNoRegexSpaces],
["no-require-imports", convertNoRequireImports],
["no-return-await", convertNoReturnAwait],
["no-shadowed-variable", convertNoShadowedVariable],
["no-sparse-arrays", convertNoSparseArrays],
["no-string-literal", convertNoStringLiteral],
["no-string-throw", convertNoStringThrow],
Expand Down Expand Up @@ -237,7 +239,6 @@ export const converters = new Map([
// ["ban", convertBan], // no-restricted-properties
// ["import-blacklist", convertImportBlacklist], // no-restricted-imports
// ["no-duplicate-variable", convertNoDuplicateVariable], // no-redeclare
// ["no-shadowed-variable", convertNoShadowedVariable], // no-shadow
// ["no-unused-expression", convertNoUnusedExpression], // no-unused-expressions
// ["space-within-parens", convertSpaceWithinParens], // space-in-parens
// ["variable-name", convertVariableName], // a bunch of rules...
Expand Down
49 changes: 49 additions & 0 deletions src/rules/converters/no-shadowed-variable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { RuleConverter } from "../converter";

const SELECTIVE_DISABLE_NOTICE =
"ESLint does not support selectively disabling shadowed declaration checks " +
"depending on the type of declaration, so all kinds of declarations are checked.";

const UNDERSCORE_DISABLE_NOTICE =
"ESLint does not support disabling shadowed variable checks based on " +
"whether their names start with underscore or not, please use 'allow' in eslint configuration to " +
"provide specific variable names you want to disable the rule for.";

export const convertNoShadowedVariable: RuleConverter = tslintRule => {
const ruleArguments: { hoist: "all" | "never" }[] = [];
const notices: string[] = [];

if (tslintRule.ruleArguments.length === 0 || !(tslintRule.ruleArguments[0] instanceof Object)) {
ruleArguments.push({ hoist: "all" });
} else {
const config: Record<string, boolean> = tslintRule.ruleArguments[0];

if (config.underscore === false) {
notices.push(UNDERSCORE_DISABLE_NOTICE);
}

if (config.temporalDeadZone === false) {
ruleArguments.push({ hoist: "never" });
} else {
ruleArguments.push({ hoist: "all" });
}

const hasUnsupportedDisables = Object.entries(config).some(
([key, value]) => value === false && key !== "underscore" && key !== "temporalDeadZone",
);

if (hasUnsupportedDisables) {
notices.push(SELECTIVE_DISABLE_NOTICE);
}
}

return {
rules: [
{
...(notices.length > 0 && { notices }),
...(ruleArguments.length > 0 && { ruleArguments }),
ruleName: "no-shadow",
},
],
};
};
85 changes: 85 additions & 0 deletions src/rules/converters/tests/no-shadowed-variable.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { convertNoShadowedVariable } from "../no-shadowed-variable";

describe(convertNoShadowedVariable, () => {
test("conversion without parameter", () => {
const result = convertNoShadowedVariable({
ruleArguments: [],
});

expect(result).toEqual({
rules: [
{
ruleArguments: [{ hoist: "all" }],
ruleName: "no-shadow",
},
],
});
});

test("conversion with non-object parameter", () => {
const result = convertNoShadowedVariable({
ruleArguments: ["will-be-ignored"],
});

expect(result).toEqual({
rules: [
{
ruleArguments: [{ hoist: "all" }],
ruleName: "no-shadow",
},
],
});
});

test("conversion with empty parameter object", () => {
const result = convertNoShadowedVariable({
ruleArguments: [{}],
});

expect(result).toEqual({
rules: [
{
ruleArguments: [{ hoist: "all" }],
ruleName: "no-shadow",
},
],
});
});

test("conversion with disabled 'temporalDeadZone'", () => {
const result = convertNoShadowedVariable({
ruleArguments: [{ temporalDeadZone: false }],
});

expect(result).toEqual({
rules: [
{
ruleArguments: [{ hoist: "never" }],
ruleName: "no-shadow",
},
],
});
});

test("conversion with disabled declaration types", () => {
const result = convertNoShadowedVariable({
ruleArguments: [{ class: false, underscore: false }],
});

expect(result).toEqual({
rules: [
{
notices: [
"ESLint does not support disabling shadowed variable checks based on " +
"whether their names start with underscore or not, please use 'allow' in eslint configuration to " +
"provide specific variable names you want to disable the rule for.",
"ESLint does not support selectively disabling shadowed declaration checks " +
"depending on the type of declaration, so all kinds of declarations are checked.",
],
ruleArguments: [{ hoist: "all" }],
ruleName: "no-shadow",
},
],
});
});
});