Skip to content

Added quick utility script for making a new rule #1089

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 12 commits into from
Jun 1, 2021
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
},
"scripts": {
"eslint": "eslint \"./src/*.ts\" \"./src/**/*.ts\" --report-unused-disable-directives",
"new-converter": "node ./script/new-converter",
"prettier": "prettier \"./src/*.{js,json,ts,xml,yaml}\" \"./src/**/*.{js,json,ts,xml,yaml}\" --ignore-path .prettierignore",
"prettier:write": "npm run prettier -- --write",
"test": "jest",
Expand Down
63 changes: 63 additions & 0 deletions script/new-converter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const { Command } = require("commander");
const { promises: fs } = require("fs");
const { upperFirst, camelCase } = require("lodash");

(async () => {
const command = new Command()
.option("--eslint [eslint]", "name of the original ESLint rule")
.option("--tslint [tslint]", "name of the original TSLint rule");

const args = command.parse(process.argv).opts();

for (const arg of ["eslint", "tslint"]) {
if (!args[arg]) {
throw new Error(`Missing --${arg} option.`);
}
}

const tslintPascalCase = upperFirst(camelCase(args.tslint));
const plugins = args.eslint.includes("/")
? `
plugins: ["${args.eslint.split("/")[0]}"],`
: "";

await fs.writeFile(
`./src/converters/lintConfigs/rules/ruleConverters/${args.tslint}.ts`,
`
import { RuleConverter } from "../ruleConverter";

export const convert${tslintPascalCase}: RuleConverter = () => {
return {${plugins}
rules: [
{
ruleName: "${args.eslint}",
},
],
};
};
`.trimLeft(),
);

await fs.writeFile(
`./src/converters/lintConfigs/rules/ruleConverters/tests/${args.tslint}.test.ts`,
`
import { convert${tslintPascalCase} } from "../${args.tslint}";

describe(convert${tslintPascalCase}, () => {
test("conversion without arguments", () => {
const result = convert${tslintPascalCase}({
ruleArguments: [],
});

expect(result).toEqual({${plugins.replace("\n", "\n ")}
rules: [
{
ruleName: "${args.eslint}",
},
],
});
});
});
`.trimLeft(),
);
})();