eslint-plugin-quintoandar
Advanced tools
Comparing version 1.14.0 to 1.14.1
{ | ||
"name": "eslint-plugin-quintoandar", | ||
"version": "1.14.0", | ||
"version": "1.14.1", | ||
"description": "An eslint-plugin for PWA-Tenants custom rules", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -152,2 +152,19 @@ <div align="center"> | ||
### No variables on message id | ||
The variable 'id' must be defined and should be a literal. | ||
This prevent us from generating messages with static code analysis. | ||
See more: https://guidelines.quintoandar.com.br/#/pwa/internationalization | ||
:warning: Do not use `id` as an object name otherwise some lint errors may occur. | ||
#### How to use it | ||
Just add the code below in your rules array: | ||
```js | ||
"quintoandar/no-var-message-id": 2, | ||
``` | ||
### No ThemeProvider import | ||
@@ -154,0 +171,0 @@ |
@@ -7,3 +7,3 @@ const defineMessagesFunctionName = 'defineMessages'; | ||
const reportText = ` | ||
Do not use variables on message id. | ||
The variable 'id' must be defined and should be a literal. | ||
This prevent us from generating messages with static code analysis. | ||
@@ -19,4 +19,28 @@ | ||
const hasMessageProps = (property) => property.value.type === objectExpressionType && property.value.properties; | ||
const isValueLiteral = (property) => property.value.type === literalType; | ||
const isValueLiteral = (property) => property && property.value.type === literalType; | ||
const hasNoProperties = property => property.value.properties === undefined; | ||
const searchLiteralInNode = (context, node, jsonObject) => { | ||
jsonObject.forEach((jsonNode) => { | ||
if (!isProperty(jsonNode) || !hasMessageProps(jsonNode)) { | ||
return; | ||
} | ||
const message = jsonNode.value.properties; | ||
const idField = message.find(x => x.key.name === 'id'); | ||
const defaultMessageField = message.find(x => x.key.name === 'defaultMessage'); | ||
if (idField || defaultMessageField) { | ||
if (!isValueLiteral(idField) || hasNoProperties(jsonNode)) { | ||
context.report({ | ||
node, | ||
message: reportText, | ||
}); | ||
} | ||
} else { | ||
searchLiteralInNode(context, node, message); | ||
} | ||
}); | ||
} | ||
module.exports = function noVarMessageId(context) { | ||
@@ -29,21 +53,5 @@ return { | ||
const jsonObject = node.arguments[0].properties; | ||
jsonObject.forEach((jsonNode) => { | ||
if (!isProperty(jsonNode) || !hasMessageProps(jsonNode)) { | ||
return; | ||
} | ||
const message = jsonNode.value.properties; | ||
const idField = message.find(x => x.key.name === 'id'); | ||
if (!isValueLiteral(idField)) { | ||
context.report({ | ||
node, | ||
message: reportText, | ||
}); | ||
} | ||
}); | ||
searchLiteralInNode(context, node, node.arguments[0].properties); | ||
} | ||
}; | ||
}; |
@@ -22,3 +22,3 @@ // ------------------------------------------------------------------------------ | ||
const reportText = ` | ||
Do not use variables on message id. | ||
The variable 'id' must be defined and should be a literal. | ||
This prevent us from generating messages with static code analysis. | ||
@@ -34,8 +34,13 @@ | ||
invalid: [ | ||
{ code: "export default defineMessages({ title: { id: `${test}`, defaultMessage: 'test invalid message' } })", errors } | ||
{ code: "export default defineMessages({ title: { id: `${test}`, defaultMessage: 'test invalid message' } })", errors }, | ||
{ code: "export default defineMessages({ subject: { title: { id: `${test}`, defaultMessage: 'test invalid message' } } })", errors}, | ||
{ code: "export default defineMessages({ title: { defaultMessage: 'message' }, subject: { title: { id: 'nested id', defaultMessage: 'nested message' }, title1: { title: { id: 'nested id', defaultMessage: 'nested message' } } } })", errors}, | ||
{ code: "export default defineMessages({ title: { id: { id: 'id', defaultMessage: 'message' } }, subject: { title: { id: 'nested id', defaultMessage: 'nested message' }, title1: { title: { id: 'nested id', defaultMessage: 'nested message' } } } })", errors}, | ||
], | ||
valid: [ | ||
{ code: "export default defineMessages({ title: { id: 'valid id', defaultMessage: 'test valid message' } })"}, | ||
{ code: "export default defineMessages({ subject: { title: { id: 'nested id', defaultMessage: 'nested message' } } })"}, | ||
{ code: "export default defineMessages({ title: { id: 'id', defaultMessage: 'message' }, subject: { title: { id: 'nested id', defaultMessage: 'nested message' } } })"}, | ||
{ code: "export default otherFunction({ title: { id: `${test}`, defaultMessage: 'test other function' } })"} | ||
], | ||
}); |
76171
1718
357