Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
Require and use CoffeeLint directly instead of the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
AsaAyers committed Feb 22, 2015
1 parent bc72169 commit fe160ba
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 42 deletions.
10 changes: 7 additions & 3 deletions lib/init.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
path = require 'path'

module.exports =
configDefaults:
coffeelintExecutablePath: path.join __dirname, '..', 'node_modules', 'coffeelint', 'bin'
coffeelintConfigPath: null
# Your configuration belongs in your project, not your editor.
# https://github.com/clutchski/coffeelint/blob/master/doc/user.md
#
# If you include coffeelint in your project's dev dependencies it will use
# that version. This is the same behavior the coffeelint commandline gives
# you.
configDefaults: {}

activate: ->
console.log 'activate linter-coffeelint'
90 changes: 52 additions & 38 deletions lib/linter-coffeelint.coffee
Original file line number Diff line number Diff line change
@@ -1,49 +1,63 @@
linterPath = atom.packages.getLoadedPackage('linter').path
Linter = require "#{linterPath}/lib/linter"
findFile = require "#{linterPath}/lib/util"
path = require('path')
resolve = require('resolve').sync

# I can't just map over parseInt because it needs the 2nd parameter and map
# passes the current index as the 2nd parameter
toInt = (str) -> parseInt(str, 10)

class LinterCoffeelint extends Linter
# The syntax that the linter handles. May be a string or
# list/tuple of strings. Names should be all lowercase.
@syntax: ['source.coffee', 'source.litcoffee']

# A string, list, tuple or callable that returns a string, list or tuple,
# containing the command line (with arguments) used to lint.
cmd: 'coffeelint --reporter jslint'
#
# If you have the react plugin it switches source.coffee to source.coffee.jsx
# even if you aren't actually using jsx in that file.
@syntax: ['source.coffee', 'source.litcoffee', 'source.coffee.jsx']

linterName: 'coffeelint'

# A regex pattern used to extract information from the executable's output.
regex:
'<issue line="(?<line>\\d+)"' +
# '.+?lineEnd="\\d+"' +
'.+?reason="\\[((?<error>error)|(?<warning>warn))\\] (?<message>.+?)"'

regexFlags: 's'

isNodeExecutable: yes

configPath: null

constructor: (editor) ->
super(editor)

atom.config.observe 'linter-coffeelint.coffeelintConfigPath', =>
@configPath = atom.config.get 'linter-coffeelint.coffeelintConfigPath'

if configPathLocal = findFile(@cwd, ['coffeelint.json'])
@cmd += " -f #{configPathLocal}"
else if @configPath
@cmd += " -f #{@configPath}"

atom.config.observe 'linter-coffeelint.coffeelintExecutablePath', =>
@executablePath = atom.config.get 'linter-coffeelint.coffeelintExecutablePath'

if editor.getGrammar().scopeName is 'source.litcoffee'
@cmd += ' --literate'

destroy: ->
atom.config.unobserve 'linter-coffeelint.coffeelintExecutablePath'
atom.config.unobserve 'linter-coffeelint.coffeelintConfigPath'
_resolveCoffeeLint: (filePath) ->
try
return resolve('coffeelint', {
basedir: path.dirname(filePath)
})
return 'coffeelint'

lintFile: (filePath, callback) ->
coffeeLintPath = @_resolveCoffeeLint(filePath)
coffeelint = require(coffeeLintPath)

# Versions before 1.9.1 don't work with atom because of an assumption that
# if window is defined, then it must be running in a browser. Atom breaks
# this assumption, so CoffeeLint < 1.9.1 will fail to find CoffeeScript.
# See https://github.com/clutchski/coffeelint/pull/383
[major, minor, patch] = coffeelint.VERSION.split('.').map(toInt)
if (major <= 1 and minor < 9) or (major is 1 and minor is 9 and patch is 0)
coffeeLintPath = 'coffeelint'
coffeelint = require(coffeeLintPath)

configFinder = require("#{coffeeLintPath}/lib/configfinder")

filename = path.basename filePath
origPath = path.join @cwd, filename

isLiterate = @editor.getGrammar().scopeName is 'source.litcoffee'
config = configFinder.getConfig(origPath)
source = @editor.getText()

result = coffeelint.lint(source, config, isLiterate)

callback(result.map(@transform))

transform: (m) =>
@createMessage {
line: m.lineNumber,
# None of the rules currently return a column, but they may in the future.
col: m.column,
error: m.level is 'error'
warning: m.level is 'warn'
message: "#{m.message} (#{m.rule})"
}

module.exports = LinterCoffeelint
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"atom": ">0.50.0"
},
"dependencies": {
"coffeelint": "~1.5.2"
"coffeelint": "^1.9.1",
"resolve": "^1.1.5"
}
}

0 comments on commit fe160ba

Please sign in to comment.