UNPKG

tslint-to-eslint-config

Version:

Converts your TSLint configuration to the closest reasonable ESLint equivalent.

106 lines 4.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertVariableName = exports.ForbiddenLeadingTrailingIdentifierMsg = exports.IgnoreLeadingTrailingIdentifierMsg = exports.ConstRequiredForAllCapsMsg = void 0; exports.ConstRequiredForAllCapsMsg = "typescript-eslint does not enforce uppercase for const only."; exports.IgnoreLeadingTrailingIdentifierMsg = "Leading and trailing underscores (_) on identifiers will now be ignored."; exports.ForbiddenLeadingTrailingIdentifierMsg = "Leading or trailing underscores (_) on identifiers will now be forbidden."; const convertVariableName = (tslintRule) => { const hasCheckFormat = tslintRule.ruleArguments.includes("check-format"); const allowedLeadingUnderscore = tslintRule.ruleArguments.includes("allow-leading-underscore"); const allowedTrailingUnderscore = tslintRule.ruleArguments.includes("allow-trailing-underscore"); const constRequiredForAllCaps = tslintRule.ruleArguments.includes("require-const-for-all-caps"); const allowPascalCase = tslintRule.ruleArguments.includes("allow-pascal-case"); const allowSnakeCase = tslintRule.ruleArguments.includes("allow-snake-case"); /** * disallows the use of certain TypeScript keywords as variable or parameter names. * @see https://palantir.github.io/tslint/rules/variable-name/ * @see https://github.com/palantir/tslint/blob/285fc1db18d1fd24680d6a2282c6445abf1566ee/src/rules/variableNameRule.ts#L26-L36 */ const banKeywords = [ "any", "Number", "number", "String", "string", "Boolean", "boolean", "Undefined", "undefined", ]; const getCamelCaseRuleOptions = () => { const camelCaseRules = []; const camelCaseOptionNotices = []; const formats = ["camelCase", "UPPER_CASE"]; if (hasCheckFormat && allowPascalCase) { formats.push("PascalCase"); } if (hasCheckFormat && allowSnakeCase) { formats.push("snake_case"); } if (!hasCheckFormat) { camelCaseRules.push({ selector: "variable", format: formats, leadingUnderscore: "forbid", trailingUnderscore: "forbid", }); } else { camelCaseRules.push({ selector: "variable", format: formats, leadingUnderscore: allowedLeadingUnderscore ? "allow" : "forbid", trailingUnderscore: allowedTrailingUnderscore ? "allow" : "forbid", }); } if (hasCheckFormat && constRequiredForAllCaps) { camelCaseOptionNotices.push(exports.ConstRequiredForAllCapsMsg); } return { ...(camelCaseOptionNotices.length !== 0 && { notices: camelCaseOptionNotices }), ...(camelCaseRules.length !== 0 && { ruleArguments: camelCaseRules }), ruleName: "@typescript-eslint/naming-convention", }; }; const getUnderscoreDangleRuleOptions = () => { let underscoreDangleOptionSeverity; const underscoreDangleOptionNotice = []; if (hasCheckFormat && (allowedLeadingUnderscore || allowedTrailingUnderscore)) { underscoreDangleOptionSeverity = "off"; underscoreDangleOptionNotice.push(exports.IgnoreLeadingTrailingIdentifierMsg); } else { underscoreDangleOptionNotice.push(exports.ForbiddenLeadingTrailingIdentifierMsg); } return { notices: underscoreDangleOptionNotice, ...(underscoreDangleOptionSeverity !== undefined && { ruleSeverity: underscoreDangleOptionSeverity, }), ruleName: "no-underscore-dangle", }; }; const getDenyListRuleOptions = () => { const denyListOptionArguments = tslintRule.ruleArguments.includes("ban-keywords") ? banKeywords : []; return { ...(denyListOptionArguments.length !== 0 && { ruleArguments: denyListOptionArguments, }), ruleName: "id-denylist", }; }; return { rules: [ getCamelCaseRuleOptions(), getUnderscoreDangleRuleOptions(), getDenyListRuleOptions(), { ruleName: "id-match", }, ], }; }; exports.convertVariableName = convertVariableName; //# sourceMappingURL=variable-name.js.map