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

Using local tslint #52

Merged
merged 5 commits into from
Feb 24, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this, the observe in activate will set it for you when the plugin is activated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does help readability though, so you might want to just leave it in.


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()
{
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 []
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "linter-tslint",
"main": "./lib/init",
"linter-package": true,
"version": "0.6.0",
"version": "0.7.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't bump the version here, apm publish does this automatically.

"description": "Linter plugin for Typescript, using tslint",
"repository": {
"type": "git",
Expand All @@ -13,6 +13,7 @@
"atom": ">0.50.0"
},
"dependencies": {
"resolve": "1.1.7",
"tslint": "^3.3.0",
"typescript": ">=1.7.5"
},
Expand Down