From 6ce22c15674583d62db1cf57cfcc3f79b5860b03 Mon Sep 17 00:00:00 2001 From: Alexander Rusakov Date: Thu, 18 Feb 2016 16:37:44 +0300 Subject: [PATCH 1/5] useLocalTslint option added, node-resolve used for search local tslint --- lib/init.coffee | 80 +++++++++++++++++++++++++++++++++++++------------ package.json | 1 + 2 files changed, 62 insertions(+), 19 deletions(-) diff --git a/lib/init.coffee b/lib/init.coffee index 93a864fb..26ebf062 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: false + + rulesDirectory: '' + tslintCache: new Map + tslintDef: null + useLocalTslint: false 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 pkd?.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" }, From 4455a9c22ef9e700486c8eee2fa53b3467975598 Mon Sep 17 00:00:00 2001 From: Alexander Rusakov Date: Thu, 18 Feb 2016 17:29:16 +0300 Subject: [PATCH 2/5] update version to 0.7 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d9a8faeb..b91d7d7a 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "linter-tslint", "main": "./lib/init", "linter-package": true, - "version": "0.6.0", + "version": "0.7.0", "description": "Linter plugin for Typescript, using tslint", "repository": { "type": "git", From e71fa976842987af2323d81da53772209630a3ab Mon Sep 17 00:00:00 2001 From: Alexander Rusakov Date: Thu, 18 Feb 2016 18:56:52 +0300 Subject: [PATCH 3/5] useLocalTslint is true by default --- lib/init.coffee | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/init.coffee b/lib/init.coffee index 26ebf062..19f4d50b 100644 --- a/lib/init.coffee +++ b/lib/init.coffee @@ -14,12 +14,12 @@ module.exports = useLocalTslint: type: 'boolean' title: 'Try using the local tslint package (if exist)' - default: false + default: true rulesDirectory: '' tslintCache: new Map tslintDef: null - useLocalTslint: false + useLocalTslint: true activate: -> @subscriptions = new CompositeDisposable From 54accb410c1e9a0f07f8cefa1776349986d066d0 Mon Sep 17 00:00:00 2001 From: arusakov Date: Fri, 19 Feb 2016 01:19:12 +0300 Subject: [PATCH 4/5] version 0.6.0 back --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b91d7d7a..d9a8faeb 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "linter-tslint", "main": "./lib/init", "linter-package": true, - "version": "0.7.0", + "version": "0.6.0", "description": "Linter plugin for Typescript, using tslint", "repository": { "type": "git", From 12c64fd1029d95cb8705d00c7e51d05efe0a458c Mon Sep 17 00:00:00 2001 From: Alexander Rusakov Date: Fri, 19 Feb 2016 13:28:26 +0300 Subject: [PATCH 5/5] fix pkg.version check --- lib/init.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/init.coffee b/lib/init.coffee index 19f4d50b..2c43a899 100644 --- a/lib/init.coffee +++ b/lib/init.coffee @@ -51,7 +51,7 @@ module.exports = new Promise (resolve, reject) => requireResolve TSLINT_MODULE_NAME, { basedir }, (err, linterPath, pkg) => - if not err and pkd?.version.startsWith('3.') + if not err and pkg?.version.startsWith '3.' linter = require linterPath else linter = @tslintDef