From f740536c0b7cbbf83253815f999b6c725ac66ad2 Mon Sep 17 00:00:00 2001 From: krulod Date: Thu, 12 Sep 2024 15:29:05 +0300 Subject: [PATCH] refactor(eslint-plugin): address issues --- packages/eslint-plugin/README.md | 6 ++-- .../src/rules/unit-naming-rule.test.ts | 5 ++++ .../src/rules/unit-naming-rule.ts | 2 +- packages/eslint-plugin/src/shared.ts | 29 ------------------- 4 files changed, 9 insertions(+), 33 deletions(-) diff --git a/packages/eslint-plugin/README.md b/packages/eslint-plugin/README.md index bd00a762..c8e94530 100644 --- a/packages/eslint-plugin/README.md +++ b/packages/eslint-plugin/README.md @@ -60,7 +60,7 @@ Here is an example of React + TypeScript + Prettier config with Reatom. "prettier/prettier": "error" }, "settings": { - "atomSuffix": "Atom" + "atomPostfix": "Atom" } } ``` @@ -93,7 +93,7 @@ When creating units within `reatom*`-named factory functions, you can also speci ```ts const reatomFood = (config: { name: string; calories: number; fat: number; carbs: number; protein: number }) => { - const { name } = config.name + const { name } = config const calories = atom(config.calories, `Food.calories`) const fat = atom(config.fat, `Food.fat`) const carbs = atom(config.carbs, `Food.carbs`) @@ -106,7 +106,7 @@ If a factory function defines a parameter or a variable named `name`, names of u ```ts const reatomFood = (config: { name: string; calories: number; fat: number; carbs: number; protein: number }) => { - const { name } = config.name + const { name } = config const calories = atom(config.calories, `${name}.calories`) const fat = atom(config.fat, `${name}.fat`) const carbs = atom(config.carbs, `${name}.carbs`) diff --git a/packages/eslint-plugin/src/rules/unit-naming-rule.test.ts b/packages/eslint-plugin/src/rules/unit-naming-rule.test.ts index 42c7bc90..08289792 100644 --- a/packages/eslint-plugin/src/rules/unit-naming-rule.test.ts +++ b/packages/eslint-plugin/src/rules/unit-naming-rule.test.ts @@ -88,5 +88,10 @@ tester.run('unit-naming-rule', unitNamingRule, { errors: [{ message: /domain must be derived from/ }], output: `function reatomSome({name}) { const field = atom(0, \`\${name}.field\`); }`, }, + { + code: `function reatomSome(config) { const {name} = config; const field = atom(0, 'Some.field'); }`, + errors: [{ message: /domain must be derived from/ }], + output: `function reatomSome(config) { const {name} = config; const field = atom(0, \`\${name}.field\`); }`, + }, ], }) diff --git a/packages/eslint-plugin/src/rules/unit-naming-rule.ts b/packages/eslint-plugin/src/rules/unit-naming-rule.ts index 5b5585db..d1dade44 100644 --- a/packages/eslint-plugin/src/rules/unit-naming-rule.ts +++ b/packages/eslint-plugin/src/rules/unit-naming-rule.ts @@ -75,7 +75,7 @@ export const unitNamingRule: Rule.RuleModule = { domainScopes.push({ is: 'static', name: node.id.name.replace('reatom', '') }) } else domainScopes.push(null) }, - [`:function:exit`](node: estree.Function) { + [`:function:exit`]() { domainScopes.pop() }, [`CallExpression[callee.name=${reatomFactoryPattern}]`](node: estree.CallExpression) { diff --git a/packages/eslint-plugin/src/shared.ts b/packages/eslint-plugin/src/shared.ts index 7df41cfc..06f721e2 100644 --- a/packages/eslint-plugin/src/shared.ts +++ b/packages/eslint-plugin/src/shared.ts @@ -3,35 +3,6 @@ import type * as estree from 'estree' export const reatomFactoryList = ['atom', 'action', 'reaction'] as const export const reatomFactoryPattern = new RegExp(`^(reatom\\w+|${reatomFactoryList.join('|')})$`) -// export const createImportMap = (packagePrefix: string) => { -// const imported = new Map() -// const local = new Map() - -// const onImportNode = (node: estree.ImportDeclaration) => { -// const source = node.source.value -// if (typeof source !== 'string' || !source.startsWith(packagePrefix)) { -// return -// } - -// for (const spec of node.specifiers) { -// if (spec.type === 'ImportSpecifier') { -// onImportSpec(spec) -// } -// } -// } - -// const onImportSpec = (spec: estree.ImportSpecifier) => { -// imported.set(spec.imported.name, spec.local.name) -// local.set(spec.local.name, spec.imported.name) -// } - -// return { -// onImportNode, -// imported: imported as ReadonlyMap, -// local: local as ReadonlyMap, -// } -// } - export const patternNames = (pattern: estree.Pattern): estree.Identifier[] => { if (pattern.type === 'AssignmentPattern') { return patternNames(pattern.left)