-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix compatability with eslint-plugin-react 7.30.0
We reached into eslint-plugin-react to access some of its internals, and they moved around in v7.30.0. Copy the utilities that we depend upon into our codebase, and remove the need touch eslint-plugin-react's internals. Also remove usage of Component.detect as it is no longer needed.
- Loading branch information
Showing
9 changed files
with
361 additions
and
134 deletions.
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
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
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
101 changes: 101 additions & 0 deletions
101
packages/eslint-plugin/lib/utilities/component-utils.js
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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copied from eslint-plugin-react's lib/util/componentUtil.js | ||
// Because we don't want to reach deep into that packages's internals | ||
// https://github.com/jsx-eslint/eslint-plugin-react/blob/18de0a653122c4ffd586a0269a7355c15ef05e23/lib/util/componentUtil.js | ||
|
||
const doctrine = require('doctrine'); | ||
|
||
const pragmaUtil = require('./pragma'); | ||
|
||
/** | ||
* @template {(_: object) => any} T | ||
* @param {T} fn | ||
* @returns {T} | ||
*/ | ||
function memoize(fn) { | ||
const cache = new WeakMap(); | ||
// @ts-ignore | ||
return function memoizedFn(arg) { | ||
const cachedValue = cache.get(arg); | ||
if (cachedValue !== undefined) { | ||
return cachedValue; | ||
} | ||
const val = fn(arg); | ||
cache.set(arg, val); | ||
return val; | ||
}; | ||
} | ||
|
||
const getPragma = memoize(pragmaUtil.getFromContext); | ||
|
||
/** | ||
* Check if the node is explicitly declared as a descendant of a React Component | ||
* @param {any} node | ||
* @param {Context} context | ||
* @returns {boolean} | ||
*/ | ||
function isExplicitComponent(node, context) { | ||
const sourceCode = context.getSourceCode(); | ||
let comment; | ||
// Sometimes the passed node may not have been parsed yet by eslint, and this function call crashes. | ||
// Can be removed when eslint sets "parent" property for all nodes on initial AST traversal: https://github.com/eslint/eslint-scope/issues/27 | ||
// eslint-disable-next-line no-warning-comments | ||
// FIXME: Remove try/catch when https://github.com/eslint/eslint-scope/issues/27 is implemented. | ||
try { | ||
comment = sourceCode.getJSDocComment(node); | ||
} catch (err) { | ||
comment = null; | ||
} | ||
|
||
if (comment === null) { | ||
return false; | ||
} | ||
|
||
let commentAst; | ||
try { | ||
commentAst = doctrine.parse(comment.value, { | ||
unwrap: true, | ||
tags: ['extends', 'augments'], | ||
}); | ||
} catch (err) { | ||
// handle a bug in the archived `doctrine`, see #2596 | ||
return false; | ||
} | ||
|
||
const relevantTags = commentAst.tags.filter( | ||
(tag) => | ||
tag.name === 'React.Component' || tag.name === 'React.PureComponent', | ||
); | ||
|
||
return relevantTags.length > 0; | ||
} | ||
|
||
/** | ||
* @param {ASTNode} node | ||
* @param {Context} context | ||
* @returns {boolean} | ||
*/ | ||
function isES6Component(node, context) { | ||
const pragma = getPragma(context); | ||
if (isExplicitComponent(node, context)) { | ||
return true; | ||
} | ||
|
||
if (!node.superClass) { | ||
return false; | ||
} | ||
if (node.superClass.type === 'MemberExpression') { | ||
return ( | ||
node.superClass.object.name === pragma && | ||
/^(Pure)?Component$/.test(node.superClass.property.name) | ||
); | ||
} | ||
if (node.superClass.type === 'Identifier') { | ||
return /^(Pure)?Component$/.test(node.superClass.name); | ||
} | ||
return false; | ||
} | ||
|
||
module.exports = { | ||
isES6Component, | ||
isExplicitComponent, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copied from eslint-plugin-react's lib/util/pragma.js | ||
// Because we don't want to reach deep into that packages's internals | ||
// https://github.com/jsx-eslint/eslint-plugin-react/blob/18de0a653122c4ffd586a0269a7355c15ef05e23/lib/util/pragma.js | ||
|
||
/** | ||
* @fileoverview Utility functions for React pragma configuration | ||
* @author Yannick Croissant | ||
*/ | ||
|
||
const JSX_ANNOTATION_REGEX = /@jsx\s+([^\s]+)/; | ||
// Does not check for reserved keywords or unicode characters | ||
const JS_IDENTIFIER_REGEX = /^[_$a-zA-Z][_$a-zA-Z0-9]*$/; | ||
|
||
/** | ||
* @param {Context} context | ||
* @returns {string} | ||
*/ | ||
function getCreateClassFromContext(context) { | ||
let pragma = 'createReactClass'; | ||
// .eslintrc shared settings (https://eslint.org/docs/user-guide/configuring#adding-shared-settings) | ||
if (context.settings.react && context.settings.react.createClass) { | ||
pragma = context.settings.react.createClass; | ||
} | ||
if (!JS_IDENTIFIER_REGEX.test(pragma)) { | ||
throw new Error( | ||
`createClass pragma ${pragma} is not a valid function name`, | ||
); | ||
} | ||
return pragma; | ||
} | ||
|
||
/** | ||
* @param {Context} context | ||
* @returns {string} | ||
*/ | ||
function getFragmentFromContext(context) { | ||
let pragma = 'Fragment'; | ||
// .eslintrc shared settings (https://eslint.org/docs/user-guide/configuring#adding-shared-settings) | ||
if (context.settings.react && context.settings.react.fragment) { | ||
pragma = context.settings.react.fragment; | ||
} | ||
if (!JS_IDENTIFIER_REGEX.test(pragma)) { | ||
throw new Error(`Fragment pragma ${pragma} is not a valid identifier`); | ||
} | ||
return pragma; | ||
} | ||
|
||
/** | ||
* @param {Context} context | ||
* @returns {string} | ||
*/ | ||
function getFromContext(context) { | ||
let pragma = 'React'; | ||
|
||
const sourceCode = context.getSourceCode(); | ||
const pragmaNode = sourceCode | ||
.getAllComments() | ||
.find((node) => JSX_ANNOTATION_REGEX.test(node.value)); | ||
|
||
if (pragmaNode) { | ||
const matches = JSX_ANNOTATION_REGEX.exec(pragmaNode.value); | ||
pragma = matches[1].split('.')[0]; | ||
// .eslintrc shared settings (https://eslint.org/docs/user-guide/configuring#adding-shared-settings) | ||
} else if (context.settings.react && context.settings.react.pragma) { | ||
pragma = context.settings.react.pragma; | ||
} | ||
|
||
if (!JS_IDENTIFIER_REGEX.test(pragma)) { | ||
throw new Error(`React pragma ${pragma} is not a valid identifier`); | ||
} | ||
return pragma; | ||
} | ||
|
||
module.exports = { | ||
getCreateClassFromContext, | ||
getFragmentFromContext, | ||
getFromContext, | ||
}; |
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
Oops, something went wrong.