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

Commit

Permalink
Run linting in a Task (#88)
Browse files Browse the repository at this point in the history
* Run core.coffee as a Task and update to Linter API v2
* Fix tests
* Clean up code
* Add config file test
* Add tests
* Use included coffeelint when project needs upgrade
  • Loading branch information
UziTech authored and Arcanemagus committed Aug 16, 2017
1 parent cd9d825 commit 767972e
Show file tree
Hide file tree
Showing 15 changed files with 1,830 additions and 153 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node_modules
.github_changelog_generator
/node_modules
.github_changelog_generator
52 changes: 52 additions & 0 deletions lib/Linter.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{Task} = require('atom')
{generateRange} = require('atom-linter')

workers = new Set
workerFile = require.resolve('./core.coffee')

module.exports =
class Linter

name: 'CoffeeLint'
grammarScopes: [
'source.coffee'
'source.litcoffee'
'source.coffee.jsx'
'source.coffee.angular'
]
scope: 'file'
lintsOnChange: true

destroy: ->
worker?.terminate() for worker in workers

lint: (textEditor) ->
filePath = textEditor.getPath()
return [] unless filePath

source = textEditor.getText()

isLiterate = textEditor.getGrammar().scopeName is 'source.litcoffee'


transform = ({level, message, rule, lineNumber, context}) ->
message = "#{message}. #{context}" if context
message = "#{message}. (#{rule})"

return {
severity: if level is 'error' then 'error' else 'warning'
excerpt: message
location:
file: filePath
position: generateRange(textEditor, lineNumber - 1)
}

return new Promise (resolve, reject) ->
task = Task.once workerFile, filePath, source, isLiterate, (results) ->
workers.delete(task)
try
resolve(results.map(transform))
catch e
reject(e)
workers.add(task)
task.on 'task:error', (e) -> reject(e)
142 changes: 50 additions & 92 deletions lib/core.coffee
Original file line number Diff line number Diff line change
@@ -1,102 +1,60 @@
# 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)
# This file is a Task file that will run in a separate process
# https://atom.io/docs/api/v1.19.0/Task

resolve = require 'resolve'
path = require 'path'

module.exports =

# The syntax that the linter handles. May be a string or
# list/tuple of strings. Names should be all lowercase.
#
# 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.
scopes: [
'source.coffee'
'source.litcoffee'
'source.coffee.jsx'
'source.coffee.angular'
]

_resolveCoffeeLint: (filePath) ->
try
return path.dirname(resolve.sync('coffeelint/package.json', {
basedir: path.dirname(filePath)
}))
catch e
expected = "Cannot find module 'coffeelint/package.json'"
if e.message[...expected.length] is expected
return 'coffeelint'
throw e

configImportsModules: (config) ->
return true for ruleName, rconfig of config when rconfig.module?
return userConfig?.coffeelint?.transforms?

canImportModules: (coffeelint) ->
[major, minor, patch] = coffeelint.VERSION.split('.').map(toInt)

if major > 1
return true
if major is 1 and minor > 9
return true
if major is 1 and minor is 9 and patch >= 5
return true
false

isCompatibleWithAtom: (coffeelint) ->
[major, minor, patch] = coffeelint.VERSION.split('.').map(toInt)

if major > 1
return true
if major is 1 and minor > 9
return true
if major is 1 and minor is 9 and patch >= 1
return true
false

lint: (filePath, source, scopeName) ->
isLiterate = scopeName is 'source.litcoffee'
showUpgradeError = false

coffeeLintPath = @_resolveCoffeeLint(filePath)
semver = require 'semver'


_resolveCoffeeLint = (filePath) ->
try
return path.dirname(resolve.sync('coffeelint/package.json', {
basedir: path.dirname(filePath)
}))
catch e
expected = "Cannot find module 'coffeelint/package.json'"
if e.message[...expected.length] is expected
return 'coffeelint'
throw e

configImportsModules = (config) ->
return true for ruleName, rconfig of config when rconfig.module?
return userConfig?.coffeelint?.transforms?

module.exports = (filePath, source, isLiterate) ->
showUpgradeError = false

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
if semver.lt(coffeelint.VERSION, '1.9.1')
coffeeLintPath = 'coffeelint'
coffeelint = require(coffeeLintPath)
showUpgradeError = true

configFinder = require("#{coffeeLintPath}/lib/configfinder")
config = configFinder.getConfig(filePath)

# 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 not @isCompatibleWithAtom(coffeelint)
unless showUpgradeError
if configImportsModules(config) and semver.lt(coffeelint.VERSION, '1.9.5')
coffeeLintPath = 'coffeelint'
coffeelint = require(coffeeLintPath)
configFinder = require("#{coffeeLintPath}/lib/configfinder")
config = configFinder.getConfig(filePath)
showUpgradeError = true

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

result = []
try
config = configFinder.getConfig(filePath)
if @configImportsModules(config) and not @canImportModules(coffeelint)
showUpgradeError = true
else
result = coffeelint.lint(source, config, isLiterate)
catch e
console.log(e.message)
console.log(e.stack)
result.push({
lineNumber: 1
level: 'error'
message: "CoffeeLint crashed, see console for error details."
rule: 'none'
})
results = coffeelint.lint(source, config, isLiterate)

if showUpgradeError
result = [{
lineNumber: 1
level: 'error'
message: "http://git.io/local_upgrade upgrade your project's CoffeeLint"
rule: 'none'
}]
if showUpgradeError
results.push {
lineNumber: 1
level: 'error'
message: "http://git.io/local_upgrade upgrade your project's CoffeeLint"
rule: 'none'
}

return result
return results
6 changes: 5 additions & 1 deletion lib/init.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ module.exports =
activate: ->
require("atom-package-deps").install()

deactivate: ->
@linter?.destroy()

provideLinter: ->
return require('./plus-linter.coffee')
Linter = require('./Linter.coffee')
@linter = new Linter
43 changes: 0 additions & 43 deletions lib/plus-linter.coffee

This file was deleted.

Loading

0 comments on commit 767972e

Please sign in to comment.