Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Clone in Desktop Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

177 lines (154 sloc) 7.62 KB
var Checker = require('../../../lib/checker');
var expect = require('chai').expect;
var reportAndFix = require('../../lib/assertHelpers').reportAndFix;
describe('rules/require-trailing-comma', function() {
var rules = { requireTrailingComma: true };
var checker;
beforeEach(function() {
checker = new Checker();
checker.registerDefaultRules();
});
reportAndFix({
name: 'missing comma in object literal',
rules: rules,
errors: 1,
input: 'var t = {\n\ta: 1,\n\tb: 2\n};',
output: 'var t = {\n\ta: 1,\n\tb: 2,\n};'
});
reportAndFix({
name: 'missing comma in object literal without newline char',
rules: rules,
errors: 1,
input: 'var t = {\n\ta: 1,\n\tb: 2};',
output: 'var t = {\n\ta: 1,\n\tb: 2,};'
});
reportAndFix({
name: 'missing comma in object literal with additional space',
rules: rules,
errors: 1,
input: 'var t = {\n\ta: 1,\n\tb: 2 };',
output: 'var t = {\n\ta: 1,\n\tb: 2, };'
});
describe('option value true', function() {
beforeEach(function() {
checker.configure({ requireTrailingComma: true });
});
it('should allow object or array initialization without comma', function() {
expect(checker.checkString('var x = {}')).to.have.no.errors();
expect(checker.checkString('var x = { }')).to.have.no.errors();
expect(checker.checkString('var x = []')).to.have.no.errors();
expect(checker.checkString('var x = [ ]')).to.have.no.errors();
});
it('should report trailing comma required in object literal', function() {
expect(checker.checkString('var x = {a: "a", b: "b",}')).to.have.no.errors();
expect(checker.checkString('var x = {a: "a", b: "b",\n}')).to.have.no.errors();
expect(checker.checkString('var x = {a: "a", b: "b"}'))
.to.have.one.validation.error.from('requireTrailingComma');
expect(checker.checkString('var x = {a: "a", b: "b"\n}'))
.to.have.one.validation.error.from('requireTrailingComma');
expect(checker.checkString('function foo() {\nreturn {a: "a"};\n}'))
.to.have.one.validation.error.from('requireTrailingComma');
});
it('should report trailing comma required in array', function() {
expect(checker.checkString('var x = [1, 2,]')).to.have.no.errors();
expect(checker.checkString('var x = [1, 2,\n]')).to.have.no.errors();
expect(checker.checkString('var x = [1, 2]')).to.have.one.validation.error.from('requireTrailingComma');
expect(checker.checkString('var x = [1, 2\n]')).to.have.one.validation.error.from('requireTrailingComma');
expect(checker.checkString('function foo() {\nreturn [1, 2];\n}'))
.to.have.one.validation.error.from('requireTrailingComma');
});
it('should not report block scopes (#368)', function() {
expect(checker.checkString('if(true) {\nconsole.log(\'Hello World\');\n}')).to.have.no.errors();
expect(checker.checkString('function add(a, b) {\nreturn a + b;\n}')).to.have.no.errors();
});
it('should not report array access (#368)', function() {
expect(checker.checkString('var foo = [\'Hello World\',\n];\nvar bar = foo[0];')).to.have.no.errors();
});
it('should report right location for no trailing comma in object (#1018)', function() {
var errs = checker.checkString('var obj = {\n foo: "foo"\n};').getErrorList();
expect(errs[0].line + ':' + errs[0].column).to.equal('2:14');
});
it('should report right location for no trailing comma in array (#1018)', function() {
var errs = checker.checkString('var arr = [\n \'foo\'\n];').getErrorList();
expect(errs[0].line + ':' + errs[0].column).to.equal('2:9');
});
});
describe('ignoreSingleValue', function() {
beforeEach(function() {
checker.configure({
requireTrailingComma: {
ignoreSingleValue: true
}
});
});
it('should allow object or array with single property', function() {
expect(checker.checkString('var x = [1]')).to.have.no.errors();
expect(checker.checkString('var x = {a: "a"}')).to.have.no.errors();
});
it('should report trailing comma required for two or more elements in an array', function() {
expect(checker.checkString('var x = [1, 2]')).to.have.one.validation.error.from('requireTrailingComma');
expect(checker.checkString('var x = [1, 2, 3]')).to.have.one.validation.error.from('requireTrailingComma');
});
it('should report trailing comma required for two or more properties in an object', function() {
expect(checker.checkString('var x = {a: "a", b: "b"}'))
.to.have.one.validation.error.from('requireTrailingComma');
expect(checker.checkString('var x = {a: "a", b: "b", c: "c"}'))
.to.have.one.validation.error.from('requireTrailingComma');
});
it('should not report empty array or object', function() {
expect(checker.checkString('var x = []')).to.have.no.errors();
expect(checker.checkString('var x = {}')).to.have.no.errors();
});
});
describe('ignoreSingleLine', function() {
beforeEach(function() {
checker.configure({
requireTrailingComma: {
ignoreSingleLine: true
}
});
});
it('should allow object or array on single line with single property', function() {
expect(checker.checkString('var x = [1,]')).to.have.no.errors();
expect(checker.checkString('var x = [1]')).to.have.no.errors();
expect(checker.checkString('var x = {a: "a",}')).to.have.no.errors();
expect(checker.checkString('var x = {a: "a"}')).to.have.no.errors();
});
it('should allow object or array on single line with multiple properties', function() {
expect(checker.checkString('var x = [1, 2,]')).to.have.no.errors();
expect(checker.checkString('var x = [1, 2]')).to.have.no.errors();
expect(checker.checkString('var x = {a: "a", b: "b",}')).to.have.no.errors();
expect(checker.checkString('var x = {a: "a", b: "b"}')).to.have.no.errors();
});
it('should report trailing comma required for object or array on multiple lines', function() {
expect(checker.checkString(
'var x = [\n' +
'1,\n' +
'2,\n' +
'3,\n' +
']'
)).to.have.no.errors();
expect(checker.checkString(
'var x = [\n' +
'1,\n' +
'2,\n' +
'3\n' +
']'
)).to.have.one.validation.error.from('requireTrailingComma');
expect(checker.checkString(
'var x = {\n' +
'a: "a",\n' +
'b: "b",\n' +
'c: "c",\n' +
'}'
)).to.have.no.errors();
expect(checker.checkString(
'var x = {\n' +
'a: "a",\n' +
'b: "b",\n' +
'c: "c"\n' +
'}'
)).to.have.one.validation.error.from('requireTrailingComma');
});
});
});
Jump to Line
Something went wrong with that request. Please try again.