[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Enforce ES5 or ES6 class for React Components
|
---|
| 3 | * @author Dan Hamilton
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | 'use strict';
|
---|
| 7 |
|
---|
| 8 | const componentUtil = require('../util/componentUtil');
|
---|
| 9 | const docsUrl = require('../util/docsUrl');
|
---|
| 10 | const report = require('../util/report');
|
---|
| 11 |
|
---|
| 12 | // ------------------------------------------------------------------------------
|
---|
| 13 | // Rule Definition
|
---|
| 14 | // ------------------------------------------------------------------------------
|
---|
| 15 |
|
---|
| 16 | const messages = {
|
---|
| 17 | shouldUseES6Class: 'Component should use es6 class instead of createClass',
|
---|
| 18 | shouldUseCreateClass: 'Component should use createClass instead of es6 class',
|
---|
| 19 | };
|
---|
| 20 |
|
---|
| 21 | /** @type {import('eslint').Rule.RuleModule} */
|
---|
| 22 | module.exports = {
|
---|
| 23 | meta: {
|
---|
| 24 | docs: {
|
---|
| 25 | description: 'Enforce ES5 or ES6 class for React Components',
|
---|
| 26 | category: 'Stylistic Issues',
|
---|
| 27 | recommended: false,
|
---|
| 28 | url: docsUrl('prefer-es6-class'),
|
---|
| 29 | },
|
---|
| 30 |
|
---|
| 31 | messages,
|
---|
| 32 |
|
---|
| 33 | schema: [{
|
---|
| 34 | enum: ['always', 'never'],
|
---|
| 35 | }],
|
---|
| 36 | },
|
---|
| 37 |
|
---|
| 38 | create(context) {
|
---|
| 39 | const configuration = context.options[0] || 'always';
|
---|
| 40 |
|
---|
| 41 | return {
|
---|
| 42 | ObjectExpression(node) {
|
---|
| 43 | if (componentUtil.isES5Component(node, context) && configuration === 'always') {
|
---|
| 44 | report(context, messages.shouldUseES6Class, 'shouldUseES6Class', {
|
---|
| 45 | node,
|
---|
| 46 | });
|
---|
| 47 | }
|
---|
| 48 | },
|
---|
| 49 | ClassDeclaration(node) {
|
---|
| 50 | if (componentUtil.isES6Component(node, context) && configuration === 'never') {
|
---|
| 51 | report(context, messages.shouldUseCreateClass, 'shouldUseCreateClass', {
|
---|
| 52 | node,
|
---|
| 53 | });
|
---|
| 54 | }
|
---|
| 55 | },
|
---|
| 56 | };
|
---|
| 57 | },
|
---|
| 58 | };
|
---|