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

Commit

Permalink
Merge pull request #52 from arusakov/try_using_local_tslint
Browse files Browse the repository at this point in the history
Using local tslint
  • Loading branch information
Arcanemagus committed Feb 24, 2016
2 parents 82e1ca1 + 12c64fd commit 858dce7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 19 deletions.
80 changes: 61 additions & 19 deletions lib/init.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{CompositeDisposable} = require 'atom'
path = require 'path'
rulesDirectory = ''
requireResolve = require 'resolve'

TSLINT_MODULE_NAME = 'tslint'

module.exports =

Expand All @@ -9,42 +11,83 @@ module.exports =
type: 'string'
title: 'Custom rules directory'
default: ''
useLocalTslint:
type: 'boolean'
title: 'Try using the local tslint package (if exist)'
default: true

rulesDirectory: ''
tslintCache: new Map
tslintDef: null
useLocalTslint: true

activate: ->
@subscriptions = new CompositeDisposable
@scopes = ['source.ts', 'source.tsx']
@subscriptions.add atom.config.observe 'linter-tslint.rulesDirectory',
(dir) =>
rulesDirectory = dir
@rulesDirectory = dir
@subscriptions.add atom.config.observe 'linter-tslint.useLocalTslint',
(use) =>
@tslintCache.clear()
@useLocalTslint = use

deactivate: ->
@subscriptions.dispose()

getLinter: (filePath) ->
basedir = path.dirname filePath
linter = @tslintCache.get basedir
if linter
return Promise.resolve(linter)

if @useLocalTslint
return @getLocalLinter(basedir)

@tslintCache.set basedir, @tslintDef
Promise.resolve(@tslintDef)

getLocalLinter: (basedir) ->
new Promise (resolve, reject) =>
requireResolve TSLINT_MODULE_NAME, { basedir },
(err, linterPath, pkg) =>
if not err and pkg?.version.startsWith '3.'
linter = require linterPath
else
linter = @tslintDef
@tslintCache.set basedir, linter
resolve(linter)

provideLinter: ->
Linter = require 'tslint'
@tslintDef = require TSLINT_MODULE_NAME

provider =
grammarScopes: @scopes
scope: 'file'
lintOnFly: true
lint: (textEditor) ->
lint: (textEditor) =>
filePath = textEditor.getPath()
text = textEditor.getText()
configuration = Linter.findConfiguration(null, filePath)

directory = undefined
if (rulesDirectory && textEditor.project && textEditor.project.getPaths().length)
directory = textEditor.project.getPaths()[0] + path.sep + rulesDirectory
@getLinter(filePath).then (Linter) =>
configuration = Linter.findConfiguration(null, filePath)

directory = undefined
if @rulesDirectory and textEditor.project?.getPaths().length
directory = path.join textEditor.project.getPaths()[0],
@rulesDirectory

linter = new Linter filePath, text,
formatter: 'json',
configuration: configuration
rulesDirectory: directory

linter = new Linter(filePath, text, {
formatter: 'json',
configuration: configuration
rulesDirectory: directory
});
lintResult = linter.lint()

lintResult = linter.lint()
if not lintResult.failureCount
return []

if (lintResult.failureCount > 0)
return lintResult.failures.map (failure) ->
lintResult.failures.map (failure) ->
startPosition = failure.getStartPosition().getLineAndCharacter()
endPosition = failure.getEndPosition().getLineAndCharacter()
{
Expand All @@ -53,7 +96,6 @@ module.exports =
filePath: path.normalize failure.getFileName()
range: [
[ startPosition.line, startPosition.character],
[ endPosition.line, endPosition.character]]
[ endPosition.line, endPosition.character]
]
}
else
return []
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"atom": ">0.50.0"
},
"dependencies": {
"resolve": "1.1.7",
"tslint": "^3.3.0",
"typescript": ">=1.7.5"
},
Expand Down

0 comments on commit 858dce7

Please sign in to comment.