-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[Refactor] improve performance of rule merging #3281
Merged
+44
−32
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -247,6 +247,39 @@ function getWrapperFunctions(context, pragma) { | |
]); | ||
} | ||
|
||
// eslint-disable-next-line valid-jsdoc | ||
/** | ||
* Merge many eslint rules into one | ||
* @param {{[_: string]: Function}[]} rules the returned values for eslint rule.create(context) | ||
* @returns {{[_: string]: Function}} merged rule | ||
*/ | ||
function mergeRules(rules) { | ||
/** @type {Map<string, Function[]>} */ | ||
const handlersByKey = new Map(); | ||
rules.forEach((rule) => { | ||
Object.keys(rule).forEach((key) => { | ||
const fns = handlersByKey.get(key); | ||
if (!fns) { | ||
handlersByKey.set(key, [rule[key]]); | ||
} else { | ||
fns.push(rule[key]); | ||
} | ||
}); | ||
}); | ||
|
||
/** @type {{[key: string]: Function}} */ | ||
const rule = {}; | ||
handlersByKey.forEach((fns, key) => { | ||
rule[key] = function mergedHandler(node) { | ||
fns.forEach((fn) => { | ||
fn(node); | ||
}); | ||
}; | ||
}); | ||
|
||
return rule; | ||
} | ||
|
||
function componentRule(rule, context) { | ||
const pragma = pragmaUtil.getFromContext(context); | ||
const sourceCode = context.getSourceCode(); | ||
|
@@ -859,44 +892,21 @@ function componentRule(rule, context) { | |
}, | ||
}; | ||
|
||
// Update the provided rule instructions to add the component detection | ||
const ruleInstructions = rule(context, components, utils); | ||
const updatedRuleInstructions = Object.assign({}, ruleInstructions); | ||
const propTypesInstructions = propTypesUtil(context, components, utils); | ||
const usedPropTypesInstructions = usedPropTypesUtil(context, components, utils); | ||
const defaultPropsInstructions = defaultPropsUtil(context, components, utils); | ||
const allKeys = new Set(Object.keys(detectionInstructions).concat( | ||
Object.keys(propTypesInstructions), | ||
Object.keys(usedPropTypesInstructions), | ||
Object.keys(defaultPropsInstructions), | ||
Object.keys(reactImportInstructions) | ||
)); | ||
|
||
allKeys.forEach((instruction) => { | ||
updatedRuleInstructions[instruction] = (node) => { | ||
if (instruction in detectionInstructions) { | ||
detectionInstructions[instruction](node); | ||
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. This property access performed for each AST node turned out to be performance bottleneck. |
||
} | ||
if (instruction in propTypesInstructions) { | ||
propTypesInstructions[instruction](node); | ||
} | ||
if (instruction in usedPropTypesInstructions) { | ||
usedPropTypesInstructions[instruction](node); | ||
} | ||
if (instruction in defaultPropsInstructions) { | ||
defaultPropsInstructions[instruction](node); | ||
} | ||
if (instruction in reactImportInstructions) { | ||
reactImportInstructions[instruction](node); | ||
} | ||
if (ruleInstructions[instruction]) { | ||
return ruleInstructions[instruction](node); | ||
} | ||
}; | ||
}); | ||
const mergedRule = mergeRules([ | ||
detectionInstructions, | ||
propTypesInstructions, | ||
usedPropTypesInstructions, | ||
defaultPropsInstructions, | ||
reactImportInstructions, | ||
ruleInstructions, | ||
]); | ||
|
||
// Return the updated rule instructions | ||
return updatedRuleInstructions; | ||
return mergedRule; | ||
} | ||
|
||
module.exports = Object.assign(Components, { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
is there no way to have this rule pass with this function's signature?
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.
That rule has been deprecated for a long time and it lacks support for lots of typescript syntaxes.
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.
gotcha; so we'd have to use a similar rule from the typescript plugin?
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.
You are looking for eslint-plugin-jsdoc .
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.
typescript plugin does not handle jsdoc as far as I know