Skip to content

Commit 0c804bb

Browse files
committed
[Refactor] context comes first
1 parent 90068a4 commit 0c804bb

22 files changed

+46
-36
lines changed

lib/rules/button-has-type.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ module.exports = {
135135
checkValue(node, propValue);
136136
},
137137
CallExpression(node) {
138-
if (!isCreateElement(node, context) || node.arguments.length < 1) {
138+
if (!isCreateElement(context, node) || node.arguments.length < 1) {
139139
return;
140140
}
141141

lib/rules/checked-requires-onchange-or-readonly.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ module.exports = {
115115
checkAttributesAndReport(node, propSet);
116116
},
117117
CallExpression(node) {
118-
if (!isCreateElement(node, context)) {
118+
if (!isCreateElement(context, node)) {
119119
return;
120120
}
121121

lib/rules/forbid-elements.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ module.exports = {
9595
},
9696

9797
CallExpression(node) {
98-
if (!isCreateElement(node, context)) {
98+
if (!isCreateElement(context, node)) {
9999
return;
100100
}
101101

lib/rules/iframe-missing-sandbox.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ module.exports = {
131131
},
132132

133133
CallExpression(node) {
134-
if (isCreateElement(node, context) && node.arguments && node.arguments.length > 0) {
134+
if (isCreateElement(context, node) && node.arguments && node.arguments.length > 0) {
135135
const tag = node.arguments[0];
136136
if (tag.type === 'Literal' && tag.value === 'iframe') {
137137
checkProps(context, node);

lib/rules/jsx-indent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ module.exports = {
425425
}
426426
if (
427427
!fn
428-
|| !jsxUtil.isReturningJSX(node, context, true)
428+
|| !jsxUtil.isReturningJSX(context, node, true)
429429
) {
430430
return;
431431
}

lib/rules/jsx-no-comment-textnodes.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ const messages = {
1717
putCommentInBraces: 'Comments inside children section of tag should be placed inside braces',
1818
};
1919

20-
function checkText(node, context) {
20+
/**
21+
* @param {Context} context
22+
* @param {ASTNode} node
23+
* @returns {void}
24+
*/
25+
function checkText(context, node) {
2126
// since babel-eslint has the wrong node.raw, we'll get the source text
2227
const rawValue = getSourceCode(context).getText(node);
2328
if (/^\s*\/(\/|\*)/m.test(rawValue)) {
@@ -56,10 +61,10 @@ module.exports = {
5661

5762
return {
5863
Literal(node) {
59-
checkText(node, context);
64+
checkText(context, node);
6065
},
6166
JSXText(node) {
62-
checkText(node, context);
67+
checkText(context, node);
6368
},
6469
};
6570
},

lib/rules/jsx-sort-default-props.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ module.exports = {
112112
*/
113113
function checkSorted(declarations) {
114114
// function fix(fixer) {
115-
// return propTypesSortUtil.fixPropTypesSort(fixer, context, declarations, ignoreCase);
115+
// return propTypesSortUtil.fixPropTypesSort(context, fixer, declarations, ignoreCase);
116116
// }
117117

118118
declarations.reduce((prev, curr, idx, decls) => {

lib/rules/no-adjacent-inline-elements.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ module.exports = {
111111
validate(node, node.children);
112112
},
113113
CallExpression(node) {
114-
if (!isCreateElement(node, context)) {
114+
if (!isCreateElement(context, node)) {
115115
return;
116116
}
117117
if (node.arguments.length < 2 || !node.arguments[2]) {

lib/rules/no-children-prop.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const report = require('../util/report');
2121
* object literal, False if not.
2222
*/
2323
function isCreateElementWithProps(node, context) {
24-
return isCreateElement(node, context)
24+
return isCreateElement(context, node)
2525
&& node.arguments.length > 1
2626
&& node.arguments[1].type === 'ObjectExpression';
2727
}

lib/rules/no-namespace.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module.exports = {
3636
create(context) {
3737
return {
3838
CallExpression(node) {
39-
if (isCreateElement(node, context) && node.arguments.length > 0 && node.arguments[0].type === 'Literal') {
39+
if (isCreateElement(context, node) && node.arguments.length > 0 && node.arguments[0].type === 'Literal') {
4040
const name = node.arguments[0].value;
4141
if (typeof name !== 'string' || name.indexOf(':') === -1) return undefined;
4242
report(context, messages.noNamespace, 'noNamespace', {

lib/rules/no-unstable-nested-components.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function isCreateElementMatcher(node, context) {
6868
return (
6969
node
7070
&& node.type === 'CallExpression'
71-
&& isCreateElement(node, context)
71+
&& isCreateElement(context, node)
7272
);
7373
}
7474

lib/rules/sort-default-props.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ module.exports = {
107107
*/
108108
function checkSorted(declarations) {
109109
// function fix(fixer) {
110-
// return propTypesSortUtil.fixPropTypesSort(fixer, context, declarations, ignoreCase);
110+
// return propTypesSortUtil.fixPropTypesSort(context, fixer, declarations, ignoreCase);
111111
// }
112112

113113
declarations.reduce((prev, curr, idx, decls) => {

lib/rules/sort-prop-types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ module.exports = {
119119

120120
function fix(fixer) {
121121
return propTypesSortUtil.fixPropTypesSort(
122-
fixer,
123122
context,
123+
fixer,
124124
declarations,
125125
ignoreCase,
126126
requiredFirst,

lib/rules/style-prop-object.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ module.exports = {
7777
return {
7878
CallExpression(node) {
7979
if (
80-
isCreateElement(node, context)
80+
isCreateElement(context, node)
8181
&& node.arguments.length > 1
8282
) {
8383
if (node.arguments[0].name) {

lib/rules/void-dom-elements-no-children.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ module.exports = {
108108
return;
109109
}
110110

111-
if (!isCreateElement(node, context)) {
111+
if (!isCreateElement(context, node)) {
112112
return;
113113
}
114114

lib/util/Components.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,15 +298,20 @@ function componentRule(rule, context) {
298298
* @returns {Boolean} True if createElement is destructured from the pragma
299299
*/
300300
isDestructuredFromPragmaImport(variable) {
301-
return isDestructuredFromPragmaImport(variable, context);
301+
return isDestructuredFromPragmaImport(context, variable);
302302
},
303303

304+
/**
305+
* @param {ASTNode} ASTNode
306+
* @param {boolean=} strict
307+
* @returns {boolean}
308+
*/
304309
isReturningJSX(ASTNode, strict) {
305-
return jsxUtil.isReturningJSX(ASTNode, context, strict, true);
310+
return jsxUtil.isReturningJSX(context, ASTNode, strict, true);
306311
},
307312

308313
isReturningJSXOrNull(ASTNode, strict) {
309-
return jsxUtil.isReturningJSX(ASTNode, context, strict);
314+
return jsxUtil.isReturningJSX(context, ASTNode, strict);
310315
},
311316

312317
isReturningOnlyNull(ASTNode) {

lib/util/isCreateElement.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ const isDestructuredFromPragmaImport = require('./isDestructuredFromPragmaImport
55

66
/**
77
* Checks if the node is a createElement call
8-
* @param {ASTNode} node - The AST node being checked.
98
* @param {Context} context - The AST node being checked.
10-
* @returns {Boolean} - True if node is a createElement call object literal, False if not.
9+
* @param {ASTNode} node - The AST node being checked.
10+
* @returns {boolean} - True if node is a createElement call object literal, False if not.
1111
*/
12-
module.exports = function isCreateElement(node, context) {
12+
module.exports = function isCreateElement(context, node) {
1313
if (
1414
node.callee
1515
&& node.callee.type === 'MemberExpression'
@@ -24,7 +24,7 @@ module.exports = function isCreateElement(node, context) {
2424
node
2525
&& node.callee
2626
&& node.callee.name === 'createElement'
27-
&& isDestructuredFromPragmaImport('createElement', context)
27+
&& isDestructuredFromPragmaImport(context, 'createElement')
2828
) {
2929
return true;
3030
}

lib/util/isDestructuredFromPragmaImport.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ const variableUtil = require('./variable');
66
/**
77
* Check if variable is destructured from pragma import
88
*
9-
* @param {string} variable The variable name to check
109
* @param {Context} context eslint context
11-
* @returns {Boolean} True if createElement is destructured from the pragma
10+
* @param {string} variable The variable name to check
11+
* @returns {boolean} True if createElement is destructured from the pragma
1212
*/
13-
module.exports = function isDestructuredFromPragmaImport(variable, context) {
13+
module.exports = function isDestructuredFromPragmaImport(context, variable) {
1414
const pragma = pragmaUtil.getFromContext(context);
1515
const variables = variableUtil.variablesInScope(context);
1616
const variableInScope = variableUtil.getVariable(variables, variable);

lib/util/jsx.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ function isWhiteSpaces(value) {
8686
/**
8787
* Check if the node is returning JSX or null
8888
*
89-
* @param {ASTNode} ASTnode The AST node being checked
9089
* @param {Context} context The context of `ASTNode`.
90+
* @param {ASTNode} ASTnode The AST node being checked
9191
* @param {Boolean} [strict] If true, in a ternary condition the node must return JSX in both cases
9292
* @param {Boolean} [ignoreNull] If true, null return values will be ignored
9393
* @returns {Boolean} True if the node is returning JSX or null, false if not
9494
*/
95-
function isReturningJSX(ASTnode, context, strict, ignoreNull) {
95+
function isReturningJSX(context, ASTnode, strict, ignoreNull) {
9696
const isJSXValue = (node) => {
9797
if (!node) {
9898
return false;
@@ -114,7 +114,7 @@ function isReturningJSX(ASTnode, context, strict, ignoreNull) {
114114
case 'JSXFragment':
115115
return true;
116116
case 'CallExpression':
117-
return isCreateElement(node, context);
117+
return isCreateElement(context, node);
118118
case 'Literal':
119119
if (!ignoreNull && node.value === null) {
120120
return true;

lib/util/propTypesSort.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ const commentnodeMap = new WeakMap(); // all nodes reference WeakMap for start a
115115
/**
116116
* Fixes sort order of prop types.
117117
*
118+
* @param {Context} context the second element to compare.
118119
* @param {Fixer} fixer the first element to compare.
119-
* @param {Object} context the second element to compare.
120120
* @param {Array} declarations The context of the two nodes.
121121
* @param {Boolean=} ignoreCase whether or not to ignore case when comparing the two elements.
122122
* @param {Boolean=} requiredFirst whether or not to sort required elements first.
@@ -126,8 +126,8 @@ const commentnodeMap = new WeakMap(); // all nodes reference WeakMap for start a
126126
* @returns {Object|*|{range, text}} the sort order of the two elements.
127127
*/
128128
function fixPropTypesSort(
129-
fixer,
130129
context,
130+
fixer,
131131
declarations,
132132
ignoreCase,
133133
requiredFirst,

lib/util/usedPropTypes.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,13 @@ function isPropTypesUsageByMemberExpression(node, context, utils, checkAsyncSafe
253253

254254
/**
255255
* Retrieve the name of a property node
256-
* @param {ASTNode} node The AST node with the property.
257256
* @param {Context} context
257+
* @param {ASTNode} node The AST node with the property.
258258
* @param {Object} utils
259259
* @param {boolean} checkAsyncSafeLifeCycles
260260
* @return {string|undefined} the name of the property or undefined if not found
261261
*/
262-
function getPropertyName(node, context, utils, checkAsyncSafeLifeCycles) {
262+
function getPropertyName(context, node, utils, checkAsyncSafeLifeCycles) {
263263
const property = node.property;
264264
if (property) {
265265
switch (property.type) {
@@ -312,7 +312,7 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
312312
switch (node.type) {
313313
case 'OptionalMemberExpression':
314314
case 'MemberExpression':
315-
name = getPropertyName(node, context, utils, checkAsyncSafeLifeCycles);
315+
name = getPropertyName(context, node, utils, checkAsyncSafeLifeCycles);
316316
if (name) {
317317
allNames = parentNames.concat(name);
318318
if (

tests/util/jsx.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const mockContext = {
3535
describe('jsxUtil', () => {
3636
describe('isReturningJSX', () => {
3737
const assertValid = (codeStr) => assert(
38-
isReturningJSX(parseCode(codeStr), mockContext)
38+
isReturningJSX(mockContext, parseCode(codeStr))
3939
);
4040

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

0 commit comments

Comments
 (0)