Skip to content

feat: add import-blacklist converter #277 #280

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
60 changes: 60 additions & 0 deletions src/rules/converters/import-blacklist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { RuleConverter } from "../converter";
import { RequireAtLeastOne } from "../../utils";

type ESLintOptionPath = {
name: string;
importNames: string[];
message?: string;
};
type ESLintSimpleOption = string[];
type ESLintComplexOption = RequireAtLeastOne<{
paths: (string | ESLintOptionPath)[];
patterns: string[];
}>;
type ESLintOptions = ESLintSimpleOption | ESLintComplexOption;

const NOTICE_MATCH_PATTERNS =
"ESLint and TSLint use different strategies to match patterns. TSLint uses standard regular expressions, but ESLint .gitignore spec.";

export const convertImportBlacklist: RuleConverter = tslintRule => {
let ruleArguments: ESLintOptions = [];
const notices = [];

if (tslintRule.ruleArguments.every(isString)) {
ruleArguments = tslintRule.ruleArguments;
} else {
const objectOption = tslintRule.ruleArguments.reduce((rules, rule) => {
if (!Array.isArray(rule)) {
const eslintRule = isString(rule)
? rule
: {
name: Object.keys(rule)[0],
importNames: Object.values(rule)[0] as string[],
};
return { ...rules, paths: [...(rules.paths || []), eslintRule] };
}

return { ...rules, patterns: [...(rules.patterns || []), ...rule] };
}, {} as ESLintComplexOption);

if ("patterns" in objectOption && objectOption.patterns.length > 0) {
notices.push(NOTICE_MATCH_PATTERNS);
}

ruleArguments = [objectOption];
}

return {
rules: [
{
ruleArguments,
...(notices.length > 0 && { notices }),
ruleName: "no-restricted-imports",
},
],
};
};

function isString(value: string): boolean {
return typeof value === "string";
}
74 changes: 74 additions & 0 deletions src/rules/converters/tests/import-blacklist.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { convertImportBlacklist } from "../import-blacklist";

describe(convertImportBlacklist, () => {
test.each([
[[], []],
[["rxjs"], ["rxjs"]],
[
["rxjs", "lodash"],
["rxjs", "lodash"],
],
[
[".*\\.temp$", ".*\\.tmp$"],
[".*\\.temp$", ".*\\.tmp$"],
],
[
["moment", "date-fns", ["eslint/*"]],
[{ patterns: ["eslint/*"], paths: ["moment", "date-fns"] }],
],
[
[{ lodash: ["pullAll", "pull"] }],
[
{
paths: [
{
name: "lodash",
importNames: ["pullAll", "pull"],
},
],
},
],
],
[
[
"rxjs",
[".*\\.temp$", ".*\\.tmp$"],
{ lodash: ["pullAll", "pull"] },
{ dummy: ["default"] },
],
[
{
paths: [
"rxjs",
{
name: "lodash",
importNames: ["pullAll", "pull"],
},
{
name: "dummy",
importNames: ["default"],
},
],
patterns: [".*\\.temp$", ".*\\.tmp$"],
},
],
],
] as any[][])("convert %j", (ruleArguments: any[], expected: any[]) => {
const result = convertImportBlacklist({ ruleArguments });
const hasPatterns = typeof expected[0] === "object" && "patterns" in expected[0];

expect(result).toEqual({
rules: [
{
ruleArguments: expected,
...(hasPatterns && {
notices: [
"ESLint and TSLint use different strategies to match patterns. TSLint uses standard regular expressions, but ESLint .gitignore spec.",
],
}),
ruleName: "no-restricted-imports",
},
],
});
});
});
3 changes: 2 additions & 1 deletion src/rules/rulesConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { convertEofline } from "./converters/eofline";
import { convertFileNameCasing } from "./converters/file-name-casing";
import { convertForin } from "./converters/forin";
import { convertFunctionConstructor } from "./converters/function-constructor";
import { convertImportBlacklist } from "./converters/import-blacklist";
import { convertIncrementDecrement } from "./converters/increment-decrement";
import { convertIndent } from "./converters/indent";
import { convertInterfaceName } from "./converters/interface-name";
Expand Down Expand Up @@ -155,6 +156,7 @@ export const rulesConverters = new Map([
["file-name-casing", convertFileNameCasing],
["forin", convertForin],
["function-constructor", convertFunctionConstructor],
["import-blacklist", convertImportBlacklist],
["increment-decrement", convertIncrementDecrement],
["indent", convertIndent],
["interface-name", convertInterfaceName],
Expand Down Expand Up @@ -275,7 +277,6 @@ export const rulesConverters = new Map([

// TSLint core rules:
// ["ban", convertBan], // no-restricted-properties
// ["import-blacklist", convertImportBlacklist], // no-restricted-imports

// tslint-microsoft-contrib rules:
// ["max-func-body-length", convertMaxFuncBodyLength],
Expand Down
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export type RemoveErrors<Items> = {

export type PromiseValue<T> = T extends Promise<infer R> ? R : never;

export type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> &
{
[K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>;
}[Keys];

export const uniqueFromSources = <T>(...sources: (T | T[] | undefined)[]) => {
const items: T[] = [];

Expand Down