diff --git a/lib/init.coffee b/lib/init.coffee index 93a864fb..2c43a899 100644 --- a/lib/init.coffee +++ b/lib/init.coffee @@ -1,6 +1,8 @@ {CompositeDisposable} = require 'atom' path = require 'path' -rulesDirectory = '' +requireResolve = require 'resolve' + +TSLINT_MODULE_NAME = 'tslint' module.exports = @@ -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() { @@ -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 [] diff --git a/package.json b/package.json index 1882cf77..d9a8faeb 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "atom": ">0.50.0" }, "dependencies": { + "resolve": "1.1.7", "tslint": "^3.3.0", "typescript": ">=1.7.5" },