Repository: brenolf/polyjuice
Pull request #22 (2016-02-19 16:33:52)

Add SpaceBetweenKeywords rules
Add disallowSpaceBeforeKeywords, requireSpaceBeforeKeywords, disallowSpaceAfterKeywords and requireSpaceAfterKeywords.


Patch:
diff --git a/doc/JSCS.md b/doc/JSCS.md
index 408d79a..abc928b 100644
--- a/doc/JSCS.md
+++ b/doc/JSCS.md
@@ -13,7 +13,6 @@
 * `disallowPaddingNewLinesInObjects`
 * `disallowSpaceAfterBinaryOperators`
 * `disallowSpaceBeforeBinaryOperators`
-* `disallowSpaceBeforeKeywords`
 * `disallowSpaceBeforePostfixUnaryOperators`
 * `disallowSpaceBetweenArguments`
 * `disallowSpacesInConditionalExpression`
@@ -38,7 +37,6 @@
 * `requirePaddingNewLinesInObjects`
 * `requireShorthandArrowFunctions`
 * `requireSpaceBeforeBinaryOperators`
-* `requireSpaceBeforeKeywords`
 * `requireSpaceBeforePostfixUnaryOperators`
 * `requireSpaceBetweenArguments`
 * `requireSpacesInAnonymousFunctionExpression`
diff --git a/lib/dictionaries/jscs.js b/lib/dictionaries/jscs.js
index 70b8e19..486e694 100644
--- a/lib/dictionaries/jscs.js
+++ b/lib/dictionaries/jscs.js
@@ -120,35 +120,6 @@ module.exports = {
     }
   },
 
-  'disallowSpaceAfterKeywords': function (value) {
-    if (value === true) {
-      return {
-        'space-after-keywords': [2, 'never'],
-        'space-return-throw-case': 0
-      }
-    }
-
-    var rules = {}
-
-    var has = {
-      return: value.indexOf('return') >= 0,
-      throw: value.indexOf('throw') >= 0,
-      case: value.indexOf('case') >= 0
-    }
-
-    var rtc = has.return + has.throw + has.case
-
-    if (rtc > 0) {
-      rules['space-return-throw-case'] = 0
-    }
-
-    if (rtc < value.length) {
-      rules['space-after-keywords'] = [2, 'never']
-    }
-
-    return rules
-  },
-
   'disallowSpaceAfterObjectKeys': {
     name: 'key-spacing',
     truthy: function (value) {
@@ -440,35 +411,6 @@ module.exports = {
     truthy: 2
   },
 
-  'requireSpaceAfterKeywords': function (value) {
-    if (value === true) {
-      return {
-        'space-after-keywords': [2, 'always'],
-        'space-return-throw-case': 0
-      }
-    }
-
-    var rules = {}
-
-    var has = {
-      return: value.indexOf('return') >= 0,
-      throw: value.indexOf('throw') >= 0,
-      case: value.indexOf('case') >= 0
-    }
-
-    var rtc = has.return + has.throw + has.case
-
-    if (rtc > 0) {
-      rules['space-return-throw-case'] = 0
-    }
-
-    if (rtc < value.length) {
-      rules['space-after-keywords'] = [2, 'always']
-    }
-
-    return rules
-  },
-
   'requireSpaceAfterLineComment': {
     name: 'spaced-comment',
     truthy: [2, 'always']
@@ -702,5 +644,93 @@ module.exports = {
 
       return [2, mark]
     }
+  },
+
+  'disallowSpaceBeforeKeywords': {
+    name: 'keyword-spacing',
+    truthy: function (value) {
+      var rule = this.value || [2, {}]
+
+      var overrides = rule[1].overrides = (rule[1].overrides || {})
+
+      if (value === true) {
+        rule[1].before = false
+
+        return rule
+      }
+
+      value.forEach(function (keyword) {
+        overrides[keyword] = overrides[keyword] || {}
+        overrides[keyword].before = false
+      })
+
+      return rule
+    }
+  },
+
+  'requireSpaceBeforeKeywords': {
+    name: 'keyword-spacing',
+    truthy: function (value) {
+      var rule = this.value || [2, {}]
+
+      var overrides = rule[1].overrides = (rule[1].overrides || {})
+
+      if (value === true) {
+        rule[1].before = true
+
+        return rule
+      }
+
+      value.forEach(function (keyword) {
+        overrides[keyword] = overrides[keyword] || {}
+        overrides[keyword].before = true
+      })
+
+      return rule
+    }
+  },
+
+  'disallowSpaceAfterKeywords': {
+    name: 'keyword-spacing',
+    truthy: function (value) {
+      var rule = this.value || [2, {}]
+
+      var overrides = rule[1].overrides = (rule[1].overrides || {})
+
+      if (value === true) {
+        rule[1].after = false
+
+        return rule
+      }
+
+      value.forEach(function (keyword) {
+        overrides[keyword] = overrides[keyword] || {}
+        overrides[keyword].after = false
+      })
+
+      return rule
+    }
+  },
+
+  'requireSpaceAfterKeywords': {
+    name: 'keyword-spacing',
+    truthy: function (value) {
+      var rule = this.value || [2, {}]
+
+      var overrides = rule[1].overrides = (rule[1].overrides || {})
+
+      if (value === true) {
+        rule[1].after = true
+
+        return rule
+      }
+
+      value.forEach(function (keyword) {
+        overrides[keyword] = overrides[keyword] || {}
+        overrides[keyword].after = true
+      })
+
+      return rule
+    }
   }
 }


Test patch:
diff --git a/test/dictionaries/jscs.test.js b/test/dictionaries/jscs.test.js
index 8d5c4ed..6bf25ec 100644
--- a/test/dictionaries/jscs.test.js
+++ b/test/dictionaries/jscs.test.js
@@ -63,28 +63,6 @@ describe('jscs', function () {
     )
   })
 
