Created
December 10, 2018 22:46
-
-
Save smashercosmo/6d07950ad7177e5dc4371b6cb5bb50e6 to your computer and use it in GitHub Desktop.
redux action eslint rule
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hasNotOnlyTypePayloadAndMetaProps(node) { | |
return node.properties.some( | |
prop => | |
prop.key.name !== 'type' && | |
prop.key.name !== 'payload' && | |
prop.key.name !== 'meta', | |
) | |
} | |
function isReduxActionTypePropertyValue(value) { | |
return /^([A-Z]+_)+[A-Z]+$/.test(value) | |
} | |
function isReduxActionTypeProperty(property) { | |
return ( | |
property.type === 'Property' && | |
property.key.name === 'type' && | |
((property.value.type === 'Identifier' && | |
isReduxActionTypePropertyValue(property.value.name)) || | |
(property.value.type === 'Literal' && | |
isReduxActionTypePropertyValue(property.value.value))) | |
) | |
} | |
function isReduxAction(node) { | |
return ( | |
node && | |
Array.isArray(node.properties) && | |
node.properties.length >= 1 && | |
node.properties.some(isReduxActionTypeProperty) | |
) | |
} | |
module.exports = function reduxActionRule(context) { | |
return { | |
ObjectExpression(node) { | |
if (isReduxAction(node) && hasNotOnlyTypePayloadAndMetaProps(node)) { | |
context.report({ | |
node, | |
message: 'Actions should only have "type", "payload" or "meta" props', | |
}) | |
} | |
}, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment