Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Add validateQuoteMarks rule, closes #106 #118

Merged
merged 1 commit into from
Dec 30, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,25 @@ Example configuration:
*/
"validateLineBreaks": "LF",

/*
Option: validateQuoteMarks
Possible values: "\"", "'", true
Requires all quote marks to be either the supplied value, or consistent if "true"

Valid example for mode "\"" or mode "true":

var x = "x";

Valid example for mode "'" or mode "true":

var x = 'x';

Invalid example for mode "true":

var x = "x", y = 'y';
*/
"validateQuoteMarks": "\"",

/*
Option: disallowKeywordsOnNewLine
Disallows placing keywords on a new line.
Expand Down
42 changes: 42 additions & 0 deletions lib/rules/validate-quote-marks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var assert = require('assert');

module.exports = function() {};

module.exports.prototype = {

configure: function(allowedQuoteMark) {
assert(
allowedQuoteMark === '"' || allowedQuoteMark === '\'' || allowedQuoteMark === true,
'validateQuoteMarks option requires \'"\', "\'", or boolean true'
);

this._allowedQuoteMark = allowedQuoteMark;
},

getOptionName: function () {
return 'validateQuoteMarks';
},

check: function(file, errors) {
var tokens = file.getTokens();
var allowedQuoteMark = this._allowedQuoteMark;

for (var i = 0, l = tokens.length; i < l; i++) {
var token = tokens[i];
if (token.type === 'String') {
if (allowedQuoteMark === true) {
allowedQuoteMark = token.value[0];
}

if (token.value[0] !== allowedQuoteMark) {
errors.add(
'Invalid quote mark found',
token.loc.start.line,
token.loc.start.column
);
}
}
}
}

};
1 change: 1 addition & 0 deletions lib/string-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ StringChecker.prototype = {
this.registerRule(new (require('./rules/disallow-keywords'))());
this.registerRule(new (require('./rules/disallow-multiple-line-breaks'))());
this.registerRule(new (require('./rules/validate-line-breaks'))());
this.registerRule(new (require('./rules/validate-quote-marks'))());
this.registerRule(new (require('./rules/require-keywords-on-new-line'))());
this.registerRule(new (require('./rules/disallow-keywords-on-new-line'))());
this.registerRule(new (require('./rules/require-line-feed-at-file-end'))());
Expand Down
77 changes: 77 additions & 0 deletions test/test.validate-quote-marks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
var Checker = require('../lib/checker');
var assert = require('assert');

describe('rules/validate-quote-marks', function() {
var checker;

beforeEach(function() {
checker = new Checker();
checker.registerDefaultRules();
});

describe('option value \' ', function() {
beforeEach(function() {
checker.configure({ validateQuoteMarks: '\'' });
});

it('should report double quotes in strings', function() {
assert(checker.checkString('var x = "x";').getErrorCount() === 1);
});

it('should not report single quotes in strings', function() {
assert(checker.checkString('var x = \'x\';').isEmpty());
});

it('should not report double quotes values in single quotes strings', function() {
assert(checker.checkString('var x = \'"x"\';').isEmpty());
});

it('should not report double quotes in comments', function() {
assert(checker.checkString('var x = \'x\'; /*"y"*/').isEmpty());
});
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikesherov IMO there should be tests checking for ' inside " & vice versa, like:

var x = "'x'";

with validateQuoteMarks: '"'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


describe('option value " ', function() {
beforeEach(function() {
checker.configure({ validateQuoteMarks: '"' });
});

it('should report single quotes in strings', function() {
assert(checker.checkString('var x = \'x\';').getErrorCount() === 1);
});

it('should not report double quotes in strings', function() {
assert(checker.checkString('var x = "x";').isEmpty());
});

it('should not report single quotes values in double quotes strings', function() {
assert(checker.checkString('var x = "\'x\'";').isEmpty());
});

it('should not report single quotes in comments', function() {
assert(checker.checkString('var x = "x"; /*\'y\'*/').isEmpty());
});
});

describe('option value true ', function() {
beforeEach(function() {
checker.configure({ validateQuoteMarks: true });
});

it('should report inconsistent quotes in strings', function() {
assert(checker.checkString('var x = \'x\', y = "y";').getErrorCount() === 1);
});

it('should not report consistent single quotes in strings', function() {
assert(checker.checkString('var x = \'x\', y = \'y\';').isEmpty());
});

it('should not report consistent double quotes in strings', function() {
assert(checker.checkString('var x = "x", y = "y";').isEmpty());
});

it('should not report inconsistent quotes in comments', function() {
assert(checker.checkString('var x = "x", y = "y"; /*\'y\'*/').isEmpty());
});
});
});