Skip to content

Commit 1d9c3fe

Browse files
authored
Merge pull request #3 from IoraHealth/zel-find-object-literals-with-dot-keys
check for object keys with dots in them
2 parents 9e1c336 + 7225103 commit 1d9c3fe

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Check if an object literal has any keynames containing a dot. (object-keynames-with-dots)
2+
3+
This rule was written after a bug surfaced in Ember 3.8 where the "each-in" function got screwed up if an object literal contained a key with a string literal that has a dot, such as `{"x.y": 3}`. This is a very simple check to identify any instances of such notation.
4+
5+
6+
## Rule Details
7+
8+
This rule aims to...
9+
10+
Examples of **incorrect** code for this rule:
11+
12+
```js
13+
14+
const OPTIONS = {
15+
"hello.world": "foo bar"
16+
};
17+
18+
```
19+
20+
Examples of **correct** code for this rule:
21+
22+
```js
23+
24+
const OPTIONS = {
25+
"hello": {
26+
"world": "foo bar"
27+
}
28+
};
29+
30+
```
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @fileoverview Check if an object literal has any keynames containing a dot.
3+
* @author Steve Zelaznik
4+
*/
5+
'use strict';
6+
7+
//------------------------------------------------------------------------------
8+
// Rule Definition
9+
//------------------------------------------------------------------------------
10+
11+
const looksLike = require("../helpers/looks-like.js");
12+
13+
module.exports = {
14+
meta: {
15+
docs: {
16+
description:
17+
'Check if an object literal has any keynames containing a dot.',
18+
recommended: false
19+
},
20+
fixable: null, // or "code" or "whitespace"
21+
schema: [
22+
// fill in your schema
23+
]
24+
},
25+
26+
create: function(context) {
27+
return {
28+
Property(node) {
29+
const isMatch = looksLike(node, {
30+
key: {
31+
type: 'Literal',
32+
value(v) {
33+
return (typeof v === 'string') && v.indexOf('.') > -1
34+
}
35+
}
36+
});
37+
38+
if (!isMatch) {
39+
return;
40+
}
41+
42+
context.report(node, 'Cannot use a string key name with a "." in it');
43+
}
44+
};
45+
}
46+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-plugin-chirp-test-rules",
3-
"version": "1.5.1",
3+
"version": "1.6.1",
44
"description": "A set of custom lint rules for apps created by Iora Health",
55
"keywords": [
66
"eslint",
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @fileoverview Check if an object literal has any keynames containing a dot.
3+
* @author Steve Zelaznik
4+
*/
5+
'use strict';
6+
7+
//------------------------------------------------------------------------------
8+
// Requirements
9+
//------------------------------------------------------------------------------
10+
11+
var rule = require('../../../lib/rules/object-keynames-with-dots'),
12+
RuleTester = require('eslint').RuleTester;
13+
14+
RuleTester.setDefaultConfig({
15+
parserOptions: {
16+
ecmaVersion: 2017,
17+
sourceType: 'module'
18+
}
19+
});
20+
21+
var ruleTester = new RuleTester();
22+
ruleTester.run('object-keynames-with-dots', rule, {
23+
valid: [
24+
"const data = {x: 3, y: 4, z: 5}"
25+
],
26+
27+
invalid: [
28+
{
29+
code: 'const data = {"x.y": 3}',
30+
errors: [
31+
{
32+
message: 'Cannot use a string key name with a "." in it'
33+
}
34+
]
35+
}
36+
]
37+
});

0 commit comments

Comments
 (0)