Skip to content

Commit

Permalink
Merge branch 'master' into remove-xregexp
Browse files Browse the repository at this point in the history
  • Loading branch information
yacinehmito committed May 9, 2020
2 parents 4f038ed + ab28224 commit 49bbe88
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 24 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -194,6 +194,7 @@ Enable the rules that you would like to use.

## Other useful plugins

- Rules of Hooks: [eslint-plugin-react-hooks](https://github.com/facebook/react/tree/master/packages/eslint-plugin-react-hooks)
- JSX accessibility: [eslint-plugin-jsx-a11y](https://github.com/evcohen/eslint-plugin-jsx-a11y)
- React Native: [eslint-plugin-react-native](https://github.com/Intellicode/eslint-plugin-react-native)

Expand Down
4 changes: 2 additions & 2 deletions lib/rules/jsx-curly-spacing.js
Expand Up @@ -244,7 +244,7 @@ module.exports = {

// Take comments into consideration to narrow the fix range to what is actually affected. (See #1414)
if (nextComment.length > 0) {
return fixByTrimmingWhitespace(fixer, token.range[1], Math.min(nextToken.range[0], nextComment[0].start), 'start');
return fixByTrimmingWhitespace(fixer, token.range[1], Math.min(nextToken.range[0], nextComment[0].range[0]), 'start');
}

return fixByTrimmingWhitespace(fixer, token.range[1], nextToken.range[0], 'start');
Expand Down Expand Up @@ -279,7 +279,7 @@ module.exports = {

// Take comments into consideration to narrow the fix range to what is actually affected. (See #1414)
if (previousComment.length > 0) {
return fixByTrimmingWhitespace(fixer, Math.max(previousToken.range[1], previousComment[0].end), token.range[0], 'end');
return fixByTrimmingWhitespace(fixer, Math.max(previousToken.range[1], previousComment[0].range[1]), token.range[0], 'end');
}

return fixByTrimmingWhitespace(fixer, previousToken.range[1], token.range[0], 'end');
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/jsx-no-bind.js
Expand Up @@ -132,13 +132,13 @@ module.exports = {

function findVariableViolation(node, name) {
getBlockStatementAncestors(node).find(
block => reportVariableViolation(node, name, block.start)
block => reportVariableViolation(node, name, block.range[0])
);
}

return {
BlockStatement(node) {
setBlockVariableNameSet(node.start);
setBlockVariableNameSet(node.range[0]);
},

VariableDeclarator(node) {
Expand All @@ -154,7 +154,7 @@ module.exports = {
node.parent.kind === 'const' // only support const right now
) {
addVariableNameToSet(
variableViolationType, node.id.name, blockAncestors[0].start
variableViolationType, node.id.name, blockAncestors[0].range[0]
);
}
},
Expand Down
16 changes: 9 additions & 7 deletions lib/rules/jsx-pascal-case.js
Expand Up @@ -93,22 +93,24 @@ module.exports = {

return {
JSXOpeningElement(node) {
const isCompatTag = jsxUtil.isDOMComponent(node);
if (isCompatTag) return undefined;

let name = elementType(node);
if (name.length === 1) return undefined;

// Get namespace if the type is JSXNamespacedName or JSXMemberExpression
if (name.indexOf(':') > -1) {
name = name.substring(0, name.indexOf(':'));
} else if (name.indexOf('.') > -1) {
name = name.substring(0, name.indexOf('.'));
// Get JSXIdentifier if the type is JSXNamespacedName or JSXMemberExpression
if (name.lastIndexOf(':') > -1) {
name = name.substring(name.lastIndexOf(':') + 1);
} else if (name.lastIndexOf('.') > -1) {
name = name.substring(name.lastIndexOf('.') + 1);
}

const isPascalCase = testPascalCase(name);
const isCompatTag = jsxUtil.isDOMComponent(node);
const isAllowedAllCaps = allowAllCaps && testAllCaps(name);
const isIgnored = ignore.indexOf(name) !== -1;

if (!isPascalCase && !isCompatTag && !isAllowedAllCaps && !isIgnored) {
if (!isPascalCase && !isAllowedAllCaps && !isIgnored) {
let message = `Imported JSX component ${name} must be in PascalCase`;

if (allowAllCaps) {
Expand Down
16 changes: 5 additions & 11 deletions lib/util/jsx.js
Expand Up @@ -6,23 +6,17 @@

const elementType = require('jsx-ast-utils/elementType');

const COMPAT_TAG_REGEX = /^[a-z]|-/;
// See https://github.com/babel/babel/blob/ce420ba51c68591e057696ef43e028f41c6e04cd/packages/babel-types/src/validators/react/isCompatTag.js
// for why we only test for the first character
const COMPAT_TAG_REGEX = /^[a-z]/;

/**
* Checks if a node represents a DOM element.
* Checks if a node represents a DOM element according to React.
* @param {object} node - JSXOpeningElement to check.
* @returns {boolean} Whether or not the node corresponds to a DOM element.
*/
function isDOMComponent(node) {
let name = elementType(node);

// Get namespace if the type is JSXNamespacedName or JSXMemberExpression
if (name.indexOf(':') > -1) {
name = name.slice(0, name.indexOf(':'));
} else if (name.indexOf('.') > -1) {
name = name.slice(0, name.indexOf('.'));
}

const name = elementType(node);
return COMPAT_TAG_REGEX.test(name);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/util/usedPropTypes.js
Expand Up @@ -170,7 +170,7 @@ function isPropArgumentInSetStateUpdater(context, name) {
unwrappedParentCalleeNode.property &&
unwrappedParentCalleeNode.property.name === 'setState' &&
// Make sure we are in the updater not the callback
scope.block.parent.arguments[0].start === scope.block.start &&
scope.block.parent.arguments[0].range[0] === scope.block.range[0] &&
scope.block.parent.arguments[0].params &&
scope.block.parent.arguments[0].params.length > 1
) {
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/jsx-pascal-case.js
Expand Up @@ -29,6 +29,10 @@ const parserOptions = {
const ruleTester = new RuleTester({parserOptions});
ruleTester.run('jsx-pascal-case', rule, {
valid: [{
// The rule must not warn on components that start with a lowercase
// because they are interpreted as HTML elements by React
code: '<testcomponent />'
}, {
code: '<testComponent />'
}, {
code: '<test_component />'
Expand All @@ -52,6 +56,8 @@ ruleTester.run('jsx-pascal-case', rule, {
code: '<Año />'
}, {
code: '<Søknad />'
}, {
code: '<T />'
}, {
code: '<T />',
parser: parsers.BABEL_ESLINT
Expand Down

0 comments on commit 49bbe88

Please sign in to comment.