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

Commit

Permalink
Rewrite in ES2015
Browse files Browse the repository at this point in the history
No more CoffeeScript! 🎉
  • Loading branch information
Arcanemagus committed Sep 26, 2016
1 parent 8f42348 commit 73aeaac
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 121 deletions.
119 changes: 0 additions & 119 deletions lib/init.coffee

This file was deleted.

144 changes: 144 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
'use babel';

/* eslint-disable import/extensions, import/no-extraneous-dependencies */
import { CompositeDisposable } from 'atom';
/* eslint-enable import/extensions, import/no-extraneous-dependencies */
import path from 'path';
import fs from 'fs';
import requireResolve from 'resolve';

const TSLINT_MODULE_NAME = 'tslint';
const tslintCache = new Map();
let tslintDef = null;

export default {
activate() {
require('atom-package-deps').install('linter-tslint');

this.subscriptions = new CompositeDisposable();
this.subscriptions.add(
atom.config.observe('linter-tslint.rulesDirectory', (dir) => {
if (dir && path.isAbsolute(dir)) {
fs.stat(dir, (err, stats) => {
if (stats && stats.isDirectory()) {
this.rulesDirectory = dir;
}
});
}
})
);

this.subscriptions.add(
atom.config.observe('linter-tslint.useLocalTslint', (use) => {
tslintCache.clear();
this.useLocalTslint = use;
})
);
},

deactivate() {
this.subscriptions.dispose();
},

getLinter(filePath) {
const basedir = path.dirname(filePath);
const linter = tslintCache.get(basedir);
if (linter) {
return Promise.resolve(linter);
}

if (this.useLocalTslint) {
return this.getLocalLinter(basedir);
}

tslintCache.set(basedir, tslintDef);
return Promise.resolve(tslintDef);
},

getLocalLinter(basedir) {
return new Promise(resolve =>
requireResolve(TSLINT_MODULE_NAME, { basedir },
(err, linterPath, pkg) => {
let linter;
if (!err && pkg && pkg.version.startsWith('3.')) {
// eslint-disable-next-line import/no-dynamic-require
linter = require('loophole').allowUnsafeNewFunction(() => require(linterPath));
} else {
linter = tslintDef;
}
tslintCache.set(basedir, linter);
return resolve(linter);
}
)
);
},

provideLinter() {
// eslint-disable-next-line import/no-dynamic-require
tslintDef = require('loophole').allowUnsafeNewFunction(() => require(TSLINT_MODULE_NAME));

return {
grammarScopes: ['source.ts', 'source.tsx'],
scope: 'file',
lintOnFly: true,
lint: (textEditor) => {
const filePath = textEditor.getPath();
const text = textEditor.getText();

return this.getLinter(filePath).then((Linter) => {
const configurationPath = Linter.findConfigurationPath(null, filePath);
const configuration = Linter.loadConfigurationFromPath(configurationPath);

let { rulesDirectory } = configuration;
if (rulesDirectory) {
const configurationDir = path.dirname(configurationPath);
if (!Array.isArray(rulesDirectory)) {
rulesDirectory = [rulesDirectory];
}
rulesDirectory = rulesDirectory.map((dir) => {
if (path.isAbsolute(dir)) {
return dir;
}
return path.join(configurationDir, dir);
});

if (this.rulesDirectory) {
rulesDirectory.push(this.rulesDirectory);
}
}

const linter = new Linter(filePath, text, {
formatter: 'json',
configuration,
rulesDirectory,
});

const lintResult = linter.lint();

if (textEditor.getText() !== text) {
// Text has been modified since the lint was triggered, tell linter not to update
return null;
}

if (!lintResult.failureCount) {
return [];
}

return lintResult.failures.map((failure) => {
const startPosition = failure.getStartPosition().getLineAndCharacter();
const endPosition = failure.getEndPosition().getLineAndCharacter();
return {
type: 'Warning',
text: `${failure.getRuleName()} - ${failure.getFailure()}`,
filePath: path.normalize(failure.getFileName()),
range: [
[startPosition.line, startPosition.character],
[endPosition.line, endPosition.character],
],
};
});
});
},
};
},
};
14 changes: 13 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "linter-tslint",
"main": "./lib/init.coffee",
"main": "./lib/main.js",
"linter-package": true,
"version": "0.11.1",
"description": "Linter plugin for Typescript, using tslint",
Expand All @@ -16,6 +16,18 @@
"engines": {
"atom": ">=1.0.0 <2.0.0"
},
"configSchema": {
"rulesDirectory": {
"type": "string",
"title": "Custom rules directory (absolute path)",
"default": ""
},
"useLocalTslint": {
"type": "boolean",
"title": "Try using the local tslint package (if exist)",
"default": true
}
},
"dependencies": {
"atom-package-deps": "4.3.1",
"loophole": "^1.1.0",
Expand Down
2 changes: 1 addition & 1 deletion spec/linter-tslint-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const validPath = path.join(__dirname, 'fixtures', 'valid', 'valid.ts');
const invalidPath = path.join(__dirname, 'fixtures', 'invalid', 'invalid.ts');

describe('The TSLint provider for Linter', () => {
const lint = require('../lib/init.coffee').provideLinter().lint;
const lint = require('../lib/main.js').provideLinter().lint;

beforeEach(() => {
atom.workspace.destroyActivePaneItem();
Expand Down

0 comments on commit 73aeaac

Please sign in to comment.