-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(eslint-plugin): [no-var-requires, no-require-imports] allow option #7710
Changes from all commits
cc3c317
1fca381
3f67961
449beae
b77f61d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,11 @@ import { getScope } from '@typescript-eslint/utils/eslint-utils'; | |
|
||
import { createRule } from '../util'; | ||
|
||
type Options = []; | ||
type Options = [ | ||
{ | ||
allow: string[]; | ||
}, | ||
]; | ||
type MessageIds = 'noVarReqs'; | ||
|
||
export default createRule<Options, MessageIds>({ | ||
|
@@ -18,14 +22,39 @@ export default createRule<Options, MessageIds>({ | |
messages: { | ||
noVarReqs: 'Require statement not part of import statement.', | ||
}, | ||
schema: [], | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
allow: { | ||
type: 'array', | ||
items: { type: 'string' }, | ||
description: 'Patterns of import paths to allow requiring from.', | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
defaultOptions: [{ allow: [] }], | ||
create(context, options) { | ||
const allowPatterns = options[0].allow.map( | ||
pattern => new RegExp(pattern, 'u'), | ||
); | ||
function isImportPathAllowed(importPath: string): boolean { | ||
return allowPatterns.some(pattern => importPath.match(pattern)); | ||
} | ||
return { | ||
'CallExpression[callee.name="require"]'( | ||
node: TSESTree.CallExpression, | ||
): void { | ||
if ( | ||
node.arguments[0]?.type === AST_NODE_TYPES.Literal && | ||
typeof node.arguments[0].value === 'string' && | ||
isImportPathAllowed(node.arguments[0].value) | ||
Comment on lines
+52
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Bug] I think this should use I did a quick search for Thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #8186. I wanted to get this PR merged 😄 and I don't think template literals in |
||
) { | ||
return; | ||
} | ||
const parent = | ||
node.parent.type === AST_NODE_TYPES.ChainExpression | ||
? node.parent.parent | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does it work with string constants?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, that needs to make the rule type-aware, or otherwise implement a go-to-definition mechanism, which I'm not sure we have the precedent. I'm also not sure if it happens more than a handful of times IRL.