Skip to content

Commit

Permalink
πŸ— Fix linter when using dict({...spread}) (#32586)
Browse files Browse the repository at this point in the history
My linting/formatting environment was not working for a particular file until I realized the linter failed when encountering it:

```
[09:23:26] TypeError in plugin "gulp-eslint"
Message:
    Cannot read property 'raw' of undefined
Occurred while linting file-abc.js:123
```

This is caused by lint plugin `local/dict-string-keys` [ailing when using a spread operator inside `dict()`:

https://astexplorer.net/#/gist/3a59548ab9a34ade0f6337b74a5c86db/e070cc191a26fa6e0358a2f5a20218fbb154176f

```js
dict({...spreadOperatorBreaksLinter});
```

We likely want to forbid this, which this change accomplishes.

```js
// Found: idKeysAreNotOkay.The Object Literal Expression passed into `dict` must only contain string keyed properties. (at 2:7)
   dict({idKeysAreNotOkay: true});
// ------^

// Found: spreadOperatorIsNotOkay. The Object Literal Expression passed into `dict` must only contain string keyed properties. (at 5:7)
   dict({...spreadOperatorIsNotOkay});
// ------^
```
  • Loading branch information
alanorozco committed Feb 10, 2021
1 parent 614dd77 commit d29f2e5
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions build-system/eslint-rules/dict-string-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,18 @@ module.exports = function (context) {
function checkNode(node, context) {
if (node.type === 'ObjectExpression') {
node.properties.forEach(function (prop) {
if (!prop.key.raw && !prop.computed) {
if (!prop.key || (!prop.key.raw && !prop.computed)) {
const {name = `[${prop.type}]`} = prop.key || prop.argument || {};
context.report({
node: prop,
message:
'Found: ' +
prop.key.name +
'. The keys of the Object ' +
'Literal Expression passed into `dict` must have string keys.',
`Found: ${name}.` +
'The Object Literal Expression passed into `dict` must only contain string keyed properties.',
});
}
checkNode(prop.value, context);
if (prop.value) {
checkNode(prop.value, context);
}
});
} else if (node.type === 'ArrayExpression') {
node.elements.forEach(function (elem) {
Expand Down

0 comments on commit d29f2e5

Please sign in to comment.