Skip to content

Commit

Permalink
Add eslint rule for dangling comma on objects and arrays which are
Browse files Browse the repository at this point in the history
multiline
  • Loading branch information
jhinch committed Dec 24, 2018
1 parent 0bff358 commit a654b1a
Show file tree
Hide file tree
Showing 16 changed files with 101 additions and 98 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ rules:
semi:
- error
- always
comma-dangle:
- error
- always-multiline
overrides:
- files:
- test/*.js
Expand Down
20 changes: 10 additions & 10 deletions bin/_cli/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,34 @@ const TABLE_CONFIG = {
0: { // Line number
alignment: 'right',
paddingLeft: 2,
paddingRight: 0
paddingRight: 0,
},
1: { // :
alignment: 'center',
paddingLeft: 0,
paddingRight: 0
paddingRight: 0,
},
2: { // Column number
alignment: 'left',
paddingLeft: 0,
paddingRight: 1
paddingRight: 1,
},
3: { // messageType
alignment: 'left',
paddingLeft: 0,
paddingRight: 1
paddingRight: 1,
},
4: { // message
alignment: 'left',
paddingLeft: 0,
paddingRight: 1
paddingRight: 1,
},
5: { // rule
alignment: 'left',
paddingLeft: 0,
paddingRight: 0
}
}
paddingRight: 0,
},
},
};

function execute(options) {
Expand Down Expand Up @@ -105,7 +105,7 @@ function outputResults(fileName, results) {
chalk.dim(pos.start.column),
chalk.red(type),
text,
chalk.dim(rule)
chalk.dim(rule),
]);
});
console.log(table(tableData, TABLE_CONFIG));
Expand All @@ -130,5 +130,5 @@ function main(args) {
}

module.exports = {
main
main,
};
2 changes: 1 addition & 1 deletion bin/_cli/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let {main} = require('./commands');

module.exports = {
main
main,
};
4 changes: 2 additions & 2 deletions bin/_cli/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const defaults = {
command: 'validate',
config: './.nginx-linter.json',
includes: ['/etc/nginx/*.conf', '/etc/nginx/**/*.conf'],
excludes: []
excludes: [],
};

function parse(args) {
Expand Down Expand Up @@ -53,5 +53,5 @@ function parse(args) {

module.exports = {
defaults,
parse
parse,
};
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let parser = require('./parser');

module.exports = {
parser
parser,
};
4 changes: 2 additions & 2 deletions lib/rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let builtins = [
require('./rule-if-is-evil'),
require('./rule-indentation'),
require('./rule-line-ending'),
require('./rule-strict-location')
require('./rule-strict-location'),
];

function run(root, options) {
Expand All @@ -13,5 +13,5 @@ function run(root, options) {

module.exports = {
builtins,
run
run,
};
2 changes: 1 addition & 1 deletion lib/rules/rule-if-is-evil.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,5 @@ function checkParameter(parameterNode, errors, state) {
module.exports = {
name: 'if-is-evil',
default: 'mostly',
invoke
invoke,
};
2 changes: 1 addition & 1 deletion lib/rules/rule-indentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ module.exports = {
break;
}
return state;
}
},
};
2 changes: 1 addition & 1 deletion lib/rules/rule-line-ending.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ module.exports = {
errors.push(`Expected ${expectedType}, found ${actualType}`);
}
}
}
},
};
2 changes: 1 addition & 1 deletion lib/rules/rule-strict-location.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ module.exports = {
}
}
return state;
}
},
};
4 changes: 2 additions & 2 deletions lib/rules/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function runRulesForNode(node, rules, results, optionsStack, state) {
pos: node.pos,
rule: rule.name,
type: 'error',
text: message
text: message,
};
}).forEach(result => results.push(result));
}
Expand All @@ -83,5 +83,5 @@ function runRulesForNode(node, rules, results, optionsStack, state) {

module.exports = {
defaults,
runRules
runRules,
};
64 changes: 32 additions & 32 deletions test/parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ let assert = require('assert');
let parser = require('../lib/parser');
let fs = require('fs');

function punctuation(value) {
return { type: 'punctuation', text: value };
function punctuation(text) {
return { type: 'punctuation', text };
}

function parameter(value) {
return { type: 'parameter', text: value };
function parameter(text) {
return { type: 'parameter', text };
}

function directive(name, parameters, body) {
Expand All @@ -21,9 +21,9 @@ function directive(name, parameters, body) {
parameters = parameters.map(parameter);
return {
type: 'directive',
name: name,
parameters: parameters,
body: body
name,
parameters,
body,
};
}

Expand All @@ -48,21 +48,21 @@ const TEST_CONFIGS = {
directive('server', null, [
directive('listen', ['80']),
directive('location', ['=', '/ok'], [
directive('return', ['200'])
])
])
])
directive('return', ['200']),
]),
]),
]),
],
'single-quotes.conf': [
directive('events', null, []),
directive('http', null, [
directive('server', null, [
directive('listen', ['80']),
directive('location', ['=', '\'/o\\\'k\''], [
directive('return', ['200'])
])
])
])
directive('return', ['200']),
]),
]),
]),
],
'if-is-evil.conf': [
directive('events', null, []),
Expand All @@ -71,47 +71,47 @@ const TEST_CONFIGS = {
directive('listen', ['80']),
directive('location', ['=', '/ok'], [
directive('if', ['($request_method', '=', 'POST)'], [
directive('return', ['405'])
directive('return', ['405']),
]),
directive('return', ['200'])
directive('return', ['200']),
]),
directive('location', ['=', '/ok-but-bad'], [
directive('if', ['($request_method', '=', 'POST)'], [
directive('return', ['405'])
directive('return', ['405']),
]),
directive('return', ['200'])
directive('return', ['200']),
]),
directive('location', ['=', '/rewrite-from'], [
directive('if', ['($request_method', '=', 'POST)'], [
directive('rewrite', ['^', '/rewrite-to', 'last'])
directive('rewrite', ['^', '/rewrite-to', 'last']),
]),
directive('return', ['200'])
directive('return', ['200']),
]),
directive('location', ['=', '/rewrite-to'], [
directive('return', ['200'])
directive('return', ['200']),
]),
directive('location', ['=', '/rewrite-bad-but-ok'], [
directive('if', ['($request_method', '=', 'POST)'], [
directive('rewrite', ['^', '/rewrite-to', 'break'])
directive('rewrite', ['^', '/rewrite-to', 'break']),
]),
directive('return', ['200'])
directive('return', ['200']),
]),
directive('location', ['=', '/rewrite-bad'], [
directive('if', ['($request_method', '=', 'POST)'], [
directive('rewrite', ['^', '/rewrite-to', 'break'])
directive('rewrite', ['^', '/rewrite-to', 'break']),
]),
directive('return', ['200'])
directive('return', ['200']),
]),
directive('location', ['/crash'], [
directive('set', ['$true', '1']),
directive('if', ['($true)'], [
directive('proxy_pass', ['http://127.0.0.1:8080/'])
directive('proxy_pass', ['http://127.0.0.1:8080/']),
]),
directive('if', ['($true)'], [])
])
])
])
]
directive('if', ['($true)'], []),
]),
]),
]),
],
};

