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

Commit

Permalink
Add support for shortcuts (noqa)
Browse files Browse the repository at this point in the history
  • Loading branch information
Radu Dan committed Mar 4, 2016
1 parent 77776c3 commit 0e68c70
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/coffeelint.coffee
Expand Up @@ -298,7 +298,7 @@ coffeelint.lint = (source, userConfig = {}, literate = false) ->
disabledInitially = []
# Check ahead for inline enabled rules
for l in source.split('\n')
[ regex, set, ..., rule ] = LineLinter.configStatement.exec(l) or []
[ regex, set, ..., rule ] = LineLinter.getDirective(l) or []
if set in ['enable', 'enable-line'] and config[rule]?.level is 'ignore'
disabledInitially.push rule
config[rule].level = 'error'
Expand Down
13 changes: 10 additions & 3 deletions src/line_linter.coffee
Expand Up @@ -70,14 +70,21 @@ BaseLinter = require './base_linter.coffee'

# Some repeatedly used regular expressions.
configStatement = /coffeelint:\s*((disable|enable)(-line)?)(?:=([\w\s,]*))?/
configShortcuts = [
# TODO: make this user (and / or api) configurable
[/\#.*noqa/, 'coffeelint: disable-line']
]

#
# A class that performs regex checks on each line of the source.
#
module.exports = class LineLinter extends BaseLinter

# This is exposed here so coffeelint.coffee can reuse it
@configStatement: configStatement
@getDirective: (line) ->
for [shortcut, replacement] in configShortcuts
if line.match(shortcut)
return configStatement.exec(replacement)
return configStatement.exec(line)

constructor: (source, config, rules, tokensByLine, literate = false) ->
super source, config, rules
Expand Down Expand Up @@ -119,7 +126,7 @@ module.exports = class LineLinter extends BaseLinter

collectInlineConfig: (line) ->
# Check for block config statements enable and disable
result = configStatement.exec(line)
result = @constructor.getDirective(line)
if result?
cmd = result[1]
rules = []
Expand Down
17 changes: 17 additions & 0 deletions test/test_comment_config.coffee
Expand Up @@ -62,6 +62,23 @@ vows.describe('comment_config').addBatch({
assert.equal(errors[0].lineNumber, 2)
assert.ok(errors[0].message)

'Expand shortcuts':
topic: () ->
'''
a 'foo'; # noqa
b 'bar';
'''

'will expand and honor directive shortcuts': (source) ->
config =
no_trailing_semicolons: level: 'error'
errors = coffeelint.lint(source, config)
assert.equal(errors.length, 1)
assert.equal(errors[0].rule, 'no_trailing_semicolons')
assert.equal(errors[0].level, 'error')
assert.equal(errors[0].lineNumber, 2)
assert.ok(errors[0].message)

'Disable all statements per line':
topic: () ->
'''
Expand Down

0 comments on commit 0e68c70

Please sign in to comment.