Valeriy Kuzmin
Moonfare, Berlin, 2022
Plan
Problem
Solution
But...
What is a custom eslint rule?
Rule
Walker
Meta
docs + messages
Loader
eslint-local-rules.js
Loader
eslint-local-rules.js
eslint-plugin-local-rules
Node type => analyzer
Loader
eslint-local-rules.js
Tests
valid cases
invalid cases
Welcome back to AST
Problem
Non-v3
V3
TOKEN_V2
Impl v2
TOKEN_V3
Impl v3
Examples (from tests)
// ok
bind<DocumentAccessValidatorInterface>(PROFESSIONALISATION_DOCUMENT_ACCESS_VALIDATOR_INTERNAL)
.to(ProfessionalisationDocumentAccessValidatorImpl)
// ok
container.bind<DocumentAccessValidatorInterface>(PROFESSIONALISATION_DOCUMENT_ACCESS_VALIDATOR_INTERNAL)
.to(ProfessionalisationDocumentAccessValidatorImpl)
// not ok
bind<AuthorizationService>(AUTHORIZATION_SERVICE).to(AuthorizationService)
context.report({
node,
messageId: 'noSmuggling',
data: {
token: bindArgument,
implementation: toArgument,
},
});
Idea
1. Analyze the whole codebase first with TS
2. Save data to files
3. Synchronously read in the rule
What we want from the helper
Validation
Unique?
Yes
Import path doesn't matter
No
v2 binds to v3 = error
v3 token and v2 class?
Yes
No or unclear
assume no error
Drawbacks
1. Reexports are not followed - you can fool the rule
2. Impls are only properly checked if all non-unique are in v2