This repository has been archived by the owner on Oct 19, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #316 from clutchski/prefer_english_operator
- Loading branch information
Showing
5 changed files
with
141 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
||
|
||
|
||
|