Skip to content
This repository has been archived by the owner on Oct 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #316 from clutchski/prefer_english_operator
Browse files Browse the repository at this point in the history
Fixes #311 #106. Copied parakeety/coffeelint-prefer-english-operator
  • Loading branch information
AsaAyers committed Aug 19, 2014
2 parents 40d0e52 + 8f0e462 commit d028c4e
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 13 deletions.
3 changes: 3 additions & 0 deletions generated_coffeelint.json
Expand Up @@ -97,6 +97,9 @@
"non_empty_constructor_needs_parens": {
"level": "ignore"
},
"prefer_english_operator": {
"level": "ignore"
},
"space_operators": {
"level": "ignore"
}
Expand Down
1 change: 1 addition & 0 deletions src/coffeelint.coffee
Expand Up @@ -160,6 +160,7 @@ coffeelint.registerRule(
require './rules/no_interpolation_in_single_quotes.coffee'
)
coffeelint.registerRule require './rules/no_empty_functions.coffee'
coffeelint.registerRule require './rules/prefer_english_operator.coffee'

hasSyntaxError = (source) ->
try
Expand Down
35 changes: 35 additions & 0 deletions src/rules/prefer_english_operator.coffee
@@ -0,0 +1,35 @@

module.exports = class RuleProcessor
rule:
name: 'prefer_english_operator'
description: '''
This rule prohibits &&, ||, ==, != and !.
Use and, or, is, isnt, and not instead.
!! for converting to a boolean is ignored.
'''
level: 'ignore'
message: 'Don\'t use &&, ||, ==, !=, or !'

tokens: ['COMPARE', 'UNARY_MATH', 'LOGIC']
lintToken: (token, tokenApi) ->
# Compare the actual token with the lexed token.
{ first_column, last_column } = token[2]
line = tokenApi.lines[tokenApi.lineNumber]
actual_token = line[first_column..last_column]
context =
switch actual_token
when '==' then 'Replace "==" with "is"'
when '!=' then 'Replace "!=" with "isnt"'
when '||' then 'Replace "||" with "or"'
when '&&' then 'Replace "&&" with "and"'
when '!'
# I think !!something is acceptable for coorcing a variable
# into a boolean. The alternative seems very awkward
# `not not something`?
if tokenApi.peek(1)?[0] isnt 'UNARY_MATH' and
tokenApi.peek(-1)?[0] isnt 'UNARY_MATH'
'Replace "!" with "not"'
else undefined

if context?
{ context }
32 changes: 19 additions & 13 deletions test/test_1.5.0_plus.coffee
Expand Up @@ -2,21 +2,27 @@ path = require 'path'
vows = require 'vows'
assert = require 'assert'
CoffeeScript = require 'coffee-script'
CoffeeScript.old_tokens = CoffeeScript.tokens
CoffeeScript.tokens = (text) ->
CoffeeScript.updated_tokens_called = true
tokens = CoffeeScript.old_tokens(text)
for token in tokens
if typeof token[2] is "number"
if token[0] is 'INDENT' or token[1] is 'OUTDENT'
token[2] = {first_line: token[2] - 1, last_line: token[2]}
else
token[2] = {first_line: token[2], last_line: token[2]}
token
coffeelint = require path.join('..', 'lib', 'coffeelint')


vows.describe("CoffeeScript 1.5.0+").addBatch({
vows.describe("CoffeeScript 1.5.0+")
.addBatch({
"Setup": ->
CoffeeScript.old_tokens = CoffeeScript.tokens
CoffeeScript.tokens = (text) ->
CoffeeScript.updated_tokens_called = true
tokens = CoffeeScript.old_tokens(text)
for token in tokens
if typeof token[2] is "number"
if token[0] is 'INDENT' or token[1] is 'OUTDENT'
token[2] =
first_line: token[2] - 1
last_line: token[2]
else
token[2] =
first_line: token[2]
last_line: token[2]
token
}).addBatch({

"lineNumber has become an object" :

Expand Down
83 changes: 83 additions & 0 deletions test/test_prefer_english_operator.coffee
@@ -0,0 +1,83 @@
path = require 'path'
vows = require 'vows'
assert = require 'assert'
coffeelint = require path.join('..', 'lib', 'coffeelint')

configError = {prefer_english_operator: {level: 'error'}}

vows.describe('PreferEnglishOperatorssemicolons').addBatch({

'non-English operators':
'should warn when == is used': ->
result = coffeelint.lint('1 == 1', configError)[0]
assert.equal result.context, 'Replace "==" with "is"'

'should warn when != is used': ->
result = coffeelint.lint('1 != 1', configError)[0]
assert.equal result.context, 'Replace "!=" with "isnt"'

'should warn when && is used': ->
result = coffeelint.lint('1 && 1', configError)[0]
assert.equal result.context, 'Replace "&&" with "and"'

'should warn when || is used': ->
result = coffeelint.lint('1 || 1', configError)[0]
assert.equal result.context, 'Replace "||" with "or"'

'should warn when ! is used': ->
result = coffeelint.lint('x = !y', configError)[0]
assert.equal result.context, 'Replace "!" with "not"'

'should not warn when !! is used': ->
result = coffeelint.lint('x = !y', configError)
assert.isEmpty(result.length)

'English operators':
'should not warn when \'is\' is used': ->
assert.isEmpty(coffeelint.lint('1 is 1', configError).length)

'should not warn when \'isnt\' is used': ->
assert.isEmpty(coffeelint.lint('1 isnt 1', configError).length)

'should not warn when \'and\' is used': ->
assert.isEmpty(coffeelint.lint('1 and 1', configError).length)

'should not warn when \'or\' is used': ->
assert.isEmpty(coffeelint.lint('1 or 1', configError).length)

'Comments': ->
topic: """
# 1 == 1
# 1 != 1
# 1 && 1
# 1 || 1
###
1 == 1
1 != 1
1 && 1
1 || 1
###
"""
'should not warn when == is used in a comment': (source) ->
assert.isEmpty(coffeelint.lint(source, configError).length)

'Strings':
'should not warn when == is used in a single-quote string': ->
assert.isEmpty(coffeelint.lint('\'1 == 1\'', configError).length)

'should not warn when == is used in a double-quote string': ->
assert.isEmpty(coffeelint.lint('"1 == 1"', configError).length)

'should not warn when == is used in a multiline string': ->
source = '''
"""
1 == 1
"""
'''
assert.isEmpty(coffeelint.lint(source, configError).length)

}).export(module)




0 comments on commit d028c4e

Please sign in to comment.