This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Require and use CoffeeLint directly instead of the CLI
- Loading branch information
Showing
3 changed files
with
61 additions
and
42 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
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' |
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 |
---|---|---|
@@ -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 |
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
"atom": ">0.50.0" | ||
}, | ||
"dependencies": { | ||
"coffeelint": "~1.5.2" | ||
"coffeelint": "^1.9.1", | ||
"resolve": "^1.1.5" | ||
} | ||
} |