Skip to content

Commit

Permalink
[Refactor] context comes first
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed May 16, 2024
1 parent 90068a4 commit 0c804bb
Show file tree
Hide file tree
Showing 22 changed files with 46 additions and 36 deletions.
2 changes: 1 addition & 1 deletion lib/rules/button-has-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ module.exports = {
checkValue(node, propValue);
},
CallExpression(node) {
if (!isCreateElement(node, context) || node.arguments.length < 1) {
if (!isCreateElement(context, node) || node.arguments.length < 1) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/checked-requires-onchange-or-readonly.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ module.exports = {
checkAttributesAndReport(node, propSet);
},
CallExpression(node) {
if (!isCreateElement(node, context)) {
if (!isCreateElement(context, node)) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/forbid-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ module.exports = {
},

CallExpression(node) {
if (!isCreateElement(node, context)) {
if (!isCreateElement(context, node)) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/iframe-missing-sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ module.exports = {
},

CallExpression(node) {
if (isCreateElement(node, context) && node.arguments && node.arguments.length > 0) {
if (isCreateElement(context, node) && node.arguments && node.arguments.length > 0) {
const tag = node.arguments[0];
if (tag.type === 'Literal' && tag.value === 'iframe') {
checkProps(context, node);
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/jsx-indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ module.exports = {
}
if (
!fn
|| !jsxUtil.isReturningJSX(node, context, true)
|| !jsxUtil.isReturningJSX(context, node, true)
) {
return;
}
Expand Down
11 changes: 8 additions & 3 deletions lib/rules/jsx-no-comment-textnodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ const messages = {
putCommentInBraces: 'Comments inside children section of tag should be placed inside braces',
};

function checkText(node, context) {
/**
* @param {Context} context
* @param {ASTNode} node
* @returns {void}
*/
function checkText(context, node) {
// since babel-eslint has the wrong node.raw, we'll get the source text
const rawValue = getSourceCode(context).getText(node);
if (/^\s*\/(\/|\*)/m.test(rawValue)) {
Expand Down Expand Up @@ -56,10 +61,10 @@ module.exports = {

return {
Literal(node) {
checkText(node, context);
checkText(context, node);
},
JSXText(node) {
checkText(node, context);
checkText(context, node);
},
};
},
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/jsx-sort-default-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ module.exports = {
*/
function checkSorted(declarations) {
// function fix(fixer) {
// return propTypesSortUtil.fixPropTypesSort(fixer, context, declarations, ignoreCase);
// return propTypesSortUtil.fixPropTypesSort(context, fixer, declarations, ignoreCase);
// }

declarations.reduce((prev, curr, idx, decls) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-adjacent-inline-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ module.exports = {
validate(node, node.children);
},
CallExpression(node) {
if (!isCreateElement(node, context)) {
if (!isCreateElement(context, node)) {
return;
}
if (node.arguments.length < 2 || !node.arguments[2]) {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-children-prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const report = require('../util/report');
* object literal, False if not.
*/
function isCreateElementWithProps(node, context) {
return isCreateElement(node, context)
return isCreateElement(context, node)
&& node.arguments.length > 1
&& node.arguments[1].type === 'ObjectExpression';
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = {
create(context) {
return {
CallExpression(node) {
if (isCreateElement(node, context) && node.arguments.length > 0 && node.arguments[0].type === 'Literal') {
if (isCreateElement(context, node) && node.arguments.length > 0 && node.arguments[0].type === 'Literal') {
const name = node.arguments[0].value;
if (typeof name !== 'string' || name.indexOf(':') === -1) return undefined;
report(context, messages.noNamespace, 'noNamespace', {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-unstable-nested-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function isCreateElementMatcher(node, context) {
return (
node
&& node.type === 'CallExpression'
&& isCreateElement(node, context)
&& isCreateElement(context, node)
);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/sort-default-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ module.exports = {
*/
function checkSorted(declarations) {
// function fix(fixer) {
// return propTypesSortUtil.fixPropTypesSort(fixer, context, declarations, ignoreCase);
// return propTypesSortUtil.fixPropTypesSort(context, fixer, declarations, ignoreCase);
// }

declarations.reduce((prev, curr, idx, decls) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/sort-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ module.exports = {

function fix(fixer) {
return propTypesSortUtil.fixPropTypesSort(
fixer,
context,
fixer,
declarations,
ignoreCase,
requiredFirst,
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/style-prop-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ module.exports = {
return {
CallExpression(node) {
if (
isCreateElement(node, context)
isCreateElement(context, node)
&& node.arguments.length > 1
) {
if (node.arguments[0].name) {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/void-dom-elements-no-children.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ module.exports = {
return;
}

if (!isCreateElement(node, context)) {
if (!isCreateElement(context, node)) {
return;
}

Expand Down
11 changes: 8 additions & 3 deletions lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,20 @@ function componentRule(rule, context) {
* @returns {Boolean} True if createElement is destructured from the pragma
*/
isDestructuredFromPragmaImport(variable) {
return isDestructuredFromPragmaImport(variable, context);
return isDestructuredFromPragmaImport(context, variable);
},

/**
* @param {ASTNode} ASTNode
* @param {boolean=} strict
* @returns {boolean}
*/
isReturningJSX(ASTNode, strict) {
return jsxUtil.isReturningJSX(ASTNode, context, strict, true);
return jsxUtil.isReturningJSX(context, ASTNode, strict, true);
},

isReturningJSXOrNull(ASTNode, strict) {
return jsxUtil.isReturningJSX(ASTNode, context, strict);
return jsxUtil.isReturningJSX(context, ASTNode, strict);
},

isReturningOnlyNull(ASTNode) {
Expand Down
8 changes: 4 additions & 4 deletions lib/util/isCreateElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ 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.
* @param {ASTNode} node - 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) {
module.exports = function isCreateElement(context, node) {
if (
node.callee
&& node.callee.type === 'MemberExpression'
Expand All @@ -24,7 +24,7 @@ module.exports = function isCreateElement(node, context) {
node
&& node.callee
&& node.callee.name === 'createElement'
&& isDestructuredFromPragmaImport('createElement', context)
&& isDestructuredFromPragmaImport(context, 'createElement')
) {
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/util/isDestructuredFromPragmaImport.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ 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
* @param {string} variable The variable name to check
* @returns {boolean} True if createElement is destructured from the pragma
*/
module.exports = function isDestructuredFromPragmaImport(variable, context) {
module.exports = function isDestructuredFromPragmaImport(context, variable) {
const pragma = pragmaUtil.getFromContext(context);
const variables = variableUtil.variablesInScope(context);
const variableInScope = variableUtil.getVariable(variables, variable);
Expand Down
6 changes: 3 additions & 3 deletions lib/util/jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ function isWhiteSpaces(value) {
/**
* Check if the node is returning JSX or null
*
* @param {ASTNode} ASTnode The AST node being checked
* @param {Context} context The context of `ASTNode`.
* @param {ASTNode} ASTnode The AST node being checked
* @param {Boolean} [strict] If true, in a ternary condition the node must return JSX in both cases
* @param {Boolean} [ignoreNull] If true, null return values will be ignored
* @returns {Boolean} True if the node is returning JSX or null, false if not
*/
function isReturningJSX(ASTnode, context, strict, ignoreNull) {
function isReturningJSX(context, ASTnode, strict, ignoreNull) {
const isJSXValue = (node) => {
if (!node) {
return false;
Expand All @@ -114,7 +114,7 @@ function isReturningJSX(ASTnode, context, strict, ignoreNull) {
case 'JSXFragment':
return true;
case 'CallExpression':
return isCreateElement(node, context);
return isCreateElement(context, node);
case 'Literal':
if (!ignoreNull && node.value === null) {
return true;
Expand Down
4 changes: 2 additions & 2 deletions lib/util/propTypesSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ const commentnodeMap = new WeakMap(); // all nodes reference WeakMap for start a
/**
* Fixes sort order of prop types.
*
* @param {Context} context the second element to compare.
* @param {Fixer} fixer the first element to compare.
* @param {Object} context the second element to compare.
* @param {Array} declarations The context of the two nodes.
* @param {Boolean=} ignoreCase whether or not to ignore case when comparing the two elements.
* @param {Boolean=} requiredFirst whether or not to sort required elements first.
Expand All @@ -126,8 +126,8 @@ const commentnodeMap = new WeakMap(); // all nodes reference WeakMap for start a
* @returns {Object|*|{range, text}} the sort order of the two elements.
*/
function fixPropTypesSort(
fixer,
context,
fixer,
declarations,
ignoreCase,
requiredFirst,
Expand Down
6 changes: 3 additions & 3 deletions lib/util/usedPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,13 @@ function isPropTypesUsageByMemberExpression(node, context, utils, checkAsyncSafe

/**
* Retrieve the name of a property node
* @param {ASTNode} node The AST node with the property.
* @param {Context} context
* @param {ASTNode} node The AST node with the property.
* @param {Object} utils
* @param {boolean} checkAsyncSafeLifeCycles
* @return {string|undefined} the name of the property or undefined if not found
*/
function getPropertyName(node, context, utils, checkAsyncSafeLifeCycles) {
function getPropertyName(context, node, utils, checkAsyncSafeLifeCycles) {
const property = node.property;
if (property) {
switch (property.type) {
Expand Down Expand Up @@ -312,7 +312,7 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
switch (node.type) {
case 'OptionalMemberExpression':
case 'MemberExpression':
name = getPropertyName(node, context, utils, checkAsyncSafeLifeCycles);
name = getPropertyName(context, node, utils, checkAsyncSafeLifeCycles);
if (name) {
allNames = parentNames.concat(name);
if (
Expand Down
2 changes: 1 addition & 1 deletion tests/util/jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const mockContext = {
describe('jsxUtil', () => {
describe('isReturningJSX', () => {
const assertValid = (codeStr) => assert(
isReturningJSX(parseCode(codeStr), mockContext)
isReturningJSX(mockContext, parseCode(codeStr))
);

it('Works when returning JSX', () => {
Expand Down

0 comments on commit 0c804bb

Please sign in to comment.