diff --git a/lib/rules/no-arrow-function-lifecycle.js b/lib/rules/no-arrow-function-lifecycle.js index 2ca48afe87..fc2f482a4f 100644 --- a/lib/rules/no-arrow-function-lifecycle.js +++ b/lib/rules/no-arrow-function-lifecycle.js @@ -2,6 +2,7 @@ * @fileoverview Lifecycle methods should be methods on the prototype, not class fields * @author Tan Nguyen */ + 'use strict'; const Components = require('../util/Components'); @@ -17,7 +18,8 @@ module.exports = { recommended: false, url: docsUrl('no-arrow-function-lifecycle') }, - schema: [] + schema: [], + fixable: 'code', }, create: Components.detect((context, components) => { @@ -25,10 +27,10 @@ module.exports = { * @param {Array} properties list of component properties */ function reportNoArrowFunctionLifecycle(properties) { - properties.forEach(node => { + properties.forEach((node) => { const propertyName = astUtil.getPropertyName(node); const nodeType = node.value && node.value.type; - const isLifecycleMethod = lifecycleMethods.indexOf(propertyName) !== -1; + const isLifecycleMethod = lifecycleMethods.includes(propertyName); if (nodeType === 'ArrowFunctionExpression' && isLifecycleMethod) { context.report({ @@ -36,16 +38,16 @@ module.exports = { message: '{{propertyName}} is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.', data: { propertyName - } + }, }); } }); } return { - 'Program:exit': function() { + 'Program:exit'() { const list = components.list(); - Object.keys(list).forEach(component => { + Object.keys(list).forEach((component) => { const properties = astUtil.getComponentProperties(list[component].node); reportNoArrowFunctionLifecycle(properties); }); diff --git a/lib/util/lifecycleMethods.js b/lib/util/lifecycleMethods.js index 2fed165647..6e630cd8f5 100644 --- a/lib/util/lifecycleMethods.js +++ b/lib/util/lifecycleMethods.js @@ -2,6 +2,7 @@ * @fileoverview lifecycle methods * @author Tan Nguyen */ + 'use strict'; module.exports = [ diff --git a/tests/lib/rules/no-arrow-function-lifecycle.js b/tests/lib/rules/no-arrow-function-lifecycle.js index 3ef25f385c..59629aada3 100644 --- a/tests/lib/rules/no-arrow-function-lifecycle.js +++ b/tests/lib/rules/no-arrow-function-lifecycle.js @@ -2,12 +2,13 @@ * @fileoverview It is not necessary to use arrow function for lifecycle methods * @author Tan Nguyen */ + 'use strict'; -const rule = require('../../../lib/rules/no-arrow-function-lifecycle'); const RuleTester = require('eslint').RuleTester; +const rule = require('../../../lib/rules/no-arrow-function-lifecycle'); -require('babel-eslint'); +const parsers = require('../../helpers/parsers'); const parserOptions = { ecmaVersion: 2018, @@ -23,707 +24,767 @@ const parserOptions = { const ruleTester = new RuleTester({parserOptions}); ruleTester.run('no-arrow-function-lifecycle', rule, { - valid: [{ - code: ` - var Hello = createReactClass({ - render: function() { return
; } - }); - ` - }, { - code: ` - var Hello = createReactClass({ - getDefaultProps: function() { return {}; }, - render: function() { return
; } - }); - ` - }, { - code: ` - var Hello = createReactClass({ - getInitialState: function() { return {}; }, - render: function() { return
; } - }); - ` - }, { - code: ` - var Hello = createReactClass({ - getChildContext: function() { return {}; }, - render: function() { return
; } - }); - ` - }, { - code: ` - var Hello = createReactClass({ - getDerivedStateFromProps: function() { return {}; }, - render: function() { return
; } - }); - ` - }, { - code: ` - var Hello = createReactClass({ - componentWillMount: function() {}, - render: function() { return
; } - }); - ` - }, { - code: ` - var Hello = createReactClass({ - UNSAFE_componentWillMount: function() {}, - render: function() { return
; } - }); - ` - }, { - code: ` - var Hello = createReactClass({ - componentDidMount: function() {}, - render: function() { return
; } - }); - ` - }, { - code: ` - var Hello = createReactClass({ - componentWillReceiveProps: function() {}, - render: function() { return
; } - }); - ` - }, { - code: ` - var Hello = createReactClass({ - UNSAFE_componentWillReceiveProps: function() {}, - render: function() { return
; } - }); - ` - }, { - code: ` - var Hello = createReactClass({ - shouldComponentUpdate: function() { return true; }, - render: function() { return
; } - }); - ` - }, { - code: ` - var Hello = createReactClass({ - componentWillUpdate: function() {}, - render: function() { return
; } - }); - ` - }, { - code: ` - var Hello = createReactClass({ - UNSAFE_componentWillUpdate: function() {}, - render: function() { return
; } - }); - ` - }, { - code: ` - var Hello = createReactClass({ - getSnapshotBeforeUpdate: function() { return {}; }, - render: function() { return
; } - }); - ` - }, { - code: ` - var Hello = createReactClass({ - componentDidUpdate: function() {}, - render: function() { return
; } - }); - ` - }, { - code: ` - var Hello = createReactClass({ - componentDidCatch: function() {}, - render: function() { return
; } - }); - ` - }, { - code: ` - var Hello = createReactClass({ - componentWillUnmount: function() {}, - render: function() { return
; } - }); - ` - }, { - code: ` - var Hello = createReactClass({ - componentWillUnmount: function() {}, - render: function() { return
; } - }); - ` - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - render() { return
; } - } - `, - parser: 'babel-eslint' - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - getDefaultProps() { return {}; } - render() { return
; } - } - `, - parser: 'babel-eslint' - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - getInitialState() { return {}; } - render() { return
; } - } - `, - parser: 'babel-eslint' - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - getChildContext() { return {}; } - render() { return
; } - } - `, - parser: 'babel-eslint' - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - getDerivedStateFromProps() { return {}; } - render() { return
; } - } - `, - parser: 'babel-eslint' - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - componentWillMount() {} - render() { return
; } - } - `, - parser: 'babel-eslint' - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - UNSAFE_componentWillMount() {} - render() { return
; } - } - `, - parser: 'babel-eslint' - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - componentDidMount() {} - render() { return
; } - } - `, - parser: 'babel-eslint' - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - componentWillReceiveProps() {} - render() { return
; } - } - `, - parser: 'babel-eslint' - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - UNSAFE_componentWillReceiveProps() {} - render() { return
; } - } - `, - parser: 'babel-eslint' - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - shouldComponentUpdate() { return true; } - render() { return
; } - } - `, - parser: 'babel-eslint' - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - componentWillUpdate() {} - render() { return
; } - } - `, - parser: 'babel-eslint' - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - UNSAFE_componentWillUpdate() {} - render() { return
; } - } - `, - parser: 'babel-eslint' - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - getSnapshotBeforeUpdate() { return {}; } - render() { return
; } - } - `, - parser: 'babel-eslint' - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - componentDidUpdate() {} - render() { return
; } - } - `, - parser: 'babel-eslint' - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - componentDidCatch() {} - render() { return
; } - } - `, - parser: 'babel-eslint' - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - componentWillUnmount() {} - render() { return
; } - } - `, - parser: 'babel-eslint' - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - componentWillUnmount() {} - render() { return
; } - } - `, - parser: 'babel-eslint' - }], + valid: [ + { + code: ` + var Hello = createReactClass({ + render: function() { return
; } + }); + ` + }, + { + code: ` + var Hello = createReactClass({ + getDefaultProps: function() { return {}; }, + render: function() { return
; } + }); + ` + }, + { + code: ` + var Hello = createReactClass({ + getInitialState: function() { return {}; }, + render: function() { return
; } + }); + ` + }, + { + code: ` + var Hello = createReactClass({ + getChildContext: function() { return {}; }, + render: function() { return
; } + }); + ` + }, + { + code: ` + var Hello = createReactClass({ + getDerivedStateFromProps: function() { return {}; }, + render: function() { return
; } + }); + ` + }, + { + code: ` + var Hello = createReactClass({ + componentWillMount: function() {}, + render: function() { return
; } + }); + ` + }, + { + code: ` + var Hello = createReactClass({ + UNSAFE_componentWillMount: function() {}, + render: function() { return
; } + }); + ` + }, + { + code: ` + var Hello = createReactClass({ + componentDidMount: function() {}, + render: function() { return
; } + }); + ` + }, + { + code: ` + var Hello = createReactClass({ + componentWillReceiveProps: function() {}, + render: function() { return
; } + }); + ` + }, + { + code: ` + var Hello = createReactClass({ + UNSAFE_componentWillReceiveProps: function() {}, + render: function() { return
; } + }); + ` + }, + { + code: ` + var Hello = createReactClass({ + shouldComponentUpdate: function() { return true; }, + render: function() { return
; } + }); + ` + }, + { + code: ` + var Hello = createReactClass({ + componentWillUpdate: function() {}, + render: function() { return
; } + }); + ` + }, + { + code: ` + var Hello = createReactClass({ + UNSAFE_componentWillUpdate: function() {}, + render: function() { return
; } + }); + ` + }, + { + code: ` + var Hello = createReactClass({ + getSnapshotBeforeUpdate: function() { return {}; }, + render: function() { return
; } + }); + ` + }, + { + code: ` + var Hello = createReactClass({ + componentDidUpdate: function() {}, + render: function() { return
; } + }); + ` + }, + { + code: ` + var Hello = createReactClass({ + componentDidCatch: function() {}, + render: function() { return
; } + }); + ` + }, + { + code: ` + var Hello = createReactClass({ + componentWillUnmount: function() {}, + render: function() { return
; } + }); + ` + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + render() { return
; } + } + `, + parser: parsers.BABEL_ESLINT + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + getDefaultProps() { return {}; } + render() { return
; } + } + `, + parser: parsers.BABEL_ESLINT + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + getInitialState() { return {}; } + render() { return
; } + } + `, + parser: parsers.BABEL_ESLINT + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + getChildContext() { return {}; } + render() { return
; } + } + `, + parser: parsers.BABEL_ESLINT + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + getDerivedStateFromProps() { return {}; } + render() { return
; } + } + `, + parser: parsers.BABEL_ESLINT + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + componentWillMount() {} + render() { return
; } + } + `, + parser: parsers.BABEL_ESLINT + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + UNSAFE_componentWillMount() {} + render() { return
; } + } + `, + parser: parsers.BABEL_ESLINT + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + componentDidMount() {} + render() { return
; } + } + `, + parser: parsers.BABEL_ESLINT + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + componentWillReceiveProps() {} + render() { return
; } + } + `, + parser: parsers.BABEL_ESLINT + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + UNSAFE_componentWillReceiveProps() {} + render() { return
; } + } + `, + parser: parsers.BABEL_ESLINT + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + shouldComponentUpdate() { return true; } + render() { return
; } + } + `, + parser: parsers.BABEL_ESLINT + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + componentWillUpdate() {} + render() { return
; } + } + `, + parser: parsers.BABEL_ESLINT + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + UNSAFE_componentWillUpdate() {} + render() { return
; } + } + `, + parser: parsers.BABEL_ESLINT + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + getSnapshotBeforeUpdate() { return {}; } + render() { return
; } + } + `, + parser: parsers.BABEL_ESLINT + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + componentDidUpdate() {} + render() { return
; } + } + `, + parser: parsers.BABEL_ESLINT + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + componentDidCatch() {} + render() { return
; } + } + `, + parser: parsers.BABEL_ESLINT + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + componentWillUnmount() {} + render() { return
; } + } + `, + parser: parsers.BABEL_ESLINT + } + ], - invalid: [{ - code: ` - var Hello = createReactClass({ - render: () => { return
; } - }); - `, - errors: [{ - message: 'render is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - var Hello = createReactClass({ - getDefaultProps: () => { return {}; }, - render: function() { return
; } - }); - `, - errors: [{ - message: 'getDefaultProps is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - var Hello = createReactClass({ - getInitialState: () => { return {}; }, - render: function() { return
; } - }); - `, - errors: [{ - message: 'getInitialState is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - var Hello = createReactClass({ - getChildContext: () => { return {}; }, - render: function() { return
; } - }); - `, - errors: [{ - message: 'getChildContext is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - var Hello = createReactClass({ - getDerivedStateFromProps: () => { return {}; }, - render: function() { return
; } - }); - `, - errors: [{ - message: 'getDerivedStateFromProps is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - var Hello = createReactClass({ - componentWillMount: () => {}, - render: function() { return
; } - }); - `, - errors: [{ - message: 'componentWillMount is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - var Hello = createReactClass({ - UNSAFE_componentWillMount: () => {}, - render: function() { return
; } - }); - `, - errors: [{ - message: 'UNSAFE_componentWillMount is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - var Hello = createReactClass({ - componentDidMount: () => {}, - render: function() { return
; } - }); - `, - errors: [{ - message: 'componentDidMount is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - var Hello = createReactClass({ - componentWillReceiveProps: () => {}, - render: function() { return
; } - }); - `, - errors: [{ - message: 'componentWillReceiveProps is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - var Hello = createReactClass({ - UNSAFE_componentWillReceiveProps: () => {}, - render: function() { return
; } - }); - `, - errors: [{ - message: 'UNSAFE_componentWillReceiveProps is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - var Hello = createReactClass({ - shouldComponentUpdate: () => { return true; }, - render: function() { return
; } - }); - `, - errors: [{ - message: 'shouldComponentUpdate is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - var Hello = createReactClass({ - componentWillUpdate: () => {}, - render: function() { return
; } - }); - `, - errors: [{ - message: 'componentWillUpdate is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - var Hello = createReactClass({ - UNSAFE_componentWillUpdate: () => {}, - render: function() { return
; } - }); - `, - errors: [{ - message: 'UNSAFE_componentWillUpdate is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - var Hello = createReactClass({ - getSnapshotBeforeUpdate: () => { return {}; }, - render: function() { return
; } - }); - `, - errors: [{ - message: 'getSnapshotBeforeUpdate is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - var Hello = createReactClass({ - componentDidUpdate: () => {}, - render: function() { return
; } - }); - `, - errors: [{ - message: 'componentDidUpdate is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - var Hello = createReactClass({ - componentDidCatch: () => {}, - render: function() { return
; } - }); - `, - errors: [{ - message: 'componentDidCatch is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - var Hello = createReactClass({ - componentWillUnmount: () => {}, - render: function() { return
; } - }); - `, - errors: [{ - message: 'componentWillUnmount is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - var Hello = createReactClass({ - componentWillUnmount: () => {}, - render: function() { return
; } - }); - `, - errors: [{ - message: 'componentWillUnmount is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - render = () => { return
; } - } - `, - parser: 'babel-eslint', - errors: [{ - message: 'render is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - getDefaultProps = () => { return {}; } - render = () => { return
; } - } - `, - parser: 'babel-eslint', - errors: [{ - message: 'getDefaultProps is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }, { - message: 'render is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - getInitialState = () => { return {}; } - render = () => { return
; } - } - `, - parser: 'babel-eslint', - errors: [{ - message: 'getInitialState is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }, { - message: 'render is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - getChildContext = () => { return {}; } - render = () => { return
; } - } - `, - parser: 'babel-eslint', - errors: [{ - message: 'getChildContext is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }, { - message: 'render is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - getDerivedStateFromProps = () => { return {}; } - render = () => { return
; } - } - `, - parser: 'babel-eslint', - errors: [{ - message: 'getDerivedStateFromProps is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }, { - message: 'render is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - componentWillMount = () => {} - render = () => { return
; } - } - `, - parser: 'babel-eslint', - errors: [{ - message: 'componentWillMount is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }, { - message: 'render is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - UNSAFE_componentWillMount = () => {} - render = () => { return
; } - } - `, - parser: 'babel-eslint', - errors: [{ - message: 'UNSAFE_componentWillMount is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }, { - message: 'render is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - componentDidMount = () => {} - render = () => { return
; } - } - `, - parser: 'babel-eslint', - errors: [{ - message: 'componentDidMount is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }, { - message: 'render is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - componentWillReceiveProps = () => {} - render = () => { return
; } - } - `, - parser: 'babel-eslint', - errors: [{ - message: 'componentWillReceiveProps is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }, { - message: 'render is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - UNSAFE_componentWillReceiveProps = () => {} - render = () => { return
; } - } - `, - parser: 'babel-eslint', - errors: [{ - message: 'UNSAFE_componentWillReceiveProps is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }, { - message: 'render is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - shouldComponentUpdate = () => { return true; } - render = () => { return
; } - } - `, - parser: 'babel-eslint', - errors: [{ - message: 'shouldComponentUpdate is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }, { - message: 'render is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - componentWillUpdate = () => {} - render = () => { return
; } - } - `, - parser: 'babel-eslint', - errors: [{ - message: 'componentWillUpdate is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }, { - message: 'render is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - UNSAFE_componentWillUpdate = () => {} - render = () => { return
; } - } - `, - parser: 'babel-eslint', - errors: [{ - message: 'UNSAFE_componentWillUpdate is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }, { - message: 'render is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - getSnapshotBeforeUpdate = () => { return {}; } - render = () => { return
; } - } - `, - parser: 'babel-eslint', - errors: [{ - message: 'getSnapshotBeforeUpdate is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }, { - message: 'render is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - componentDidUpdate = () => {} - render = () => { return
; } - } - `, - parser: 'babel-eslint', - errors: [{ - message: 'componentDidUpdate is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }, { - message: 'render is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - componentDidCatch = () => {} - render = () => { return
; } - } - `, - parser: 'babel-eslint', - errors: [{ - message: 'componentDidCatch is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }, { - message: 'render is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }, { - code: ` - class Hello extends React.Component { - handleEventMethods = () => {} - componentWillUnmount = () => {} - render = () => { return
; } - } - `, - parser: 'babel-eslint', - errors: [{ - message: 'componentWillUnmount is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }, { - message: 'render is a React lifecycle method, and should not be an arrow function. Use an instance method instead.' - }] - }] + invalid: [ + { + code: ` + var Hello = createReactClass({ + render: () => { return
; } + }); + `, + errors: [{ + message: 'render is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + var Hello = createReactClass({ + getDefaultProps: () => { return {}; }, + render: function() { return
; } + }); + `, + errors: [{ + message: 'getDefaultProps is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + var Hello = createReactClass({ + getInitialState: () => { return {}; }, + render: function() { return
; } + }); + `, + errors: [{ + message: 'getInitialState is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + var Hello = createReactClass({ + getChildContext: () => { return {}; }, + render: function() { return
; } + }); + `, + errors: [{ + message: 'getChildContext is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + var Hello = createReactClass({ + getDerivedStateFromProps: () => { return {}; }, + render: function() { return
; } + }); + `, + errors: [{ + message: 'getDerivedStateFromProps is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + var Hello = createReactClass({ + componentWillMount: () => {}, + render: function() { return
; } + }); + `, + errors: [{ + message: 'componentWillMount is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + var Hello = createReactClass({ + UNSAFE_componentWillMount: () => {}, + render: function() { return
; } + }); + `, + errors: [{ + message: 'UNSAFE_componentWillMount is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + var Hello = createReactClass({ + componentDidMount: () => {}, + render: function() { return
; } + }); + `, + errors: [{ + message: 'componentDidMount is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + var Hello = createReactClass({ + componentWillReceiveProps: () => {}, + render: function() { return
; } + }); + `, + errors: [{ + message: 'componentWillReceiveProps is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + var Hello = createReactClass({ + UNSAFE_componentWillReceiveProps: () => {}, + render: function() { return
; } + }); + `, + errors: [{ + message: 'UNSAFE_componentWillReceiveProps is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + var Hello = createReactClass({ + shouldComponentUpdate: () => { return true; }, + render: function() { return
; } + }); + `, + errors: [{ + message: 'shouldComponentUpdate is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + var Hello = createReactClass({ + componentWillUpdate: () => {}, + render: function() { return
; } + }); + `, + errors: [{ + message: 'componentWillUpdate is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + var Hello = createReactClass({ + UNSAFE_componentWillUpdate: () => {}, + render: function() { return
; } + }); + `, + errors: [{ + message: 'UNSAFE_componentWillUpdate is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + var Hello = createReactClass({ + getSnapshotBeforeUpdate: () => { return {}; }, + render: function() { return
; } + }); + `, + errors: [{ + message: 'getSnapshotBeforeUpdate is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + var Hello = createReactClass({ + componentDidUpdate: () => {}, + render: function() { return
; } + }); + `, + errors: [{ + message: 'componentDidUpdate is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + var Hello = createReactClass({ + componentDidCatch: () => {}, + render: function() { return
; } + }); + `, + errors: [{ + message: 'componentDidCatch is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + var Hello = createReactClass({ + componentWillUnmount: () => {}, + render: function() { return
; } + }); + `, + errors: [{ + message: 'componentWillUnmount is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + render = () => { return
; } + } + `, + parser: parsers.BABEL_ESLINT, + errors: [{ + message: 'render is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + getDefaultProps = () => { return {}; } + render = () => { return
; } + } + `, + parser: parsers.BABEL_ESLINT, + errors: [{ + message: 'getDefaultProps is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }, + { + message: 'render is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + getInitialState = () => { return {}; } + render = () => { return
; } + } + `, + parser: parsers.BABEL_ESLINT, + errors: [{ + message: 'getInitialState is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }, + { + message: 'render is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + getChildContext = () => { return {}; } + render = () => { return
; } + } + `, + parser: parsers.BABEL_ESLINT, + errors: [{ + message: 'getChildContext is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }, + { + message: 'render is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + getDerivedStateFromProps = () => { return {}; } + render = () => { return
; } + } + `, + parser: parsers.BABEL_ESLINT, + errors: [{ + message: 'getDerivedStateFromProps is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }, + { + message: 'render is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + componentWillMount = () => {} + render = () => { return
; } + } + `, + parser: parsers.BABEL_ESLINT, + errors: [{ + message: 'componentWillMount is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }, + { + message: 'render is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + UNSAFE_componentWillMount = () => {} + render = () => { return
; } + } + `, + parser: parsers.BABEL_ESLINT, + errors: [{ + message: 'UNSAFE_componentWillMount is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }, + { + message: 'render is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + componentDidMount = () => {} + render = () => { return
; } + } + `, + parser: parsers.BABEL_ESLINT, + errors: [{ + message: 'componentDidMount is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }, + { + message: 'render is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + componentWillReceiveProps = () => {} + render = () => { return
; } + } + `, + parser: parsers.BABEL_ESLINT, + errors: [{ + message: 'componentWillReceiveProps is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }, + { + message: 'render is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + UNSAFE_componentWillReceiveProps = () => {} + render = () => { return
; } + } + `, + parser: parsers.BABEL_ESLINT, + errors: [{ + message: 'UNSAFE_componentWillReceiveProps is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }, + { + message: 'render is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + shouldComponentUpdate = () => { return true; } + render = () => { return
; } + } + `, + parser: parsers.BABEL_ESLINT, + errors: [{ + message: 'shouldComponentUpdate is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }, + { + message: 'render is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + componentWillUpdate = () => {} + render = () => { return
; } + } + `, + parser: parsers.BABEL_ESLINT, + errors: [{ + message: 'componentWillUpdate is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }, + { + message: 'render is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + UNSAFE_componentWillUpdate = () => {} + render = () => { return
; } + } + `, + parser: parsers.BABEL_ESLINT, + errors: [{ + message: 'UNSAFE_componentWillUpdate is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }, + { + message: 'render is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + getSnapshotBeforeUpdate = () => { return {}; } + render = () => { return
; } + } + `, + parser: parsers.BABEL_ESLINT, + errors: [{ + message: 'getSnapshotBeforeUpdate is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }, + { + message: 'render is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + componentDidUpdate = (prevProps) => {} + render = () => { return
; } + } + `, + parser: parsers.BABEL_ESLINT, + errors: [{ + message: 'componentDidUpdate is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }, + { + message: 'render is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + componentDidCatch = () => {} + render = () => { return
; } + } + `, + parser: parsers.BABEL_ESLINT, + errors: [{ + message: 'componentDidCatch is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }, + { + message: 'render is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + }, + { + code: ` + class Hello extends React.Component { + handleEventMethods = () => {} + componentWillUnmount = () => {} + render = () => { return
; } + } + `, + parser: parsers.BABEL_ESLINT, + errors: [{ + message: 'componentWillUnmount is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }, + { + message: 'render is a React lifecycle method, and should not be an arrow function or in a class field. Use an instance method instead.' + }] + } + ] });