Skip to content
Draft
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
9 changes: 9 additions & 0 deletions .changeset/named-parameter-types.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions packages/eslint-config-basic/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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/
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-config-basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 6 additions & 1 deletion packages/eslint-config-react/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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}'],
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-plugin/index.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/* (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: {
name: '@frontify/eslint-plugin',
},
rules: {
'jsx-shorthand-boolean': jsxShorthandBoolean,
'no-inline-param-type': noInlineParamType,
},
};
4 changes: 3 additions & 1 deletion packages/eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
52 changes: 52 additions & 0 deletions packages/eslint-plugin/rules/no-inline-param-type.mjs
Original file line number Diff line number Diff line change
@@ -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 (`<Component>Props`, `<function>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;
98 changes: 98 additions & 0 deletions packages/eslint-plugin/rules/no-inline-param-type.test.mjs
Original file line number Diff line number Diff line change
@@ -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 }) => <span>{label}</span>;',
},
{
name: 'reports an array binding parameter',
code: 'function first([label]: { [Symbol.iterator](): Iterator<string> }) { 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],
},
],
});
4 changes: 3 additions & 1 deletion packages/oxlint-config-basic/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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: '^_' },
Expand Down
1 change: 1 addition & 0 deletions packages/oxlint-config-basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 0 additions & 5 deletions packages/oxlint-config-react/index.mjs
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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',
},
Expand Down
1 change: 0 additions & 1 deletion packages/oxlint-config-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
},
"dependencies": {
"@eslint-react/eslint-plugin": "^5.8.9",
"@frontify/eslint-plugin": "workspace:^",
"@frontify/oxlint-config-basic": "workspace:^"
},
"publishConfig": {
Expand Down
12 changes: 9 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.