Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Space after keywords superset rule (nested boolean properties) #182

Closed
wants to merge 3 commits into from

2 participants

Nick Schonning Mike Sherov
Nick Schonning

DO NOT MERGE

Alternate format to #181. Sample aggregate rule example from #136

"spaceAfterKeywords": {
  "if": true,
  "return": false
}

TODO:

  • How to map old rule back in given current export pattern

/cc @markelog

Nick Schonning

Mapped back the old rules to the new by requiring the new rule and using an apply. Some additional configuration re-writing might be required for other rules.

Nick Schonning nschonni referenced this pull request
Closed

JSCS version 2.0 #303

Mike Sherov
Owner

@nschonni thanks again for doing this work way back when. We're going to take config in a new direction, so closing this for now. Once a new issue is published for it, we'll ping you so you can help us bikeshed :-)

Mike Sherov mikesherov closed this
Nick Schonning nschonni deleted the branch
Nick Schonning nschonni referenced this pull request
Open

New format attempt #698

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 17, 2014
  1. Nick Schonning

    spaceAfterKeywords: combine required and disallow

    nschonni authored
    Use boolean properties for nested rules
Commits on Jan 19, 2014
  1. Nick Schonning
  2. Nick Schonning
This page is out of date. Refresh to see the latest.
42 README.md
View
@@ -67,9 +67,47 @@ if (x) {
if (x) x++;
```
+### spaceAfterKeywords
+
+Require or disallow a space after certain keywords.
+
+Type: `Object`
+
+Values: Keyword object keys with boolean values. `true` enforces the space and `false` disallows.
+
+#### Example
+
+```js
+"spaceAfterKeywords": {
+ "if": false,
+ "else": true,
+ "for": true,
+ "while": true,
+ "do": true,
+ "switch": true,
+ "return": true,
+ "try": true,
+ "catch": true
+}
+```
+
+##### Valid `true`
+
+```js
+return true;
+```
+
+##### Valid `false`
+
+```js
+if(x) {
+ x++;
+}
+```
+
### requireSpaceAfterKeywords
-Requires space after keyword.
+DEPRECIATED: Replaced by [spaceAfterKeywords](spaceAfterKeywords). Requires space after keyword.
Type: `Array`
@@ -107,7 +145,7 @@ if(x) {
### disallowSpaceAfterKeywords
-Disallows space after keyword.
+DEPRECIATED: Replaced by [spaceAfterKeywords](spaceAfterKeywords). Disallows space after keyword.
Type: `Array`
19 lib/rules/disallow-space-after-keywords.js
View
@@ -1,4 +1,5 @@
var assert = require('assert');
+var spaceAfterKeywords = require('./space-after-keywords');
module.exports = function() {};
@@ -8,7 +9,7 @@ module.exports.prototype = {
assert(Array.isArray(keywords), 'disallowSpaceAfterKeywords option requires array value');
this._keywordIndex = {};
for (var i = 0, l = keywords.length; i < l; i++) {
- this._keywordIndex[keywords[i]] = true;
+ this._keywordIndex[keywords[i]] = false;
}
},
@@ -17,20 +18,8 @@ module.exports.prototype = {
},
check: function(file, errors) {
- var keywordIndex = this._keywordIndex;
-
- file.iterateTokensByType('Keyword', function(token, i, tokens) {
- if (keywordIndex[token.value]) {
- var nextToken = tokens[i + 1];
- if (nextToken && nextToken.range[0] !== token.range[1]) {
- errors.add(
- 'Illegal space after `' + token.value + '` keyword',
- nextToken.loc.start.line,
- nextToken.loc.start.column
- );
- }
- }
- });
+ /* jshint unused: false */
+ spaceAfterKeywords.prototype.check.apply(this, arguments);
}
};
19 lib/rules/require-space-after-keywords.js
View
@@ -1,4 +1,5 @@
var assert = require('assert');
+var spaceAfterKeywords = require('./space-after-keywords');
module.exports = function() {};
@@ -17,22 +18,8 @@ module.exports.prototype = {
},
check: function(file, errors) {
- var keywordIndex = this._keywordIndex;
-
- file.iterateTokensByType('Keyword', function(token, i, tokens) {
- if (keywordIndex[token.value]) {
- var nextToken = tokens[i + 1];
- if (nextToken && nextToken.range[0] === token.range[1]) {
- if (nextToken.type !== 'Punctuator' || nextToken.value !== ';') {
- errors.add(
- 'Missing space after `' + token.value + '` keyword',
- nextToken.loc.start.line,
- nextToken.loc.start.column
- );
- }
- }
- }
- });
+ /* jshint unused: false */
+ spaceAfterKeywords.prototype.check.apply(this, arguments);
}
};
59 lib/rules/space-after-keywords.js
View
@@ -0,0 +1,59 @@
+var assert = require('assert');
+
+module.exports = function() {};
+
+module.exports.prototype = {
+
+ configure: function(options) {
+ var value, keyword;
+
+ assert(
+ typeof options === 'object',
+ 'spaceAfterKeywords option must be an object'
+ );
+
+ this._keywordIndex = {};
+
+ for (keyword in options) {
+ if (options.hasOwnProperty(keyword)) {
+ value = options[keyword];
+ assert(typeof value === 'boolean', 'spaceAfterKeywords values must be boolean values');
+ this._keywordIndex[keyword] = value;
+ }
+ }
+
+ },
+
+ getOptionName: function () {
+ return 'spaceAfterKeywords';
+ },
+
+ check: function(file, errors) {
+ var keywordIndex = this._keywordIndex;
+
+ file.iterateTokensByType('Keyword', function(token, i, tokens) {
+ var nextToken = tokens[i + 1];
+ var value = typeof keywordIndex[token.value] === 'undefined' ? null : keywordIndex[token.value];
+ if (value === true) {
+ if (nextToken && nextToken.range[0] === token.range[1]) {
+ if (nextToken.type !== 'Punctuator' || nextToken.value !== ';') {
+ errors.add(
+ 'Missing space after `' + token.value + '` keyword',
+ nextToken.loc.start.line,
+ nextToken.loc.start.column
+ );
+ }
+ }
+ } else if (value === false) {
+ if (nextToken && nextToken.range[0] !== token.range[1]) {
+ errors.add(
+ 'Illegal space after `' + token.value + '` keyword',
+ nextToken.loc.start.line,
+ nextToken.loc.start.column
+ );
+ }
+ }
+ });
+ }
+
+};
2  lib/string-checker.js
View
@@ -96,6 +96,8 @@ StringChecker.prototype = {
this.registerRule(new (require('./rules/safe-context-keyword'))());
this.registerRule(new (require('./rules/require-dot-notation'))());
+
+ this.registerRule(new (require('./rules/space-after-keywords'))());
},
/**
31 test/test.space-after-keywords.js
View
@@ -0,0 +1,31 @@
+var Checker = require('../lib/checker');
+var assert = require('assert');
+
+describe('rules/space-after-keywords', function() {
+ var checker;
+ beforeEach(function() {
+ checker = new Checker();
+ checker.registerDefaultRules();
+ });
+ it('should report illegal space after keyword', function() {
+ checker.configure({ spaceAfterKeywords: { 'if': false }});
+ assert(checker.checkString('if (x) { x++; }').getErrorCount() === 1);
+ });
+ it('should not report space after keyword', function() {
+ checker.configure({ spaceAfterKeywords: { 'if': false }});
+ assert(checker.checkString('if(x) { x++; }').isEmpty());
+ });
+
+ it('should report missing space after keyword', function() {
+ checker.configure({ spaceAfterKeywords: { 'if': true }});
+ assert(checker.checkString('if(x) { x++; }').getErrorCount() === 1);
+ });
+ it('should not report space after keyword', function() {
+ checker.configure({ spaceAfterKeywords: { 'if': true }});
+ assert(checker.checkString('if (x) { x++; }').isEmpty());
+ });
+ it('should not report semicolon after keyword', function() {
+ checker.configure({ spaceAfterKeywords: { 'return': true }});
+ assert(checker.checkString('var x = function () { return; }').isEmpty());
+ });
+});
Something went wrong with that request. Please try again.