describe('parser', () => {
Expand Down
20 changes: 10 additions & 10 deletions test/rules/rule-if-is-evil.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ location / {
type: 'error',
pos: {
start: { column: 5, line: 3, offset: 18 },
end: { column: 6, line: 5, offset: 61 }
end: { column: 6, line: 5, offset: 61 },
},
}
},
]),
testConfig('simple if with return with always with comment', 'always', `
location / {
Expand All @@ -48,28 +48,28 @@ location / {
text: 'if is evil and not allowed',
pos: {
start: { column: 13, line: 19, offset: 353 },
end: { column: 14, line: 21, offset: 424 }
}
end: { column: 14, line: 21, offset: 424 },
},
},
{
rule: 'if-is-evil',
type: 'error',
text: 'A \'rewrite\' within an \'if\' must use the \'last\' flag, found \'break\'',
pos: {
start: { column: 39, line: 46, offset: 1033 },
end: { column: 44, line: 46, offset: 1038 }
}
end: { column: 44, line: 46, offset: 1038 },
},
},
{
rule: 'if-is-evil',
type: 'error',
text: 'Only a \'rewrite\' or \'return\' is allowed within an \'if\', found \'proxy_pass\'',
pos: {
start: { column: 17, line: 56, offset: 1218 },
end: { column: 51, line: 56, offset: 1252 }
}
}
])
end: { column: 51, line: 56, offset: 1252 },
},
},
]),
];

describe('rules/if-is-evil', () => {
Expand Down
26 changes: 13 additions & 13 deletions test/rules/rule-indentation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ location / {
type: 'error',
pos: {
start: { column: 5, line: 2, offset: 17 },
end: { column: 16, line: 2, offset: 28 }
}
}
end: { column: 16, line: 2, offset: 28 },
},
},
]),
testConfig('4 spaces', 4, 'location / {\n # OK\n return 200;\n}', []),
testConfig('2 spaces instead of 4', 4, 'location / {\n return 200;\n}', [
Expand All @@ -38,9 +38,9 @@ location / {
type: 'error',
pos: {
start: { column: 3, line: 2, offset: 15 },
end: { column: 14, line: 2, offset: 26 }
}
}
end: { column: 14, line: 2, offset: 26 },
},
},
]),
testConfig('tabs', 'tab', 'location / {\n\t# OK\n\treturn 200;\n}', []),
testConfig('2 spaces instead of tabs', 'tab', 'location / {\n return 200;\n}', [
Expand All @@ -50,9 +50,9 @@ location / {
type: 'error',
pos: {
start: { column: 3, line: 2, offset: 15 },
end: { column: 14, line: 2, offset: 26 }
}
}
end: { column: 14, line: 2, offset: 26 },
},
},
]),
testConfig('2 spaces instead of tabs', 'tab', 'location / {\n \treturn 200;\n}', [
{
Expand All @@ -61,10 +61,10 @@ location / {
type: 'error',
pos: {
start: { column: 3, line: 2, offset: 15 },
end: { column: 14, line: 2, offset: 26 }
}
}
])
end: { column: 14, line: 2, offset: 26 },
},
},
]),
];

describe('rules/indentation', () => {
Expand Down

0 comments on commit a654b1a

Please sign in to comment.