Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Refactor] create/extract
isCreateElement
and `isDestructuredFromPr…
…agmaImport` utils This should improve detection in the following rules: - `button-has-type` - `forbid-elements` - `no-adjacent-inline-elements` - `no-children-prop` - `style-prop-object`
- Loading branch information
Showing
9 changed files
with
131 additions
and
121 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
const pragmaUtil = require('./pragma'); | ||
const isDestructuredFromPragmaImport = require('./isDestructuredFromPragmaImport'); | ||
|
||
/** | ||
* Checks if the node is a createElement call | ||
* @param {ASTNode} node - The AST node being checked. | ||
* @param {Context} context - The AST node being checked. | ||
* @returns {Boolean} - True if node is a createElement call object literal, False if not. | ||
*/ | ||
module.exports = function isCreateElement(node, context) { | ||
const pragma = pragmaUtil.getFromContext(context); | ||
if ( | ||
node.callee | ||
&& node.callee.type === 'MemberExpression' | ||
&& node.callee.property.name === 'createElement' | ||
&& node.callee.object | ||
&& node.callee.object.name === pragma | ||
) { | ||
return true; | ||
} | ||
|
||
if ( | ||
node | ||
&& node.callee | ||
&& node.callee.name === 'createElement' | ||
&& isDestructuredFromPragmaImport('createElement', context) | ||
) { | ||
return true; | ||
} | ||
|
||
return false; | ||
}; |
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,79 @@ | ||
'use strict'; | ||
|
||
const pragmaUtil = require('./pragma'); | ||
const variableUtil = require('./variable'); | ||
|
||
/** | ||
* Check if variable is destructured from pragma import | ||
* | ||
* @param {string} variable The variable name to check | ||
* @param {Context} context eslint context | ||
* @returns {Boolean} True if createElement is destructured from the pragma | ||
*/ | ||
module.exports = function isDestructuredFromPragmaImport(variable, context) { | ||
const pragma = pragmaUtil.getFromContext(context); | ||
const variables = variableUtil.variablesInScope(context); | ||
const variableInScope = variableUtil.getVariable(variables, variable); | ||
if (variableInScope) { | ||
const latestDef = variableUtil.getLatestVariableDefinition(variableInScope); | ||
if (latestDef) { | ||
// check if latest definition is a variable declaration: 'variable = value' | ||
if (latestDef.node.type === 'VariableDeclarator' && latestDef.node.init) { | ||
// check for: 'variable = pragma.variable' | ||
if ( | ||
latestDef.node.init.type === 'MemberExpression' | ||
&& latestDef.node.init.object.type === 'Identifier' | ||
&& latestDef.node.init.object.name === pragma | ||
) { | ||
return true; | ||
} | ||
// check for: '{variable} = pragma' | ||
if ( | ||
latestDef.node.init.type === 'Identifier' | ||
&& latestDef.node.init.name === pragma | ||
) { | ||
return true; | ||
} | ||
|
||
// "require('react')" | ||
let requireExpression = null; | ||
|
||
// get "require('react')" from: "{variable} = require('react')" | ||
if (latestDef.node.init.type === 'CallExpression') { | ||
requireExpression = latestDef.node.init; | ||
} | ||
// get "require('react')" from: "variable = require('react').variable" | ||
if ( | ||
!requireExpression | ||
&& latestDef.node.init.type === 'MemberExpression' | ||
&& latestDef.node.init.object.type === 'CallExpression' | ||
) { | ||
requireExpression = latestDef.node.init.object; | ||
} | ||
|
||
// check proper require. | ||
if ( | ||
requireExpression | ||
&& requireExpression.callee | ||
&& requireExpression.callee.name === 'require' | ||
&& requireExpression.arguments[0] | ||
&& requireExpression.arguments[0].value === pragma.toLocaleLowerCase() | ||
) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
// latest definition is an import declaration: import {<variable>} from 'react' | ||
if ( | ||
latestDef.parent | ||
&& latestDef.parent.type === 'ImportDeclaration' | ||
&& latestDef.parent.source.value === pragma.toLocaleLowerCase() | ||
) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
}; |