Skip to content
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

Add converter for typedef property #1370

Merged
merged 5 commits into from
Feb 28, 2022
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
2 changes: 2 additions & 0 deletions src/converters/lintConfigs/rules/ruleConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ import { convertTemplateUseTrackByFunction } from "./ruleConverters/template-use
import { convertTrailingComma } from "./ruleConverters/trailing-comma";
import { convertTripleEquals } from "./ruleConverters/triple-equals";
import { convertTypeLiteralDelimiter } from "./ruleConverters/type-literal-delimiter";
import { convertTypedef } from "./ruleConverters/typedef";
import { convertTypedefWhitespace } from "./ruleConverters/typedef-whitespace";
import { convertTypeofCompare } from "./ruleConverters/typeof-compare";
import { convertUnderscoreConsistentInvocation } from "./ruleConverters/underscore-consistent-invocation";
Expand Down Expand Up @@ -669,6 +670,7 @@ export const ruleConverters = new Map([
["trailing-comma", convertTrailingComma],
["triple-equals", convertTripleEquals],
["type-literal-delimiter", convertTypeLiteralDelimiter],
["typedef", convertTypedef],
["typedef-whitespace", convertTypedefWhitespace],
["typeof-compare", convertTypeofCompare],
["underscore-consistent-invocation", convertUnderscoreConsistentInvocation],
Expand Down
177 changes: 177 additions & 0 deletions src/converters/lintConfigs/rules/ruleConverters/tests/typedef.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import { describe, expect, test } from "@jest/globals";

import { convertTypedef } from "../typedef";

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

expect(result).toEqual({
rules: [
{
ruleName: "@typescript-eslint/typedef",
},
{
ruleName: "@typescript-eslint/explicit-function-return-type",
},
{
ruleName: "@typescript-eslint/explicit-module-boundary-types",
},
],
});
});

test("conversion with a few arguments", () => {
const result = convertTypedef({
ruleArguments: [
"parameter",
"variable-declaration-ignore-function",
"array-destructuring",
],
});

expect(result).toEqual({
rules: [
{
ruleArguments: [
{
parameter: true,
variableDeclarationIgnoreFunction: true,
arrayDestructuring: true,
},
],
ruleName: "@typescript-eslint/typedef",
},
{
ruleName: "@typescript-eslint/explicit-function-return-type",
},
{
ruleName: "@typescript-eslint/explicit-module-boundary-types",
},
],
});
});

test("conversion with all arguments", () => {
const result = convertTypedef({
ruleArguments: [
"parameter",
"arrow-parameter",
"property-declaration",
"variable-declaration",
"variable-declaration-ignore-function",
"member-variable-declaration",
"object-destructuring",
"array-destructuring",
],
});

expect(result).toEqual({
rules: [
{
ruleArguments: [
{
parameter: true,
arrowParameter: true,
propertyDeclaration: true,
variableDeclaration: true,
variableDeclarationIgnoreFunction: true,
memberVariableDeclaration: true,
objectDestructuring: true,
arrayDestructuring: true,
},
],
ruleName: "@typescript-eslint/typedef",
},
{
ruleName: "@typescript-eslint/explicit-function-return-type",
},
{
ruleName: "@typescript-eslint/explicit-module-boundary-types",
},
],
});
});

test("conversion with call-signature", () => {
const result = convertTypedef({
ruleArguments: ["call-signature"],
});

expect(result).toEqual({
notices: [
"ESLint does not differentiate between the call signatures of arrow and non-arrow functions. Both will be checked",
],
rules: [
{
ruleName: "@typescript-eslint/typedef",
},
{
ruleArguments: [
{
allowExpressions: false,
allowTypedFunctionExpressions: false,
allowHigherOrderFunctions: false,
allowDirectConstAssertionInArrowFunctions: true,
allowConciseArrowFunctionExpressionsStartingWithVoid: true,
},
],
ruleName: "@typescript-eslint/explicit-function-return-type",
},
{
ruleArguments: [
{
allowArgumentsExplicitlyTypedAsAny: true,
allowDirectConstAssertionInArrowFunctions: true,
allowHigherOrderFunctions: false,
allowTypedFunctionExpressions: false,
},
],
ruleName: "@typescript-eslint/explicit-module-boundary-types",
},
],
});
});

