diff --git a/eslint-plugin-expensify/display-name-property.js b/eslint-plugin-expensify/display-name-property.js deleted file mode 100644 index c689332..0000000 --- a/eslint-plugin-expensify/display-name-property.js +++ /dev/null @@ -1,65 +0,0 @@ -const _ = require('lodash'); -const {isReactViewFile} = require('./utils'); -const {DISPLAY_NAME_PROPERTY_CLASS, DISPLAY_NAME_PROPERTY_FUNCTION} = require('./CONST').MESSAGE; - -module.exports = { - create: context => ({ - Program(node) { - // Only looking at react files - if (!isReactViewFile(context.getFilename())) { - return; - } - - // Infer the component name from the filename - const filenameParts = _.split(context.getFilename(), '/'); - let componentName = _(filenameParts).last().split('.')[0]; - if (componentName === 'index') { - componentName = filenameParts[filenameParts.length - 2]; - } - - // Find a root level variable delcaration that matches the component name - const functionComponent = _.find(node.body, n => n.type === 'VariableDeclaration' - && _.get(n, 'declarations[0].id.name') === componentName - && ['FunctionExpression', 'ArrowFunctionExpression'].includes(_.get(n, 'declarations[0].init.type'))); - - // Find a root level class delcaration that matches the component name - const classComponent = _.find(node.body, n => n.type === 'ClassDeclaration' - && _.get(n, 'id.name') === componentName); - - if (_.isUndefined(functionComponent) && _.isUndefined(classComponent)) { - return; - } - - // Find a root level assignment expression that matches .displayName = ... - const displayNameAssignment = _.find(node.body, bodyNode => bodyNode.type === 'ExpressionStatement' - && _.get(bodyNode, 'expression.type', '') === 'AssignmentExpression' - && _.get(bodyNode, 'expression.left.property.name', '') === 'displayName' - && _.get(bodyNode, 'expression.left.object.name', '') === componentName); - - // Allow class components without displayName set - if (!_.isUndefined(classComponent) && _.isUndefined(displayNameAssignment)) { - return; - } - - // Allow function components with displayName set - if (!_.isUndefined(functionComponent) && !_.isUndefined(displayNameAssignment)) { - return; - } - - const isNoopComponent = _.get(functionComponent, 'declarations[0].init.type') === 'ArrowFunctionExpression' - && _.get(functionComponent, 'declarations[0].init.body.type') === 'Literal' - && _.get(functionComponent, 'declarations[0].init.body.value') === null; - - // Ignore noop components, ie. const Test = () => null; - if (isNoopComponent) { - return; - } - - // Otherwise report - context.report({ - node, - message: classComponent ? DISPLAY_NAME_PROPERTY_CLASS : DISPLAY_NAME_PROPERTY_FUNCTION, - }); - }, - }), -}; diff --git a/eslint-plugin-expensify/tests/display-name-property.test.js b/eslint-plugin-expensify/tests/display-name-property.test.js deleted file mode 100644 index ecc82fe..0000000 --- a/eslint-plugin-expensify/tests/display-name-property.test.js +++ /dev/null @@ -1,123 +0,0 @@ -const RuleTester = require('eslint').RuleTester; -const rule = require('../display-name-property'); -const {DISPLAY_NAME_PROPERTY_CLASS, DISPLAY_NAME_PROPERTY_FUNCTION} = require('../CONST').MESSAGE; - -const ruleTester = new RuleTester({ - parserOptions: { - ecmaVersion: 6, - sourceType: 'module', - }, -}); - -const functionalComponentWithDisplayName = ` - const Test = () => 'Test'; - Test.displayName = 'Test'; -`; - -const classComponentWithoutDisplayName = ` - class Test extends Component { - render() { - return 'Test'; - } - } -`; - -const functionalComponentWithoutDisplayName = ` - const Test = () => 'Test'; -`; - -const classComponentWithDisplayName = ` - class Test extends Component { - render() { - return 'Test'; - } - } - Test.displayName = 'Test'; -`; - -const functionalNoopComponentWithoutDisplayName = ` - const Test = () => null; -`; - -ruleTester.run('display-name-property', rule, { - valid: [ - { - code: functionalComponentWithDisplayName, - filename: './src/components/Test.js', - }, - { - code: classComponentWithoutDisplayName, - filename: './src/components/Test.js', - }, - { - code: functionalComponentWithDisplayName, - filename: './src/components/Test/index.js', - }, - { - code: classComponentWithoutDisplayName, - filename: './src/components/Test/index.js', - }, - { - code: functionalComponentWithDisplayName, - filename: './src/components/Test/index.native.js', - }, - { - code: classComponentWithoutDisplayName, - filename: './src/components/Test/index.native.js', - }, - { - code: ` - const examplePropTypes = {}; - `, - filename: './src/components/examplePropTypes.js', - }, - { - code: functionalNoopComponentWithoutDisplayName, - filename: './src/components/Test.js', - }, - ], - invalid: [ - { - code: functionalComponentWithoutDisplayName, - filename: './src/components/Test.js', - errors: [{ - message: DISPLAY_NAME_PROPERTY_FUNCTION, - }], - }, - { - code: classComponentWithDisplayName, - filename: './src/components/Test.js', - errors: [{ - message: DISPLAY_NAME_PROPERTY_CLASS, - }], - }, - { - code: functionalComponentWithoutDisplayName, - filename: './src/components/Test/index.js', - errors: [{ - message: DISPLAY_NAME_PROPERTY_FUNCTION, - }], - }, - { - code: classComponentWithDisplayName, - filename: './src/components/Test/index.js', - errors: [{ - message: DISPLAY_NAME_PROPERTY_CLASS, - }], - }, - { - code: functionalComponentWithoutDisplayName, - filename: './src/components/Test/index.native.js', - errors: [{ - message: DISPLAY_NAME_PROPERTY_FUNCTION, - }], - }, - { - code: classComponentWithDisplayName, - filename: './src/components/Test/index.native.js', - errors: [{ - message: DISPLAY_NAME_PROPERTY_CLASS, - }], - }, - ], -}); diff --git a/rules/expensify.js b/rules/expensify.js index e9fc647..1934a49 100644 --- a/rules/expensify.js +++ b/rules/expensify.js @@ -14,7 +14,6 @@ module.exports = { 'rulesdir/no-multiple-onyx-in-file': 'error', 'rulesdir/no-call-actions-from-actions': 'error', 'rulesdir/no-api-side-effects-method': 'error', - 'rulesdir/display-name-property': 'error', 'rulesdir/prefer-localization': 'error', 'rulesdir/onyx-props-must-have-default': 'error', 'no-restricted-imports': ['error', {