1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | var assert = require('assert');
|
28 | var cst = require('cst');
|
29 |
|
30 | module.exports = function() { };
|
31 |
|
32 | module.exports.prototype = {
|
33 |
|
34 | configure: function(options) {
|
35 | assert(
|
36 | options === true,
|
37 | this.getOptionName() + ' option requires a true value or should be removed'
|
38 | );
|
39 | },
|
40 |
|
41 | getOptionName: function() {
|
42 | return 'requireQuotedKeysInObjects';
|
43 | },
|
44 |
|
45 | check: function(file, errors) {
|
46 | file.iterateNodesByType('ObjectExpression', function(node) {
|
47 | node.properties.forEach(function(property) {
|
48 | if (
|
49 | property.type === 'ObjectMethod' &&
|
50 | (property.kind === 'get' || property.kind === 'set')
|
51 | ) {
|
52 | return;
|
53 | }
|
54 |
|
55 | if (property.shorthand || property.method ||
|
56 | node.type === 'SpreadProperty') {
|
57 | return;
|
58 | }
|
59 |
|
60 | var key = property.key;
|
61 | if (key && !(typeof key.value === 'string' && key.type.indexOf('Literal') > -1)) {
|
62 | errors.cast({
|
63 | message: 'Object key without surrounding quotes',
|
64 | element: property.firstChild
|
65 | });
|
66 | }
|
67 | });
|
68 | });
|
69 | },
|
70 |
|
71 | _fix: function(file, error) {
|
72 | var element = error.element.firstChild;
|
73 | var newElem = new cst.Token(element.type, '"' + element.getSourceCode() + '"');
|
74 |
|
75 | element.parentElement.replaceChild(
|
76 | newElem,
|
77 | element
|
78 | );
|
79 | }
|
80 | };
|