From 61ef7694659396c399a41e80591a83160e874e41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Costa?= Date: Wed, 25 Nov 2026 16:22:12 +0100 Subject: [PATCH] feat(lint): require named object types on function parameters --- .changeset/named-parameter-types.md | 9 ++ .../eslint-config-basic/eslint.config.mjs | 4 + packages/eslint-config-basic/package.json | 1 + .../eslint-config-react/eslint.config.mjs | 7 +- packages/eslint-plugin/index.mjs | 2 + packages/eslint-plugin/package.json | 4 +- .../rules/no-inline-param-type.mjs | 52 ++++++++++ .../rules/no-inline-param-type.test.mjs | 98 +++++++++++++++++++ packages/oxlint-config-basic/index.mjs | 4 +- packages/oxlint-config-basic/package.json | 1 + packages/oxlint-config-react/index.mjs | 5 - packages/oxlint-config-react/package.json | 1 - pnpm-lock.yaml | 12 ++- 13 files changed, 188 insertions(+), 12 deletions(-) create mode 100644 .changeset/named-parameter-types.md create mode 100644 packages/eslint-plugin/rules/no-inline-param-type.mjs create mode 100644 packages/eslint-plugin/rules/no-inline-param-type.test.mjs diff --git a/.changeset/named-parameter-types.md b/.changeset/named-parameter-types.md new file mode 100644 index 00000000..7313c6ff --- /dev/null +++ b/.changeset/named-parameter-types.md @@ -0,0 +1,9 @@ +--- +'@frontify/eslint-config-basic': minor +'@frontify/eslint-config-react': minor +'@frontify/eslint-plugin': patch +'@frontify/oxlint-config-basic': minor +'@frontify/oxlint-config-react': minor +--- + +ESLint and Oxlint now report inline object types on TypeScript function parameters as errors. Declare a named type at module level and use it in the parameter annotation. diff --git a/packages/eslint-config-basic/eslint.config.mjs b/packages/eslint-config-basic/eslint.config.mjs index 8a7e4f29..ed4a8df6 100644 --- a/packages/eslint-config-basic/eslint.config.mjs +++ b/packages/eslint-config-basic/eslint.config.mjs @@ -5,6 +5,7 @@ import eslint from '@eslint/js'; import eslintPluginMarkdown from '@eslint/markdown'; import eslintPluginComments from '@eslint-community/eslint-plugin-eslint-comments/configs'; +import frontifyPlugin from '@frontify/eslint-plugin'; import eslintPluginStylistic from '@stylistic/eslint-plugin'; import { defineConfig } from 'eslint/config'; import { createTypeScriptImportResolver } from 'eslint-import-resolver-typescript'; @@ -339,7 +340,10 @@ export default defineConfig( { // TypeScript files files: ['**/*.ts', '**/*.tsx', '**/*.mts', '**/*.cts'], + plugins: { '@frontify': frontifyPlugin }, rules: { + '@frontify/no-inline-param-type': 'error', + // As we're using @typescript-eslint it's recommended to turn off // the standard eslint rule for unused variables and use the @typescript-eslint rule instead. // See here: https://typescript-eslint.io/rules/no-unused-vars/ diff --git a/packages/eslint-config-basic/package.json b/packages/eslint-config-basic/package.json index 7ae9e23f..6977e75f 100644 --- a/packages/eslint-config-basic/package.json +++ b/packages/eslint-config-basic/package.json @@ -28,6 +28,7 @@ "@eslint-community/eslint-plugin-eslint-comments": "^4.7.2", "@eslint/js": "^10.0.1", "@eslint/markdown": "^8.0.2", + "@frontify/eslint-plugin": "workspace:^", "@stylistic/eslint-plugin": "^5.10.0", "@typescript-eslint/eslint-plugin": "^8.60.0", "@typescript-eslint/parser": "^8.60.0", diff --git a/packages/eslint-config-react/eslint.config.mjs b/packages/eslint-config-react/eslint.config.mjs index c668cb9e..f0721d89 100644 --- a/packages/eslint-config-react/eslint.config.mjs +++ b/packages/eslint-config-react/eslint.config.mjs @@ -12,7 +12,12 @@ export default defineConfig( eslintPluginReact.configs['recommended-type-checked'], { // Keep existing rule overrides and suppressions valid. - plugins: { '@eslint-react/kit': frontifyPlugin }, + plugins: { + '@eslint-react/kit': { + meta: frontifyPlugin.meta, + rules: { 'jsx-shorthand-boolean': frontifyPlugin.rules['jsx-shorthand-boolean'] }, + }, + }, }, { files: ['**/*.{js,mjs,cjs,jsx,ts,tsx,mts,cts}'], diff --git a/packages/eslint-plugin/index.mjs b/packages/eslint-plugin/index.mjs index e34f3da8..975fb992 100644 --- a/packages/eslint-plugin/index.mjs +++ b/packages/eslint-plugin/index.mjs @@ -1,6 +1,7 @@ /* (c) Copyright Frontify Ltd., all rights reserved. */ import jsxShorthandBoolean from './rules/jsx-shorthand-boolean.mjs'; +import noInlineParamType from './rules/no-inline-param-type.mjs'; export default { meta: { @@ -8,5 +9,6 @@ export default { }, rules: { 'jsx-shorthand-boolean': jsxShorthandBoolean, + 'no-inline-param-type': noInlineParamType, }, }; diff --git a/packages/eslint-plugin/package.json b/packages/eslint-plugin/package.json index 6118364f..e6eb4cce 100644 --- a/packages/eslint-plugin/package.json +++ b/packages/eslint-plugin/package.json @@ -15,12 +15,14 @@ }, "files": [ "index.mjs", - "rules/jsx-shorthand-boolean.mjs" + "rules/jsx-shorthand-boolean.mjs", + "rules/no-inline-param-type.mjs" ], "scripts": { "test": "node --test 'rules/**/*.test.mjs'" }, "devDependencies": { + "@typescript-eslint/parser": "^8.60.0", "eslint": "^10.4.1" }, "publishConfig": { diff --git a/packages/eslint-plugin/rules/no-inline-param-type.mjs b/packages/eslint-plugin/rules/no-inline-param-type.mjs new file mode 100644 index 00000000..74b1355c --- /dev/null +++ b/packages/eslint-plugin/rules/no-inline-param-type.mjs @@ -0,0 +1,52 @@ +/* (c) Copyright Frontify Ltd., all rights reserved. */ + +// @ts-check + +const FUNCTION_NODES = [ + 'FunctionDeclaration', + 'FunctionExpression', + 'ArrowFunctionExpression', + 'TSDeclareFunction', + 'TSEmptyBodyFunctionExpression', + 'TSFunctionType', + 'TSConstructorType', + 'TSMethodSignature', + 'TSCallSignatureDeclaration', + 'TSConstructSignatureDeclaration', +]; +const FUNCTION_SIGNATURE = `:matches(${FUNCTION_NODES.join(', ')})`; +const TYPED_BINDING = ':matches(Identifier, ObjectPattern, ArrayPattern, RestElement)'; +const INLINE_OBJECT_TYPE = 'TSTypeAnnotation.typeAnnotation > TSTypeLiteral.typeAnnotation'; +const INLINE_PARAMETER_TYPE = [ + `${TYPED_BINDING}.params`, + `AssignmentPattern.params > ${TYPED_BINDING}.left`, + `TSParameterProperty.params > ${TYPED_BINDING}.parameter`, + `TSParameterProperty.params > AssignmentPattern.parameter > ${TYPED_BINDING}.left`, +] + .map((binding) => `${FUNCTION_SIGNATURE} > ${binding} > ${INLINE_OBJECT_TYPE}`) + .join(', '); +const NAMED_TYPE_MESSAGE_ID = 'namedType'; + +/** @satisfies {import('eslint').Rule.RuleModule} */ +const noInlineParamType = { + meta: { + type: 'problem', + schema: [], + messages: { + [NAMED_TYPE_MESSAGE_ID]: 'Declare a named type at module level (`Props`, `Options`).', + }, + }, + create(context) { + return { + /** + * Suggests a named type for the parameter. + * @param {import('eslint').JSSyntaxElement} node - The inline object type written on the parameter. + */ + [INLINE_PARAMETER_TYPE](node) { + context.report({ node, messageId: NAMED_TYPE_MESSAGE_ID }); + }, + }; + }, +}; + +export default noInlineParamType; diff --git a/packages/eslint-plugin/rules/no-inline-param-type.test.mjs b/packages/eslint-plugin/rules/no-inline-param-type.test.mjs new file mode 100644 index 00000000..ef31c1f4 --- /dev/null +++ b/packages/eslint-plugin/rules/no-inline-param-type.test.mjs @@ -0,0 +1,98 @@ +/* (c) Copyright Frontify Ltd., all rights reserved. */ + +import * as parser from '@typescript-eslint/parser'; +import { RuleTester } from 'eslint'; + +import plugin from '../index.mjs'; + +const rule = plugin.rules['no-inline-param-type']; +const ruleTester = new RuleTester({ languageOptions: { parser } }); +const inlineObjectTypeError = { messageId: 'namedType' }; + +const inlineParameterTypes = [ + { + name: 'reports a function expression parameter', + code: 'const read = function (options: { label: string }) { return options.label; };', + }, + { + name: 'reports destructured component props', + filename: 'example.tsx', + code: 'const Badge = ({ label }: { label: string }) => {label};', + }, + { + name: 'reports an array binding parameter', + code: 'function first([label]: { [Symbol.iterator](): Iterator }) { return label; }', + }, + { + name: 'reports a defaulted destructured parameter', + code: 'function read({ label }: { label: string } = { label: "x" }) { return label; }', + }, + { + name: 'reports a rest parameter', + code: 'function read(...options: { [index: number]: string }) { return options[0]; }', + }, + { + name: 'reports a constructor parameter property', + code: 'class Reader { constructor(private options: { label: string }) {} }', + }, + { + name: 'reports a defaulted constructor parameter property', + code: 'class Reader { constructor(private options: { label: string } = { label: "x" }) {} }', + }, + { + name: 'reports an ambient function parameter', + code: 'declare function read(options: { label: string }): string;', + }, + { + name: 'reports a method overload parameter', + code: 'class Reader { read(options: { label: string }): string; read(options: unknown) { return ""; } }', + }, + { + name: 'reports a function type parameter', + code: 'type Reader = (options: { label: string }) => string;', + }, + { + name: 'reports a constructor type parameter', + code: 'type Reader = new (options: { label: string }) => object;', + }, + { + name: 'reports an interface method parameter', + code: 'interface Reader { read(options: { label: string }): string; }', + }, + { + name: 'reports a call signature parameter', + code: 'interface Reader { (options: { label: string }): string; }', + }, + { + name: 'reports a construct signature parameter', + code: 'interface Reader { new (options: { label: string }): object; }', + }, +]; + +ruleTester.run('no-inline-param-type', rule, { + valid: [ + 'type Options = { label: string }; function read(options: Options) { return options.label; }', + 'type Options = { label: string }; const read = ({ label }: Options = { label: "x" }) => label;', + 'type Options = { label: string }; class Reader { constructor(private options: Options) {} }', + 'const read = (options = { label: "x" }) => options.label;', + 'function read(label: string): { label: string } { return { label }; }', + 'function read() { const options: { label: string } = { label: "x" }; return options; }', + 'function read(options: { label: string }[]) { return options[0]; }', + 'function read(options: { label: string } | undefined) { return options; }', + 'function read(options: { label: string } & { enabled: boolean }) { return options; }', + 'function read(options: Readonly<{ label: string }>) { return options; }', + ], + invalid: [ + { + name: 'underlines only the inline type on a function declaration parameter', + code: 'function read(options: { label: string }) { return options.label; }', + errors: [{ ...inlineObjectTypeError, line: 1, column: 24, endLine: 1, endColumn: 41 }], + }, + ...inlineParameterTypes.map((testCase) => ({ ...testCase, errors: [inlineObjectTypeError] })), + { + name: 'reports each inline object type in the parameter list', + code: 'function read(first: { label: string }, second: { active: boolean }) {}', + errors: [inlineObjectTypeError, inlineObjectTypeError], + }, + ], +}); diff --git a/packages/oxlint-config-basic/index.mjs b/packages/oxlint-config-basic/index.mjs index d9ec20e3..c4fcd64c 100644 --- a/packages/oxlint-config-basic/index.mjs +++ b/packages/oxlint-config-basic/index.mjs @@ -14,7 +14,8 @@ export default defineConfig({ plugins: ['typescript', 'eslint', 'promise', 'unicorn', 'import', 'jsdoc'], jsPlugins: [ '@eslint-community/eslint-plugin-eslint-comments', - // Resolve from this package so consumers can use isolated installs. + // Resolve these dependencies here so consumers can use isolated installs. + { name: '@frontify', specifier: fileURLToPath(import.meta.resolve('@frontify/eslint-plugin')) }, { name: 'jsdoc-js', specifier: fileURLToPath(import.meta.resolve('eslint-plugin-jsdoc')) }, ], categories: { @@ -575,6 +576,7 @@ export default defineConfig({ { files: ['**/*.ts', '**/*.tsx', '**/*.mts', '**/*.cts'], rules: { + '@frontify/no-inline-param-type': 'error', 'no-unused-vars': [ 'error', { argsIgnorePattern: '^_', varsIgnorePattern: '^_' }, diff --git a/packages/oxlint-config-basic/package.json b/packages/oxlint-config-basic/package.json index 284816c4..bf75f5a4 100644 --- a/packages/oxlint-config-basic/package.json +++ b/packages/oxlint-config-basic/package.json @@ -22,6 +22,7 @@ }, "dependencies": { "@eslint-community/eslint-plugin-eslint-comments": "^4.7.2", + "@frontify/eslint-plugin": "workspace:^", "@stylistic/eslint-plugin": "^5.10.0", "eslint-plugin-jsdoc": "^65.4.5", "eslint-plugin-jsonc": "^3.2.0", diff --git a/packages/oxlint-config-react/index.mjs b/packages/oxlint-config-react/index.mjs index c94d1176..0ec66296 100644 --- a/packages/oxlint-config-react/index.mjs +++ b/packages/oxlint-config-react/index.mjs @@ -1,7 +1,5 @@ /* (c) Copyright Frontify Ltd., all rights reserved. */ -import { fileURLToPath } from 'node:url'; - import basicConfig from '@frontify/oxlint-config-basic'; import { defineConfig } from 'oxlint'; @@ -156,9 +154,6 @@ export default defineConfig({ overrides: [ { files: ['**/*.{js,jsx,cjs,mjs,ts,tsx,cts,mts}'], - jsPlugins: [ - { name: '@frontify', specifier: fileURLToPath(import.meta.resolve('@frontify/eslint-plugin')) }, - ], rules: { '@frontify/jsx-shorthand-boolean': 'error', }, diff --git a/packages/oxlint-config-react/package.json b/packages/oxlint-config-react/package.json index 2483b741..0fe27f53 100644 --- a/packages/oxlint-config-react/package.json +++ b/packages/oxlint-config-react/package.json @@ -22,7 +22,6 @@ }, "dependencies": { "@eslint-react/eslint-plugin": "^5.8.9", - "@frontify/eslint-plugin": "workspace:^", "@frontify/oxlint-config-basic": "workspace:^" }, "publishConfig": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ad1e7bc1..3089edc6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -41,6 +41,9 @@ importers: '@eslint/markdown': specifier: ^8.0.2 version: 8.0.2 + '@frontify/eslint-plugin': + specifier: workspace:^ + version: link:../eslint-plugin '@stylistic/eslint-plugin': specifier: ^5.10.0 version: 5.10.0(eslint@10.4.1(jiti@2.7.0)) @@ -136,6 +139,9 @@ importers: packages/eslint-plugin: devDependencies: + '@typescript-eslint/parser': + specifier: ^8.60.0 + version: 8.60.0(eslint@10.4.1(jiti@2.7.0))(typescript@6.0.3) eslint: specifier: ^10.4.1 version: 10.4.1(jiti@2.7.0) @@ -151,6 +157,9 @@ importers: '@eslint-community/eslint-plugin-eslint-comments': specifier: ^4.7.2 version: 4.7.2(eslint@10.4.1(jiti@2.7.0)) + '@frontify/eslint-plugin': + specifier: workspace:^ + version: link:../eslint-plugin '@stylistic/eslint-plugin': specifier: ^5.10.0 version: 5.10.0(eslint@10.4.1(jiti@2.7.0)) @@ -175,9 +184,6 @@ importers: '@eslint-react/eslint-plugin': specifier: ^5.8.9 version: 5.8.9(eslint@10.4.1(jiti@2.7.0))(typescript@6.0.3) - '@frontify/eslint-plugin': - specifier: workspace:^ - version: link:../eslint-plugin '@frontify/oxlint-config-basic': specifier: workspace:^ version: link:../oxlint-config-basic