Created
November 9, 2024 16:04
-
-
Save binjospookie/317b3b32bd9671d08f6c6db053e579af to your computer and use it in GitHub Desktop.
storeName.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ESLintUtils } from '@typescript-eslint/utils'; | |
const createRule = ESLintUtils.RuleCreator(() => ''); | |
export const storeNamingConvention = createRule({ | |
name: 'store-naming-convention', | |
meta: { | |
type: 'layout', | |
docs: { | |
description: 'Enforce $ as a prefix or postfix for any store created by Effector methods', | |
recommended: true, | |
}, | |
fixable: 'code', | |
schema: [], | |
}, | |
defaultOptions: [], | |
create(context) { | |
const parserServices = ESLintUtils.getParserServices(context); | |
return { | |
'VariableDeclarator[parent.kind="const"][init.type="CallExpression"]'(node) { | |
const varName = node.id.name; | |
if (!varName || varName.startsWith('$')) return; | |
const type = parserServices.getTypeAtLocation(node); | |
// Store includes StoreWritable too | |
if (type.symbol && type.symbol.escapedName.includes('Store')) { | |
context.report({ | |
node: node.id, | |
message: `Rename "${varName}" to $${varName}"`, | |
fix(fixer) { | |
return fixer.replaceText(node.id, `$${varName}`); | |
}, | |
}); | |
} | |
}, | |
}; | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment