123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- /**
- * @fileoverview Enforces consistent naming for boolean props
- * @author Ev Haus
- */
- 'use strict';
- const Components = require('../util/Components');
- const propsUtil = require('../util/props');
- const docsUrl = require('../util/docsUrl');
- const propWrapperUtil = require('../util/propWrapper');
- // ------------------------------------------------------------------------------
- // Rule Definition
- // ------------------------------------------------------------------------------
- // Predefine message for use in context.report conditional.
- // messageId will still be usable in tests.
- const PATTERN_MISMATCH_MSG = 'Prop name ({{propName}}) doesn\'t match rule ({{pattern}})';
- module.exports = {
- meta: {
- docs: {
- category: 'Stylistic Issues',
- description: 'Enforces consistent naming for boolean props',
- recommended: false,
- url: docsUrl('boolean-prop-naming')
- },
- messages: {
- patternMismatch: PATTERN_MISMATCH_MSG
- },
- schema: [{
- additionalProperties: false,
- properties: {
- propTypeNames: {
- items: {
- type: 'string'
- },
- minItems: 1,
- type: 'array',
- uniqueItems: true
- },
- rule: {
- default: '^(is|has)[A-Z]([A-Za-z0-9]?)+',
- minLength: 1,
- type: 'string'
- },
- message: {
- minLength: 1,
- type: 'string'
- },
- validateNested: {
- default: false,
- type: 'boolean'
- }
- },
- type: 'object'
- }]
- },
- create: Components.detect((context, components, utils) => {
- const config = context.options[0] || {};
- const rule = config.rule ? new RegExp(config.rule) : null;
- const propTypeNames = config.propTypeNames || ['bool'];
- // Remembers all Flowtype object definitions
- const objectTypeAnnotations = new Map();
- /**
- * Returns the prop key to ensure we handle the following cases:
- * propTypes: {
- * full: React.PropTypes.bool,
- * short: PropTypes.bool,
- * direct: bool,
- * required: PropTypes.bool.isRequired
- * }
- * @param {Object} node The node we're getting the name of
- * @returns {string | null}
- */
- function getPropKey(node) {
- // Check for `ExperimentalSpreadProperty` (ESLint 3/4) and `SpreadElement` (ESLint 5)
- // so we can skip validation of those fields.
- // Otherwise it will look for `node.value.property` which doesn't exist and breaks ESLint.
- if (node.type === 'ExperimentalSpreadProperty' || node.type === 'SpreadElement') {
- return null;
- }
- if (node.value && node.value.property) {
- const name = node.value.property.name;
- if (name === 'isRequired') {
- if (node.value.object && node.value.object.property) {
- return node.value.object.property.name;
- }
- return null;
- }
- return name;
- }
- if (node.value && node.value.type === 'Identifier') {
- return node.value.name;
- }
- return null;
- }
- /**
- * Returns the name of the given node (prop)
- * @param {Object} node The node we're getting the name of
- * @returns {string}
- */
- function getPropName(node) {
- // Due to this bug https://github.com/babel/babel-eslint/issues/307
- // we can't get the name of the Flow object key name. So we have
- // to hack around it for now.
- if (node.type === 'ObjectTypeProperty') {
- return context.getSourceCode().getFirstToken(node).value;
- }
- return node.key.name;
- }
- /**
- * Checks if prop is declared in flow way
- * @param {Object} prop Property object, single prop type declaration
- * @returns {Boolean}
- */
- function flowCheck(prop) {
- return (
- prop.type === 'ObjectTypeProperty'
- && prop.value.type === 'BooleanTypeAnnotation'
- && rule.test(getPropName(prop)) === false
- );
- }
- /**
- * Checks if prop is declared in regular way
- * @param {Object} prop Property object, single prop type declaration
- * @returns {Boolean}
- */
- function regularCheck(prop) {
- const propKey = getPropKey(prop);
- return (
- propKey
- && propTypeNames.indexOf(propKey) >= 0
- && rule.test(getPropName(prop)) === false
- );
- }
- function tsCheck(prop) {
- if (prop.type !== 'TSPropertySignature') return false;
- const typeAnnotation = (prop.typeAnnotation || {}).typeAnnotation;
- return (
- typeAnnotation
- && typeAnnotation.type === 'TSBooleanKeyword'
- && rule.test(getPropName(prop)) === false
- );
- }
- /**
- * Checks if prop is nested
- * @param {Object} prop Property object, single prop type declaration
- * @returns {Boolean}
- */
- function nestedPropTypes(prop) {
- return (
- prop.type === 'Property'
- && prop.value.type === 'CallExpression'
- );
- }
- /**
- * Runs recursive check on all proptypes
- * @param {Array} proptypes A list of Property object (for each proptype defined)
- * @param {Function} addInvalidProp callback to run for each error
- */
- function runCheck(proptypes, addInvalidProp) {
- proptypes = proptypes || [];
- proptypes.forEach((prop) => {
- if (config.validateNested && nestedPropTypes(prop)) {
- runCheck(prop.value.arguments[0].properties, addInvalidProp);
- return;
- }
- if (flowCheck(prop) || regularCheck(prop) || tsCheck(prop)) {
- addInvalidProp(prop);
- }
- });
- }
- /**
- * Checks and mark props with invalid naming
- * @param {Object} node The component node we're testing
- * @param {Array} proptypes A list of Property object (for each proptype defined)
- */
- function validatePropNaming(node, proptypes) {
- const component = components.get(node) || node;
- const invalidProps = component.invalidProps || [];
- runCheck(proptypes, (prop) => {
- invalidProps.push(prop);
- });
- components.set(node, {
- invalidProps
- });
- }
- /**
- * Reports invalid prop naming
- * @param {Object} component The component to process
- */
- function reportInvalidNaming(component) {
- component.invalidProps.forEach((propNode) => {
- const propName = getPropName(propNode);
- context.report(Object.assign({
- node: propNode,
- data: {
- component: propName,
- propName,
- pattern: config.rule
- }
- }, config.message ? {message: config.message} : {messageId: 'patternMismatch'}));
- });
- }
- function checkPropWrapperArguments(node, args) {
- if (!node || !Array.isArray(args)) {
- return;
- }
- args.filter((arg) => arg.type === 'ObjectExpression').forEach((object) => validatePropNaming(node, object.properties));
- }
- // --------------------------------------------------------------------------
- // Public
- // --------------------------------------------------------------------------
- return {
- ClassProperty(node) {
- if (!rule || !propsUtil.isPropTypesDeclaration(node)) {
- return;
- }
- if (
- node.value
- && node.value.type === 'CallExpression'
- && propWrapperUtil.isPropWrapperFunction(
- context,
- context.getSourceCode().getText(node.value.callee)
- )
- ) {
- checkPropWrapperArguments(node, node.value.arguments);
- }
- if (node.value && node.value.properties) {
- validatePropNaming(node, node.value.properties);
- }
- if (node.typeAnnotation && node.typeAnnotation.typeAnnotation) {
- validatePropNaming(node, node.typeAnnotation.typeAnnotation.properties);
- }
- },
- MemberExpression(node) {
- if (!rule || !propsUtil.isPropTypesDeclaration(node)) {
- return;
- }
- const component = utils.getRelatedComponent(node);
- if (!component || !node.parent.right) {
- return;
- }
- const right = node.parent.right;
- if (
- right.type === 'CallExpression'
- && propWrapperUtil.isPropWrapperFunction(
- context,
- context.getSourceCode().getText(right.callee)
- )
- ) {
- checkPropWrapperArguments(component.node, right.arguments);
- return;
- }
- validatePropNaming(component.node, node.parent.right.properties);
- },
- ObjectExpression(node) {
- if (!rule) {
- return;
- }
- // Search for the proptypes declaration
- node.properties.forEach((property) => {
- if (!propsUtil.isPropTypesDeclaration(property)) {
- return;
- }
- validatePropNaming(node, property.value.properties);
- });
- },
- TypeAlias(node) {
- // Cache all ObjectType annotations, we will check them at the end
- if (node.right.type === 'ObjectTypeAnnotation') {
- objectTypeAnnotations.set(node.id.name, node.right);
- }
- },
- TSTypeAliasDeclaration(node) {
- if (node.typeAnnotation.type === 'TSTypeLiteral') {
- objectTypeAnnotations.set(node.id.name, node.typeAnnotation);
- }
- },
- // eslint-disable-next-line object-shorthand
- 'Program:exit'() {
- if (!rule) {
- return;
- }
- const list = components.list();
- Object.keys(list).forEach((component) => {
- // If this is a functional component that uses a global type, check it
- if (
- (
- list[component].node.type === 'FunctionDeclaration'
- || list[component].node.type === 'ArrowFunctionExpression'
- )
- && list[component].node.params
- && list[component].node.params.length
- && list[component].node.params[0].typeAnnotation
- ) {
- const typeNode = list[component].node.params[0].typeAnnotation;
- const annotation = typeNode.typeAnnotation;
- let propType;
- if (annotation.type === 'GenericTypeAnnotation') {
- propType = objectTypeAnnotations.get(annotation.id.name);
- } else if (annotation.type === 'ObjectTypeAnnotation') {
- propType = annotation;
- } else if (annotation.type === 'TSTypeReference') {
- propType = objectTypeAnnotations.get(annotation.typeName.name);
- }
- if (propType) {
- validatePropNaming(
- list[component].node,
- propType.properties || propType.members
- );
- }
- }
- if (list[component].invalidProps && list[component].invalidProps.length > 0) {
- reportInvalidNaming(list[component]);
- }
- });
- // Reset cache
- objectTypeAnnotations.clear();
- }
- };
- })
- };
|