test("conversion with arrow-call-signature", () => {
const result = convertTypedef({
ruleArguments: ["arrow-call-signature"],
});

expect(result).toEqual({
notices: [
"ESLint does not differentiate between the call signatures of arrow and non-arrow functions. Both will be checked",
],
rules: [
{
ruleName: "@typescript-eslint/typedef",
},
{
ruleArguments: [
{
allowExpressions: false,
allowTypedFunctionExpressions: false,
allowHigherOrderFunctions: false,
allowDirectConstAssertionInArrowFunctions: true,
allowConciseArrowFunctionExpressionsStartingWithVoid: true,
},
],
ruleName: "@typescript-eslint/explicit-function-return-type",
},
{
ruleArguments: [
{
allowArgumentsExplicitlyTypedAsAny: true,
allowDirectConstAssertionInArrowFunctions: true,
allowHigherOrderFunctions: false,
allowTypedFunctionExpressions: false,
},
],
ruleName: "@typescript-eslint/explicit-module-boundary-types",
},
],
});
});
});
67 changes: 67 additions & 0 deletions src/converters/lintConfigs/rules/ruleConverters/typedef.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { RuleConverter } from "../ruleConverter";

export const convertTypedef: RuleConverter = (tslintRule) => {
const typedefRule: Record<string, boolean> = {};
const functionReturnRule: Record<string, boolean> = {};
const moduleBoundaryRule: Record<string, boolean> = {};

const originalArguments = new Set(tslintRule.ruleArguments);

const argumentEquivalents = {
parameter: "parameter",
"arrow-parameter": "arrowParameter",
"property-declaration": "propertyDeclaration",
"variable-declaration": "variableDeclaration",
"variable-declaration-ignore-function": "variableDeclarationIgnoreFunction",
"member-variable-declaration": "memberVariableDeclaration",
"object-destructuring": "objectDestructuring",
"array-destructuring": "arrayDestructuring",
};

for (const [tslintArgument, eslintArgument] of Object.entries(argumentEquivalents)) {
if (originalArguments.has(tslintArgument)) typedefRule[eslintArgument] = true;
}

const checksFunctionCallSignture =
originalArguments.has("arrow-call-signature") || originalArguments.has("call-signature");
if (checksFunctionCallSignture) {
functionReturnRule.allowExpressions = false;
functionReturnRule.allowTypedFunctionExpressions = false;
functionReturnRule.allowHigherOrderFunctions = false;
functionReturnRule.allowDirectConstAssertionInArrowFunctions = true;
functionReturnRule.allowConciseArrowFunctionExpressionsStartingWithVoid = true;

moduleBoundaryRule.allowArgumentsExplicitlyTypedAsAny = true;
moduleBoundaryRule.allowDirectConstAssertionInArrowFunctions = true;
moduleBoundaryRule.allowHigherOrderFunctions = false;
moduleBoundaryRule.allowTypedFunctionExpressions = false;
}

return {
...(checksFunctionCallSignture && {
notices: [
"ESLint does not differentiate between the call signatures of arrow and non-arrow functions. Both will be checked",
],
}),
rules: [
{
...(Object.keys(typedefRule).length !== 0 && {
ruleArguments: [typedefRule],
}),
ruleName: "@typescript-eslint/typedef",
},
{
...(Object.keys(functionReturnRule).length !== 0 && {
ruleArguments: [functionReturnRule],
}),
ruleName: "@typescript-eslint/explicit-function-return-type",
},
{
...(Object.keys(moduleBoundaryRule).length !== 0 && {
ruleArguments: [moduleBoundaryRule],
}),
ruleName: "@typescript-eslint/explicit-module-boundary-types",
},
],
};
};