You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.2 KiB

2 years ago
/**
* @fileoverview Report "this" being used in stateless functional components.
*/
'use strict';
const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
const messages = {
noThisInSFC: 'Stateless functional components should not use `this`',
};
module.exports = {
meta: {
docs: {
description: 'Report "this" being used in stateless components',
category: 'Possible Errors',
recommended: false,
url: docsUrl('no-this-in-sfc'),
},
messages,
schema: [],
},
create: Components.detect((context, components, utils) => ({
MemberExpression(node) {
if (node.object.type === 'ThisExpression') {
const component = components.get(utils.getParentStatelessComponent());
if (!component || (component.node && component.node.parent && component.node.parent.type === 'Property')) {
return;
}
report(context, messages.noThisInSFC, 'noThisInSFC', {
node,
});
}
},
})),
};