diff --git a/.eslintrc b/.eslintrc index 03c2f178f9..b3af93dd31 100644 --- a/.eslintrc +++ b/.eslintrc @@ -30,9 +30,11 @@ "prefer-object-spread": 0, // until node 8 is required "prefer-rest-params": 0, // until node 6 is required "prefer-spread": 0, // until node 6 is required + "function-call-argument-newline": 1, // TODO: enable "function-paren-newline": 0, "no-plusplus": 1, "no-param-reassign": 1, + "no-unreachable-loop": 1, // TODO: enable "no-restricted-syntax": [2, { "selector": "ObjectPattern", "message": "Object destructuring is not compatible with Node v4" diff --git a/lib/rules/jsx-equals-spacing.js b/lib/rules/jsx-equals-spacing.js index 0ec17d06b4..41d123152e 100644 --- a/lib/rules/jsx-equals-spacing.js +++ b/lib/rules/jsx-equals-spacing.js @@ -37,7 +37,7 @@ module.exports = { }, create(context) { - const config = context.options[0]; + const config = context.options[0] || 'never'; /** * Determines a given attribute node has an equal sign. @@ -64,48 +64,44 @@ module.exports = { const spacedBefore = sourceCode.isSpaceBetweenTokens(attrNode.name, equalToken); const spacedAfter = sourceCode.isSpaceBetweenTokens(equalToken, attrNode.value); - switch (config) { - default: - case 'never': - if (spacedBefore) { - report(context, messages.noSpaceBefore, 'noSpaceBefore', { - node: attrNode, - loc: equalToken.loc.start, - fix(fixer) { - return fixer.removeRange([attrNode.name.range[1], equalToken.range[0]]); - }, - }); - } - if (spacedAfter) { - report(context, messages.noSpaceAfter, 'noSpaceAfter', { - node: attrNode, - loc: equalToken.loc.start, - fix(fixer) { - return fixer.removeRange([equalToken.range[1], attrNode.value.range[0]]); - }, - }); - } - break; - case 'always': - if (!spacedBefore) { - report(context, messages.needSpaceBefore, 'needSpaceBefore', { - node: attrNode, - loc: equalToken.loc.start, - fix(fixer) { - return fixer.insertTextBefore(equalToken, ' '); - }, - }); - } - if (!spacedAfter) { - report(context, messages.needSpaceAfter, 'needSpaceAfter', { - node: attrNode, - loc: equalToken.loc.start, - fix(fixer) { - return fixer.insertTextAfter(equalToken, ' '); - }, - }); - } - break; + if (config === 'never') { + if (spacedBefore) { + report(context, messages.noSpaceBefore, 'noSpaceBefore', { + node: attrNode, + loc: equalToken.loc.start, + fix(fixer) { + return fixer.removeRange([attrNode.name.range[1], equalToken.range[0]]); + }, + }); + } + if (spacedAfter) { + report(context, messages.noSpaceAfter, 'noSpaceAfter', { + node: attrNode, + loc: equalToken.loc.start, + fix(fixer) { + return fixer.removeRange([equalToken.range[1], attrNode.value.range[0]]); + }, + }); + } + } else if (config === 'always') { + if (!spacedBefore) { + report(context, messages.needSpaceBefore, 'needSpaceBefore', { + node: attrNode, + loc: equalToken.loc.start, + fix(fixer) { + return fixer.insertTextBefore(equalToken, ' '); + }, + }); + } + if (!spacedAfter) { + report(context, messages.needSpaceAfter, 'needSpaceAfter', { + node: attrNode, + loc: equalToken.loc.start, + fix(fixer) { + return fixer.insertTextAfter(equalToken, ' '); + }, + }); + } } }); }, diff --git a/lib/util/propTypes.js b/lib/util/propTypes.js index f3d192e66c..7cd4e2ab7b 100644 --- a/lib/util/propTypes.js +++ b/lib/util/propTypes.js @@ -170,27 +170,31 @@ module.exports = function propTypesInstructions(context, components, utils) { type: 'shape', children: {}, }; - iterateProperties(context, annotation.properties, (childKey, childValue, propNode) => { - const fullName = [parentName, childKey].join('.'); - if (childKey || childValue) { - const types = buildTypeAnnotationDeclarationTypes(childValue, fullName, seen); - types.fullName = fullName; - types.name = childKey; - types.node = propNode; - types.isRequired = !childValue.optional; - shapeTypeDefinition.children[childKey] = types; - } - }, - (spreadNode) => { - const key = astUtil.getKeyValue(context, spreadNode); - const types = buildTypeAnnotationDeclarationTypes(spreadNode, key, seen); - if (!types.children) { - containsUnresolvedObjectTypeSpread = true; - } else { - Object.assign(shapeTypeDefinition, types.children); + iterateProperties( + context, + annotation.properties, + (childKey, childValue, propNode) => { + const fullName = [parentName, childKey].join('.'); + if (childKey || childValue) { + const types = buildTypeAnnotationDeclarationTypes(childValue, fullName, seen); + types.fullName = fullName; + types.name = childKey; + types.node = propNode; + types.isRequired = !childValue.optional; + shapeTypeDefinition.children[childKey] = types; + } + }, + (spreadNode) => { + const key = astUtil.getKeyValue(context, spreadNode); + const types = buildTypeAnnotationDeclarationTypes(spreadNode, key, seen); + if (!types.children) { + containsUnresolvedObjectTypeSpread = true; + } else { + Object.assign(shapeTypeDefinition, types.children); + } + containsSpread = true; } - containsSpread = true; - }); + ); // Mark if this shape has spread or an indexer. We will know to consider all props from this shape as having propTypes, // but still have the ability to detect unused children of this shape. diff --git a/package.json b/package.json index 210dc79f75..c5a87761e6 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "description": "React specific linting rules for ESLint", "main": "index.js", "scripts": { - "lint": "eslint ./", + "lint": "eslint .", "postlint": "npm run type-check", "pretest": "npm run lint", "test": "npm run unit-test", @@ -57,7 +57,7 @@ "aud": "^1.1.5", "babel-eslint": "^8 || ^9 || ^10.1.0", "eslint": "^3 || ^4 || ^5 || ^6 || ^7", - "eslint-config-airbnb-base": "^14.2.1", + "eslint-config-airbnb-base": "^15.0.0", "eslint-plugin-eslint-plugin": "^2.3.0 || ^3.5.3 || ^4.0.1", "eslint-plugin-import": "^2.25.2", "eslint-remote-tester": "^2.0.1", diff --git a/tests/lib/rules/no-deprecated.js b/tests/lib/rules/no-deprecated.js index 4c43bf18e2..bf0e5bd7c3 100644 --- a/tests/lib/rules/no-deprecated.js +++ b/tests/lib/rules/no-deprecated.js @@ -237,20 +237,24 @@ ruleTester.run('no-deprecated', rule, { `, errors: [ errorMessage( - 'componentWillMount', '16.9.0', 'UNSAFE_componentWillMount', - 'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount. ' - + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', + 'componentWillMount', + '16.9.0', + 'UNSAFE_componentWillMount', + 'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount. Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', { type: 'Identifier', line: 3, column: 11 } ), errorMessage( - 'componentWillReceiveProps', '16.9.0', 'UNSAFE_componentWillReceiveProps', - 'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops. ' - + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', + 'componentWillReceiveProps', + '16.9.0', + 'UNSAFE_componentWillReceiveProps', + 'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops. Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', { type: 'Identifier', line: 4, column: 11 } ), - errorMessage('componentWillUpdate', '16.9.0', 'UNSAFE_componentWillUpdate', - 'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate. ' - + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', + errorMessage( + 'componentWillUpdate', + '16.9.0', + 'UNSAFE_componentWillUpdate', + 'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate. Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', { type: 'Identifier', line: 5, column: 11 } ), ], @@ -267,20 +271,24 @@ ruleTester.run('no-deprecated', rule, { `, errors: [ errorMessage( - 'componentWillMount', '16.9.0', 'UNSAFE_componentWillMount', - 'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount. ' - + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', + 'componentWillMount', + '16.9.0', + 'UNSAFE_componentWillMount', + 'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount. Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', { type: 'Identifier', line: 4, column: 13 } ), errorMessage( - 'componentWillReceiveProps', '16.9.0', 'UNSAFE_componentWillReceiveProps', - 'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops. ' - + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', + 'componentWillReceiveProps', + '16.9.0', + 'UNSAFE_componentWillReceiveProps', + 'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops. Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', { type: 'Identifier', line: 5, column: 13 } ), - errorMessage('componentWillUpdate', '16.9.0', 'UNSAFE_componentWillUpdate', - 'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate. ' - + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', + errorMessage( + 'componentWillUpdate', + '16.9.0', + 'UNSAFE_componentWillUpdate', + 'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate. Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', { type: 'Identifier', line: 6, column: 13 } ), ], @@ -295,20 +303,24 @@ ruleTester.run('no-deprecated', rule, { `, errors: [ errorMessage( - 'componentWillMount', '16.9.0', 'UNSAFE_componentWillMount', - 'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount. ' - + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', + 'componentWillMount', + '16.9.0', + 'UNSAFE_componentWillMount', + 'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount. Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', { type: 'Identifier', line: 3, column: 11 } ), errorMessage( - 'componentWillReceiveProps', '16.9.0', 'UNSAFE_componentWillReceiveProps', - 'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops. ' - + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', + 'componentWillReceiveProps', + '16.9.0', + 'UNSAFE_componentWillReceiveProps', + 'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops. Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', { type: 'Identifier', line: 4, column: 11 } ), - errorMessage('componentWillUpdate', '16.9.0', 'UNSAFE_componentWillUpdate', - 'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate. ' - + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', + errorMessage( + 'componentWillUpdate', + '16.9.0', + 'UNSAFE_componentWillUpdate', + 'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate. Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', { type: 'Identifier', line: 5, column: 11 } ), ], @@ -323,20 +335,24 @@ ruleTester.run('no-deprecated', rule, { `, errors: [ errorMessage( - 'componentWillMount', '16.9.0', 'UNSAFE_componentWillMount', - 'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount. ' - + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', + 'componentWillMount', + '16.9.0', + 'UNSAFE_componentWillMount', + 'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount. Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', { type: 'Identifier', line: 3, column: 11 } ), errorMessage( - 'componentWillReceiveProps', '16.9.0', 'UNSAFE_componentWillReceiveProps', - 'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops. ' - + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', + 'componentWillReceiveProps', + '16.9.0', + 'UNSAFE_componentWillReceiveProps', + 'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops. Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', { type: 'Identifier', line: 4, column: 11 } ), - errorMessage('componentWillUpdate', '16.9.0', 'UNSAFE_componentWillUpdate', - 'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate. ' - + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', + errorMessage( + 'componentWillUpdate', + '16.9.0', + 'UNSAFE_componentWillUpdate', + 'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate. Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', { type: 'Identifier', line: 5, column: 11 } ), ], @@ -351,20 +367,24 @@ ruleTester.run('no-deprecated', rule, { `, errors: [ errorMessage( - 'componentWillMount', '16.9.0', 'UNSAFE_componentWillMount', - 'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount. ' - + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', + 'componentWillMount', + '16.9.0', + 'UNSAFE_componentWillMount', + 'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount. Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', { type: 'Identifier', line: 3, column: 11 } ), errorMessage( - 'componentWillReceiveProps', '16.9.0', 'UNSAFE_componentWillReceiveProps', - 'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops. ' - + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', + 'componentWillReceiveProps', + '16.9.0', + 'UNSAFE_componentWillReceiveProps', + 'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops. Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', { type: 'Identifier', line: 4, column: 11 } ), - errorMessage('componentWillUpdate', '16.9.0', 'UNSAFE_componentWillUpdate', - 'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate. ' - + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', + errorMessage( + 'componentWillUpdate', + '16.9.0', + 'UNSAFE_componentWillUpdate', + 'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate. Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', { type: 'Identifier', line: 5, column: 11 } ), ], @@ -379,20 +399,24 @@ ruleTester.run('no-deprecated', rule, { `, errors: [ errorMessage( - 'componentWillMount', '16.9.0', 'UNSAFE_componentWillMount', - 'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount. ' - + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', + 'componentWillMount', + '16.9.0', + 'UNSAFE_componentWillMount', + 'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount. Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', { type: 'Identifier', line: 3, column: 11 } ), errorMessage( - 'componentWillReceiveProps', '16.9.0', 'UNSAFE_componentWillReceiveProps', - 'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops. ' - + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', + 'componentWillReceiveProps', + '16.9.0', + 'UNSAFE_componentWillReceiveProps', + 'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops. Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', { type: 'Identifier', line: 4, column: 11 } ), - errorMessage('componentWillUpdate', '16.9.0', 'UNSAFE_componentWillUpdate', - 'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate. ' - + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', + errorMessage( + 'componentWillUpdate', + '16.9.0', + 'UNSAFE_componentWillUpdate', + 'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate. Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', { type: 'Identifier', line: 5, column: 11 } ), ], @@ -408,20 +432,24 @@ ruleTester.run('no-deprecated', rule, { `, errors: [ errorMessage( - 'componentWillMount', '16.9.0', 'UNSAFE_componentWillMount', - 'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount. ' - + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', + 'componentWillMount', + '16.9.0', + 'UNSAFE_componentWillMount', + 'https://reactjs.org/docs/react-component.html#unsafe_componentwillmount. Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', { type: 'Identifier', line: 4, column: 11 } ), errorMessage( - 'componentWillReceiveProps', '16.9.0', 'UNSAFE_componentWillReceiveProps', - 'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops. ' - + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', + 'componentWillReceiveProps', + '16.9.0', + 'UNSAFE_componentWillReceiveProps', + 'https://reactjs.org/docs/react-component.html#unsafe_componentwillreceiveprops. Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', { type: 'Identifier', line: 5, column: 11 } ), - errorMessage('componentWillUpdate', '16.9.0', 'UNSAFE_componentWillUpdate', - 'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate. ' - + 'Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', + errorMessage( + 'componentWillUpdate', + '16.9.0', + 'UNSAFE_componentWillUpdate', + 'https://reactjs.org/docs/react-component.html#unsafe_componentwillupdate. Use https://github.com/reactjs/react-codemod#rename-unsafe-lifecycles to automatically update your components.', { type: 'Identifier', line: 6, column: 11 } ), ],