-  it('converts disallowSpaceAfterKeywords correctly', function () {
-    var fn = getFn('disallowSpaceAfterKeywords')
-
-    expect(fn(true)).to.eql({
-      'space-after-keywords': [2, 'never'],
-      'space-return-throw-case': 0
-    })
-
-    expect(fn(['if', 'else'])).to.eql({
-      'space-after-keywords': [2, 'never']
-    })
-
-    expect(fn(['return', 'throw'])).to.eql({
-      'space-return-throw-case': 0
-    })
-
-    expect(fn(['if', 'return'])).to.eql({
-      'space-after-keywords': [2, 'never'],
-      'space-return-throw-case': 0
-    })
-  })
-
   it('converts disallowSpaceAfterObjectKeys correctly', function () {
     var fn = getFn('disallowSpaceAfterObjectKeys')
 
@@ -247,28 +225,6 @@ describe('jscs', function () {
     }])
   })
 
-  it('converts requireSpaceAfterKeywords correctly', function () {
-    var fn = getFn('requireSpaceAfterKeywords')
-
-    expect(fn(true)).to.eql({
-      'space-after-keywords': [2, 'always'],
-      'space-return-throw-case': 0
-    })
-
-    expect(fn(['if', 'else'])).to.eql({
-      'space-after-keywords': [2, 'always']
-    })
-
-    expect(fn(['return', 'throw'])).to.eql({
-      'space-return-throw-case': 0
-    })
-
-    expect(fn(['if', 'return'])).to.eql({
-      'space-after-keywords': [2, 'always'],
-      'space-return-throw-case': 0
-    })
-  })
-
   it('converts requireSpaceAfterObjectKeys correctly', function () {
     var fn = getFn('requireSpaceAfterObjectKeys')
     var fnBound = getFn(
@@ -438,4 +394,176 @@ describe('jscs', function () {
       [2, 'double']
     )
   })
+
+  it('converts disallowSpaceBeforeKeywords correctly', function () {
+    var fn = getFn('disallowSpaceBeforeKeywords')
+
+    var fnBound = getFn('disallowSpaceBeforeKeywords', [
+      2, {
+        overrides: {
+          'if': { after: false }
+        }
+      }
+    ])
+
+    expect(fn(true)).to.eql([2, { before: false, overrides: {} }])
+
+    expect(fnBound(true)).to.eql([2, {
+      before: false,
+      overrides: {
+        'if': { after: false }
+      }
+    }])
+
+    expect(fn([
+      'else', 'try', 'catch'
+    ])).to.eql([2, {
+      overrides: {
+        'else': { before: false },
+        'try': { before: false },
+        'catch': { before: false }
+      }
+    }])
+
+    expect(fnBound([
+      'else', 'try', 'catch'
+    ])).to.eql([2, {
+      before: false,
+      overrides: {
+        'else': { before: false },
+        'try': { before: false },
+        'catch': { before: false },
+        'if': { after: false }
+      }
+    }])
+  })
+
+  it('converts requireSpaceBeforeKeywords correctly', function () {
+    var fn = getFn('requireSpaceBeforeKeywords')
+
+    var fnBound = getFn('requireSpaceBeforeKeywords', [
+      2, {
+        overrides: {
+          'if': { after: false }
+        }
+      }
+    ])
+
+    expect(fn(true)).to.eql([2, { before: true, overrides: {} }])
+
+    expect(fnBound(true)).to.eql([2, {
+      before: true,
+      overrides: {
+        'if': { after: false }
+      }
+    }])
+
+    expect(fn([
+      'else', 'try', 'catch'
+    ])).to.eql([2, {
+      overrides: {
+        'else': { before: true },
+        'try': { before: true },
+        'catch': { before: true }
+      }
+    }])
+
+    expect(fnBound([
+      'else', 'try', 'catch'
+    ])).to.eql([2, {
+      before: true,
+      overrides: {
+        'else': { before: true },
+        'try': { before: true },
+        'catch': { before: true },
+        'if': { after: false }
+      }
+    }])
+  })
+
+  it('converts disallowSpaceAfterKeywords correctly', function () {
+    var fn = getFn('disallowSpaceAfterKeywords')
+
+    var fnBound = getFn('disallowSpaceAfterKeywords', [
+      2, {
+        overrides: {
+          'if': { after: true }
+        }
+      }
+    ])
+
+    expect(fn(true)).to.eql([2, { after: false, overrides: {} }])
+
+    expect(fnBound(true)).to.eql([2, {
+      after: false,
+      overrides: {
+        'if': { after: true }
+      }
+    }])
+
+    expect(fn([
+      'else', 'try', 'catch'
+    ])).to.eql([2, {
+      overrides: {
+        'else': { after: false },
+        'try': { after: false },
+        'catch': { after: false }
+      }
+    }])
+
+    expect(fnBound([
+      'else', 'try', 'catch'
+    ])).to.eql([2, {
+      after: false,
+      overrides: {
+        'else': { after: false },
+        'try': { after: false },
+        'catch': { after: false },
+        'if': { after: true }
+      }
+    }])
+  })
+
+  it('converts requireSpaceAfterKeywords correctly', function () {
+    var fn = getFn('requireSpaceAfterKeywords')
+
+    var fnBound = getFn('requireSpaceAfterKeywords', [
+      2, {
+        overrides: {
+          'if': { after: true }
+        }
+      }
+    ])
+
+    expect(fn(true)).to.eql([2, { after: true, overrides: {} }])
+
+    expect(fnBound(true)).to.eql([2, {
+      after: true,
+      overrides: {
+        'if': { after: true }
+      }
+    }])
+
+    expect(fn([
+      'else', 'try', 'catch'
+    ])).to.eql([2, {
+      overrides: {
+        'else': { after: true },
+        'try': { after: true },
+        'catch': { after: true }
+      }
+    }])
+
+    expect(fnBound([
+      'else', 'try', 'catch'
+    ])).to.eql([2, {
+      after: true,
+      overrides: {
+        'else': { after: true },
+        'try': { after: true },
+        'catch': { after: true },
+        'if': { after: true }
+      }
+    }])
+  })
 })