From 284b585fa6320ca1205fc5263d27933d18eed19e Mon Sep 17 00:00:00 2001 From: steelbrain Date: Mon, 30 Nov 2015 16:54:08 -0700 Subject: [PATCH 01/15] :new: Add ucompilerrc --- .ucompilerrc | 10 ++++++++++ package.json | 6 ++++-- 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 .ucompilerrc diff --git a/.ucompilerrc b/.ucompilerrc new file mode 100644 index 00000000..caf5754e --- /dev/null +++ b/.ucompilerrc @@ -0,0 +1,10 @@ +{ + "plugins": ["babel"], + "rules": [{ + "path": "src/*.js", + "outputPath": "lib/{name}.js", + "babel": { + "presets": ["steelbrain"] + } + }] +} diff --git a/package.json b/package.json index 934cee9d..e086c186 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "atom": ">0.50.0" }, "scripts": { - "lint": "eslint ." + "lint": "eslint .", + "compile": "ucompiler go" }, "dependencies": { "atom-linter": "^3.4.0", @@ -26,7 +27,8 @@ "eslint-config-steelbrain": "latest", "eslint-plugin-import": "^0.11.0", "eslint-plugin-react": "^3.10.0", - "shelljs": "^0.5.3" + "shelljs": "^0.5.3", + "babel-preset-steelbrain": "^1.0.0" }, "package-deps": [ "linter" From 4b092faeacb4e2094fab22f62cc81d2ff51bafda Mon Sep 17 00:00:00 2001 From: steelbrain Date: Mon, 30 Nov 2015 16:55:44 -0700 Subject: [PATCH 02/15] :new: Add src folder with source js files --- lib/helpers.js | 73 ++++++++++------- lib/main.js | 149 +++++++++++++++++++--------------- src/es5-helpers.js | 80 +++++++++++++++++++ src/helpers.js | 36 +++++++++ src/main.js | 193 +++++++++++++++++++++++++++++++++++++++++++++ src/reporter.js | 5 ++ src/worker.js | 109 +++++++++++++++++++++++++ 7 files changed, 553 insertions(+), 92 deletions(-) create mode 100644 src/es5-helpers.js create mode 100644 src/helpers.js create mode 100644 src/main.js create mode 100644 src/reporter.js create mode 100644 src/worker.js diff --git a/lib/helpers.js b/lib/helpers.js index 9665e46e..33743af8 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -1,36 +1,49 @@ -'use babel' +'use strict'; +'use babel'; -import ChildProcess from 'child_process' -import CP from 'childprocess-promise' +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.spawnWorker = spawnWorker; -export function spawnWorker() { - let shouldLive = true - const env = Object.create(process.env) - delete env.NODE_PATH - delete env.NODE_ENV - const data = {stdout: [], stderr: []} - const child = ChildProcess.fork(__dirname + '/worker.js', [], {env, silent: true}) - const worker = new CP(child) +var _child_process = require('child_process'); + +var _child_process2 = _interopRequireDefault(_child_process); + +var _childprocessPromise = require('childprocess-promise'); + +var _childprocessPromise2 = _interopRequireDefault(_childprocessPromise); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function spawnWorker() { + let shouldLive = true; + const env = Object.create(process.env); + delete env.NODE_PATH; + delete env.NODE_ENV; + const data = { stdout: [], stderr: [] }; + const child = _child_process2.default.fork(__dirname + '/worker.js', [], { env: env, silent: true }); + const worker = new _childprocessPromise2.default(child); function killer() { - shouldLive = false - child.kill() + shouldLive = false; + child.kill(); } - child.stdout.on('data', function(chunk) { - data.stdout.push(chunk) - }) - child.stderr.on('data', function(chunk) { - data.stderr.push(chunk) - }) - child.on('exit', function() { + child.stdout.on('data', function (chunk) { + data.stdout.push(chunk); + }); + child.stderr.on('data', function (chunk) { + data.stderr.push(chunk); + }); + child.on('exit', function () { if (shouldLive) { - console.log('ESLint Worker Info', {stdout: data.stdout.join(''), stderr: data.stderr.join('')}) - atom.notifications.addWarning('[Linter-ESLint] Worker died unexpectedly', {detail: 'Check your console for more info. A new worker will be spawned instantly.', dismissable: true}) + console.log('ESLint Worker Info', { stdout: data.stdout.join(''), stderr: data.stderr.join('') }); + atom.notifications.addWarning('[Linter-ESLint] Worker died unexpectedly', { detail: 'Check your console for more info. A new worker will be spawned instantly.', dismissable: true }); } - child.emit('exit-linter', shouldLive) - }) - process.on('exit', killer) - return {child, worker, subscription: {dispose: function() { - killer() - process.removeListener('exit', killer) - }}} -} + child.emit('exit-linter', shouldLive); + }); + process.on('exit', killer); + return { child: child, worker: worker, subscription: { dispose: function () { + killer(); + process.removeListener('exit', killer); + } } }; +} \ No newline at end of file diff --git a/lib/main.js b/lib/main.js index e231856b..22d30e3e 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1,11 +1,25 @@ -'use babel' +'use strict'; +'use babel'; -import Path from 'path' -import {CompositeDisposable} from 'atom' -import {spawnWorker} from './helpers' -import escapeHTML from 'escape-html' +Object.defineProperty(exports, "__esModule", { + value: true +}); -export default { +var _path = require('path'); + +var _path2 = _interopRequireDefault(_path); + +var _atom = require('atom'); + +var _helpers = require('./helpers'); + +var _escapeHtml = require('escape-html'); + +var _escapeHtml2 = _interopRequireDefault(_escapeHtml); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +exports.default = { config: { lintHtmlFiles: { title: 'Lint HTML Files', @@ -53,40 +67,40 @@ export default { default: false } }, - activate: function() { - require('atom-package-deps').install() + activate: function () { + require('atom-package-deps').install(); - this.subscriptions = new CompositeDisposable() - this.active = true - this.worker = null - this.scopes = ['source.js', 'source.jsx', 'source.js.jsx', 'source.babel', 'source.js-semantic'] + this.subscriptions = new _atom.CompositeDisposable(); + this.active = true; + this.worker = null; + this.scopes = ['source.js', 'source.jsx', 'source.js.jsx', 'source.babel', 'source.js-semantic']; - const embeddedScope = 'source.js.embedded.html' + const embeddedScope = 'source.js.embedded.html'; this.subscriptions.add(atom.config.observe('linter-eslint.lintHtmlFiles', lintHtmlFiles => { if (lintHtmlFiles) { - this.scopes.push(embeddedScope) + this.scopes.push(embeddedScope); } else { if (this.scopes.indexOf(embeddedScope) !== -1) { - this.scopes.splice(this.scopes.indexOf(embeddedScope), 1) + this.scopes.splice(this.scopes.indexOf(embeddedScope), 1); } } - })) + })); this.subscriptions.add(atom.commands.add('atom-text-editor', { 'linter-eslint:fix-file': () => { - const textEditor = atom.workspace.getActiveTextEditor() - const filePath = textEditor.getPath() - const fileDir = Path.dirname(filePath) + const textEditor = atom.workspace.getActiveTextEditor(); + const filePath = textEditor.getPath(); + const fileDir = _path2.default.dirname(filePath); if (!textEditor || textEditor.isModified()) { // Abort for invalid or unsaved text editors - atom.notifications.addError('Linter-ESLint: Please save before fixing') - return + atom.notifications.addError('Linter-ESLint: Please save before fixing'); + return; } if (this.worker === null) { // Abort if worker is not yet ready - atom.notifications.addError('Linter-ESLint: Not ready, please try again') - return + atom.notifications.addError('Linter-ESLint: Not ready, please try again'); + return; } this.worker.request('FIX', { @@ -95,13 +109,13 @@ export default { global: atom.config.get('linter-eslint.useGlobalEslint'), nodePath: atom.config.get('linter-eslint.globalNodePath'), configFile: atom.config.get('linter-eslint.eslintrcPath') - }).then(function(response) { - atom.notifications.addSuccess(response) - }).catch(function(response) { - atom.notifications.addWarning(response) - }) + }).then(function (response) { + atom.notifications.addSuccess(response); + }).catch(function (response) { + atom.notifications.addWarning(response); + }); } - })) + })); // Reason: I (steelbrain) have observed that if we spawn a // process while atom is starting up, it can increase startup @@ -109,39 +123,44 @@ export default { // we barely feel a thing. const initializeWorker = () => { if (this.active) { - const {child, worker, subscription} = spawnWorker() - this.worker = worker - this.subscriptions.add(subscription) + var _spawnWorker = (0, _helpers.spawnWorker)(); + + const child = _spawnWorker.child; + const worker = _spawnWorker.worker; + const subscription = _spawnWorker.subscription; + + this.worker = worker; + this.subscriptions.add(subscription); child.on('exit-linter', shouldLive => { - this.worker = null + this.worker = null; // Respawn if it crashed. See atom/electron#3446 if (shouldLive) { - initializeWorker() + initializeWorker(); } - }) + }); } - } - setTimeout(initializeWorker, 5 * 1000) + }; + setTimeout(initializeWorker, 5 * 1000); }, - deactivate: function() { - this.active = false - this.subscriptions.dispose() + deactivate: function () { + this.active = false; + this.subscriptions.dispose(); }, - provideLinter: function() { - const Helpers = require('atom-linter') + provideLinter: function () { + const Helpers = require('atom-linter'); return { name: 'ESLint', grammarScopes: this.scopes, scope: 'file', lintOnFly: true, lint: textEditor => { - const text = textEditor.getText() + const text = textEditor.getText(); if (text.length === 0) { - return Promise.resolve([]) + return Promise.resolve([]); } - const filePath = textEditor.getPath() - const fileDir = Path.dirname(filePath) - const showRule = atom.config.get('linter-eslint.showRuleIdInMessage') + const filePath = textEditor.getPath(); + const fileDir = _path2.default.dirname(filePath); + const showRule = atom.config.get('linter-eslint.showRuleIdInMessage'); if (this.worker === null) { return Promise.resolve([{ @@ -149,7 +168,7 @@ export default { type: 'Info', text: 'Worker initialization is delayed. Please try saving or typing to begin linting.', range: Helpers.rangeFromLineNumber(textEditor, 0) - }]) + }]); } return this.worker.request('JOB', { @@ -162,32 +181,38 @@ export default { rulesDir: atom.config.get('linter-eslint.eslintRulesDir'), configFile: atom.config.get('linter-eslint.eslintrcPath'), disableIgnores: atom.config.get('linter-eslint.disableEslintIgnore') - }).then(function(response) { + }).then(function (response) { if (response.length === 1 && response[0].message === 'File ignored because of your .eslintignore file. Use --no-ignore to override.') { - return [] + return []; } - return response.map(function({message, line, severity, ruleId, column}) { - const range = Helpers.rangeFromLineNumber(textEditor, line - 1) + return response.map(function (_ref) { + let message = _ref.message; + let line = _ref.line; + let severity = _ref.severity; + let ruleId = _ref.ruleId; + let column = _ref.column; + + const range = Helpers.rangeFromLineNumber(textEditor, line - 1); if (column) { - range[0][1] = column - 1 + range[0][1] = column - 1; } if (column > range[1][1]) { - range[1][1] = column - 1 + range[1][1] = column - 1; } const ret = { filePath: filePath, type: severity === 1 ? 'Warning' : 'Error', range: range - } + }; if (showRule) { - ret.html = `${ruleId || 'Fatal'} ${escapeHTML(message)}` + ret.html = `${ ruleId || 'Fatal' } ${ (0, _escapeHtml2.default)(message) }`; } else { - ret.text = message + ret.text = message; } - return ret - }) - }) + return ret; + }); + }); } - } + }; } -} +}; \ No newline at end of file diff --git a/src/es5-helpers.js b/src/es5-helpers.js new file mode 100644 index 00000000..a1591855 --- /dev/null +++ b/src/es5-helpers.js @@ -0,0 +1,80 @@ +'use strict' + +const ChildProcess = require('child_process') +const Path = require('path') +const FS = require('fs') +const find = require('atom-linter').findFile + +let prefixPath = null +const atomEslintPath = Path.join(FS.realpathSync(Path.join(__dirname, '..')), 'node_modules', 'eslint') + +function findEslintDir(params) { + const modulesPath = find(params.fileDir, 'node_modules') + let eslintNewPath = null + + if (params.global) { + if (params.nodePath === '' && prefixPath === null) { + const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm' + try { + prefixPath = ChildProcess.spawnSync(npmCommand, ['get', 'prefix']).output[1].toString().trim() + } catch (e) { + throw new Error('Unable to execute `npm get prefix`. Please make sure Atom is getting $PATH correctly') + } + } + if (process.platform === 'win32') { + eslintNewPath = Path.join(params.nodePath || prefixPath, 'node_modules', 'eslint') + } else { + eslintNewPath = Path.join(params.nodePath || prefixPath, 'lib', 'node_modules', 'eslint') + } + } else { + try { + FS.accessSync(eslintNewPath = Path.join(modulesPath, 'eslint'), FS.R_OK) + } catch (_) { + eslintNewPath = atomEslintPath + } + } + + return eslintNewPath +} + +// Check for project config file or eslint config in package.json and determine +// whether to bail out or use config specified in package options +function determineConfigFile(params) { + // config file + const configFile = find(params.fileDir, ['.eslintrc.js', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json', '.eslintrc']) || null + if (configFile) { + return configFile + } + // package.json + const packagePath = find(params.fileDir, 'package.json') + if (packagePath && Boolean(require(packagePath).eslintConfig)) { + return packagePath + } + // Couldn't find a config + if (params.canDisable) { + return null + } + // If all else fails, use the configFile specified in the linter-eslint options + if (params.configFile) { + return params.configFile + } +} + +function getEslintCli(path) { + try { + const eslint = require(Path.join(path, 'lib', 'cli.js')) + return eslint + } catch (e) { + if (e.code === 'MODULE_NOT_FOUND') { + throw new Error('ESLint not found, Please install or make sure Atom is getting $PATH correctly') + } else throw e + } +} + + +module.exports = { + findEslintDir: findEslintDir, + find: find, + determineConfigFile: determineConfigFile, + getEslintCli: getEslintCli +} diff --git a/src/helpers.js b/src/helpers.js new file mode 100644 index 00000000..9665e46e --- /dev/null +++ b/src/helpers.js @@ -0,0 +1,36 @@ +'use babel' + +import ChildProcess from 'child_process' +import CP from 'childprocess-promise' + +export function spawnWorker() { + let shouldLive = true + const env = Object.create(process.env) + delete env.NODE_PATH + delete env.NODE_ENV + const data = {stdout: [], stderr: []} + const child = ChildProcess.fork(__dirname + '/worker.js', [], {env, silent: true}) + const worker = new CP(child) + function killer() { + shouldLive = false + child.kill() + } + child.stdout.on('data', function(chunk) { + data.stdout.push(chunk) + }) + child.stderr.on('data', function(chunk) { + data.stderr.push(chunk) + }) + child.on('exit', function() { + if (shouldLive) { + console.log('ESLint Worker Info', {stdout: data.stdout.join(''), stderr: data.stderr.join('')}) + atom.notifications.addWarning('[Linter-ESLint] Worker died unexpectedly', {detail: 'Check your console for more info. A new worker will be spawned instantly.', dismissable: true}) + } + child.emit('exit-linter', shouldLive) + }) + process.on('exit', killer) + return {child, worker, subscription: {dispose: function() { + killer() + process.removeListener('exit', killer) + }}} +} diff --git a/src/main.js b/src/main.js new file mode 100644 index 00000000..e231856b --- /dev/null +++ b/src/main.js @@ -0,0 +1,193 @@ +'use babel' + +import Path from 'path' +import {CompositeDisposable} from 'atom' +import {spawnWorker} from './helpers' +import escapeHTML from 'escape-html' + +export default { + config: { + lintHtmlFiles: { + title: 'Lint HTML Files', + description: 'You should also add `eslint-plugin-html` to your .eslintrc plugins', + type: 'boolean', + default: false + }, + useGlobalEslint: { + title: 'Use global ESLint installation', + description: 'Make sure you have it in your $PATH', + type: 'boolean', + default: false + }, + showRuleIdInMessage: { + title: 'Show Rule ID in Messages', + type: 'boolean', + default: true + }, + disableWhenNoEslintConfig: { + title: 'Disable when no ESLint config is found (in package.json or .eslintrc)', + type: 'boolean', + default: true + }, + eslintrcPath: { + title: '.eslintrc Path', + description: "It will only be used when there's no config file in project", + type: 'string', + default: '' + }, + globalNodePath: { + title: 'Global Node Installation Path', + description: 'Write the value of `npm get prefix` here', + type: 'string', + default: '' + }, + eslintRulesDir: { + title: 'ESLint Rules Dir', + description: 'Specify a directory for ESLint to load rules from', + type: 'string', + default: '' + }, + disableEslintIgnore: { + title: 'Disable using .eslintignore files', + type: 'boolean', + default: false + } + }, + activate: function() { + require('atom-package-deps').install() + + this.subscriptions = new CompositeDisposable() + this.active = true + this.worker = null + this.scopes = ['source.js', 'source.jsx', 'source.js.jsx', 'source.babel', 'source.js-semantic'] + + const embeddedScope = 'source.js.embedded.html' + this.subscriptions.add(atom.config.observe('linter-eslint.lintHtmlFiles', lintHtmlFiles => { + if (lintHtmlFiles) { + this.scopes.push(embeddedScope) + } else { + if (this.scopes.indexOf(embeddedScope) !== -1) { + this.scopes.splice(this.scopes.indexOf(embeddedScope), 1) + } + } + })) + this.subscriptions.add(atom.commands.add('atom-text-editor', { + 'linter-eslint:fix-file': () => { + const textEditor = atom.workspace.getActiveTextEditor() + const filePath = textEditor.getPath() + const fileDir = Path.dirname(filePath) + + if (!textEditor || textEditor.isModified()) { + // Abort for invalid or unsaved text editors + atom.notifications.addError('Linter-ESLint: Please save before fixing') + return + } + + if (this.worker === null) { + // Abort if worker is not yet ready + atom.notifications.addError('Linter-ESLint: Not ready, please try again') + return + } + + this.worker.request('FIX', { + fileDir: fileDir, + filePath: filePath, + global: atom.config.get('linter-eslint.useGlobalEslint'), + nodePath: atom.config.get('linter-eslint.globalNodePath'), + configFile: atom.config.get('linter-eslint.eslintrcPath') + }).then(function(response) { + atom.notifications.addSuccess(response) + }).catch(function(response) { + atom.notifications.addWarning(response) + }) + } + })) + + // Reason: I (steelbrain) have observed that if we spawn a + // process while atom is starting up, it can increase startup + // time by several seconds, But if we do this after 5 seconds, + // we barely feel a thing. + const initializeWorker = () => { + if (this.active) { + const {child, worker, subscription} = spawnWorker() + this.worker = worker + this.subscriptions.add(subscription) + child.on('exit-linter', shouldLive => { + this.worker = null + // Respawn if it crashed. See atom/electron#3446 + if (shouldLive) { + initializeWorker() + } + }) + } + } + setTimeout(initializeWorker, 5 * 1000) + }, + deactivate: function() { + this.active = false + this.subscriptions.dispose() + }, + provideLinter: function() { + const Helpers = require('atom-linter') + return { + name: 'ESLint', + grammarScopes: this.scopes, + scope: 'file', + lintOnFly: true, + lint: textEditor => { + const text = textEditor.getText() + if (text.length === 0) { + return Promise.resolve([]) + } + const filePath = textEditor.getPath() + const fileDir = Path.dirname(filePath) + const showRule = atom.config.get('linter-eslint.showRuleIdInMessage') + + if (this.worker === null) { + return Promise.resolve([{ + filePath: filePath, + type: 'Info', + text: 'Worker initialization is delayed. Please try saving or typing to begin linting.', + range: Helpers.rangeFromLineNumber(textEditor, 0) + }]) + } + + return this.worker.request('JOB', { + fileDir: fileDir, + filePath: filePath, + contents: text, + global: atom.config.get('linter-eslint.useGlobalEslint'), + canDisable: atom.config.get('linter-eslint.disableWhenNoEslintConfig'), + nodePath: atom.config.get('linter-eslint.globalNodePath'), + rulesDir: atom.config.get('linter-eslint.eslintRulesDir'), + configFile: atom.config.get('linter-eslint.eslintrcPath'), + disableIgnores: atom.config.get('linter-eslint.disableEslintIgnore') + }).then(function(response) { + if (response.length === 1 && response[0].message === 'File ignored because of your .eslintignore file. Use --no-ignore to override.') { + return [] + } + return response.map(function({message, line, severity, ruleId, column}) { + const range = Helpers.rangeFromLineNumber(textEditor, line - 1) + if (column) { + range[0][1] = column - 1 + } + if (column > range[1][1]) { + range[1][1] = column - 1 + } + const ret = { + filePath: filePath, + type: severity === 1 ? 'Warning' : 'Error', + range: range + } + if (showRule) { + ret.html = `${ruleId || 'Fatal'} ${escapeHTML(message)}` + } else { + ret.text = message + } + return ret + }) + }) + } + } + } +} diff --git a/src/reporter.js b/src/reporter.js new file mode 100644 index 00000000..1eea8bec --- /dev/null +++ b/src/reporter.js @@ -0,0 +1,5 @@ +'use strict' +// This file is used by eslint to hand the errors over to the worker +module.exports = function(results) { + global.__LINTER_RESPONSE = results[0].messages +} diff --git a/src/worker.js b/src/worker.js new file mode 100644 index 00000000..3a950187 --- /dev/null +++ b/src/worker.js @@ -0,0 +1,109 @@ +'use strict' +// Note: 'use babel' doesn't work in forked processes +process.title = 'linter-eslint helper' + +const CP = require('childprocess-promise') +const execFileSync = require('child_process').execFileSync +const Path = require('path') + +const resolveEnv = require('resolve-env') +const Helpers = require('./es5-helpers') + +const findEslintDir = Helpers.findEslintDir +const find = Helpers.find +const determineConfigFile = Helpers.determineConfigFile +const getEslintCli = Helpers.getEslintCli +const Communication = new CP() + +// closed-over module-scope variables +let eslintPath = null +let eslint = null + +Communication.on('JOB', function(job) { + const params = job.Message + const modulesPath = find(params.fileDir, 'node_modules') + const eslintignoreDir = Path.dirname(find(params.fileDir, '.eslintignore')) + let configFile = null + global.__LINTER_RESPONSE = [] + + // Check for config file and determine whether to bail out + configFile = determineConfigFile(params) + + if (params.canDisable && configFile === null) { + job.Response = [] + return + } + + if (modulesPath) { + process.env.NODE_PATH = modulesPath + } else process.env.NODE_PATH = '' + require('module').Module._initPaths() + + // Determine which eslint instance to use + const eslintNewPath = findEslintDir(params) + if (eslintNewPath !== eslintPath) { + eslint = getEslintCli(eslintNewPath) + eslintPath = eslintNewPath + } + + job.Response = new Promise(function(resolve) { + let filePath + if (eslintignoreDir) { + filePath = Path.relative(eslintignoreDir, params.filePath) + process.chdir(eslintignoreDir) + } else { + filePath = Path.basename(params.filePath) + process.chdir(params.fileDir) + } + const argv = [ + process.execPath, + eslintPath, + '--stdin', + '--format', + Path.join(__dirname, 'reporter.js') + ] + if (params.rulesDir) { + let rulesDir = resolveEnv(params.rulesDir) + if (!Path.isAbsolute(rulesDir)) { + rulesDir = find(params.fileDir, rulesDir) + } + argv.push('--rulesdir', rulesDir) + } + if (configFile !== null) { + argv.push('--config', resolveEnv(configFile)) + } + if (params.disableIgnores) { + argv.push('--no-ignore') + } + argv.push('--stdin-filename', filePath) + process.argv = argv + eslint.execute(process.argv, params.contents) + resolve(global.__LINTER_RESPONSE) + }) +}) + +Communication.on('FIX', function(fixJob) { + const params = fixJob.Message + const eslintDir = findEslintDir(params) + const configFile = determineConfigFile(params) + const eslintBinPath = Path.normalize(Path.join(eslintDir, 'bin', 'eslint.js')) + + const argv = [ + params.filePath, + '--fix' + ] + if (configFile !== null) { + argv.push('--config', resolveEnv(configFile)) + } + + fixJob.Response = new Promise(function(resolve, reject) { + try { + execFileSync(eslintBinPath, argv, {cwd: params.fileDir}) + } catch (err) { + reject('Linter-ESLint: Fix Attempt Completed, Linting Errors Remain') + } + resolve('Linter-ESLint: Fix Complete') + }) +}) + +process.exit = function() { /* Stop eslint from closing the daemon */ } From a25dfbdeafd2d9d1501d0bef186350f79e511c41 Mon Sep 17 00:00:00 2001 From: steelbrain Date: Mon, 30 Nov 2015 16:58:06 -0700 Subject: [PATCH 03/15] :new: Add getCliFromPath helper --- src/helpers.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/helpers.js b/src/helpers.js index 9665e46e..36e8ebdf 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -1,8 +1,12 @@ 'use babel' +import Path from 'path' +import FS from 'fs' import ChildProcess from 'child_process' import CP from 'childprocess-promise' +export const bundledEslintPath = Path.join(FS.realpathSync(Path.join(__dirname, '..')), 'node_modules', 'eslint') + export function spawnWorker() { let shouldLive = true const env = Object.create(process.env) @@ -34,3 +38,13 @@ export function spawnWorker() { process.removeListener('exit', killer) }}} } + +export function getCliFromPath(path) { + try { + return require(Path.join(path, 'lib', 'cli.js')) + } catch (e) { + if (e.code === 'MODULE_NOT_FOUND') { + throw new Error('ESLint not found, Please install or make sure Atom is getting $PATH correctly') + } else throw e + } +} From 7bba1a20667b899e0a7f5c9ca46323ac2ebac812 Mon Sep 17 00:00:00 2001 From: steelbrain Date: Mon, 30 Nov 2015 17:09:38 -0700 Subject: [PATCH 04/15] :new: Port more helpers to babel --- src/helpers.js | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index 36e8ebdf..44180725 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -4,8 +4,7 @@ import Path from 'path' import FS from 'fs' import ChildProcess from 'child_process' import CP from 'childprocess-promise' - -export const bundledEslintPath = Path.join(FS.realpathSync(Path.join(__dirname, '..')), 'node_modules', 'eslint') +import {findFile} from 'atom-linter' export function spawnWorker() { let shouldLive = true @@ -39,7 +38,7 @@ export function spawnWorker() { }}} } -export function getCliFromPath(path) { +export function getCliFromDirectory(path) { try { return require(Path.join(path, 'lib', 'cli.js')) } catch (e) { @@ -48,3 +47,46 @@ export function getCliFromPath(path) { } else throw e } } + +let nodePrefixPath = null + +export function getNodePrefixPath() { + if (nodePrefixPath === null) { + const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm' + try { + nodePrefixPath = ChildProcess.spawnSync(npmCommand, ['get', 'prefix']).output[1].toString().trim() + } catch (e) { + throw new Error('Unable to execute `npm get prefix`. Please make sure Atom is getting $PATH correctly') + } + } + return nodePrefixPath +} + +let bundledEslintDirectory = null + +export function getBundledEslintDirectory() { + if (bundledEslintDirectory === null) { + bundledEslintDirectory = Path.join(FS.realpathSync(Path.join(__dirname, '..')), 'node_modules', 'eslint') + } + return bundledEslintDirectory +} + +export function getEslintDirectory(params) { + if (params.global) { + const prefixPath = getNodePrefixPath() + if (process.platform === 'win32') { + return Path.join(params.nodePath || prefixPath, 'node_modules', 'eslint') + } else { + return Path.join(params.nodePath || prefixPath, 'lib', 'node_modules', 'eslint') + } + } else { + const modulesPath = findFile(params.fileDir, 'node_modules') + const eslintPath = Path.join(modulesPath, 'eslint') + try { + FS.accessSync(eslintPath, FS.R_OK) + return eslintPath + } catch (_) { + return getBundledEslintDirectory() + } + } +} From 58a063ca2fa81c83e66cf826b3b4821920b41c43 Mon Sep 17 00:00:00 2001 From: steelbrain Date: Mon, 30 Nov 2015 17:19:26 -0700 Subject: [PATCH 05/15] :new: Port getEslintConfig to babel --- src/es5-helpers.js | 80 ---------------------------------------------- src/helpers.js | 24 ++++++++++++-- 2 files changed, 22 insertions(+), 82 deletions(-) delete mode 100644 src/es5-helpers.js diff --git a/src/es5-helpers.js b/src/es5-helpers.js deleted file mode 100644 index a1591855..00000000 --- a/src/es5-helpers.js +++ /dev/null @@ -1,80 +0,0 @@ -'use strict' - -const ChildProcess = require('child_process') -const Path = require('path') -const FS = require('fs') -const find = require('atom-linter').findFile - -let prefixPath = null -const atomEslintPath = Path.join(FS.realpathSync(Path.join(__dirname, '..')), 'node_modules', 'eslint') - -function findEslintDir(params) { - const modulesPath = find(params.fileDir, 'node_modules') - let eslintNewPath = null - - if (params.global) { - if (params.nodePath === '' && prefixPath === null) { - const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm' - try { - prefixPath = ChildProcess.spawnSync(npmCommand, ['get', 'prefix']).output[1].toString().trim() - } catch (e) { - throw new Error('Unable to execute `npm get prefix`. Please make sure Atom is getting $PATH correctly') - } - } - if (process.platform === 'win32') { - eslintNewPath = Path.join(params.nodePath || prefixPath, 'node_modules', 'eslint') - } else { - eslintNewPath = Path.join(params.nodePath || prefixPath, 'lib', 'node_modules', 'eslint') - } - } else { - try { - FS.accessSync(eslintNewPath = Path.join(modulesPath, 'eslint'), FS.R_OK) - } catch (_) { - eslintNewPath = atomEslintPath - } - } - - return eslintNewPath -} - -// Check for project config file or eslint config in package.json and determine -// whether to bail out or use config specified in package options -function determineConfigFile(params) { - // config file - const configFile = find(params.fileDir, ['.eslintrc.js', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json', '.eslintrc']) || null - if (configFile) { - return configFile - } - // package.json - const packagePath = find(params.fileDir, 'package.json') - if (packagePath && Boolean(require(packagePath).eslintConfig)) { - return packagePath - } - // Couldn't find a config - if (params.canDisable) { - return null - } - // If all else fails, use the configFile specified in the linter-eslint options - if (params.configFile) { - return params.configFile - } -} - -function getEslintCli(path) { - try { - const eslint = require(Path.join(path, 'lib', 'cli.js')) - return eslint - } catch (e) { - if (e.code === 'MODULE_NOT_FOUND') { - throw new Error('ESLint not found, Please install or make sure Atom is getting $PATH correctly') - } else throw e - } -} - - -module.exports = { - findEslintDir: findEslintDir, - find: find, - determineConfigFile: determineConfigFile, - getEslintCli: getEslintCli -} diff --git a/src/helpers.js b/src/helpers.js index 44180725..95b2ef0b 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -4,7 +4,7 @@ import Path from 'path' import FS from 'fs' import ChildProcess from 'child_process' import CP from 'childprocess-promise' -import {findFile} from 'atom-linter' +import {findFile as find} from 'atom-linter' export function spawnWorker() { let shouldLive = true @@ -80,7 +80,7 @@ export function getEslintDirectory(params) { return Path.join(params.nodePath || prefixPath, 'lib', 'node_modules', 'eslint') } } else { - const modulesPath = findFile(params.fileDir, 'node_modules') + const modulesPath = find(params.fileDir, 'node_modules') const eslintPath = Path.join(modulesPath, 'eslint') try { FS.accessSync(eslintPath, FS.R_OK) @@ -90,3 +90,23 @@ export function getEslintDirectory(params) { } } } + +export function getEslintConfig(params) { + const configFile = find(params.fileDir, ['.eslintrc.js', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json', '.eslintrc']) || null + if (configFile) { + return configFile + } + + const packagePath = find(params.fileDir, 'package.json') + if (packagePath && Boolean(require(packagePath).eslintConfig)) { + return packagePath + } + + if (params.canDisable) { + return null + } + + if (params.configFile) { + return params.configFile + } +} From 2983cc518edbf97b5aeb9f1faafa98d11ead6c5a Mon Sep 17 00:00:00 2001 From: steelbrain Date: Mon, 30 Nov 2015 17:26:35 -0700 Subject: [PATCH 06/15] :art: Use Path.normalize instead of FS.realPathSync --- src/helpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers.js b/src/helpers.js index 95b2ef0b..ccabc34d 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -66,7 +66,7 @@ let bundledEslintDirectory = null export function getBundledEslintDirectory() { if (bundledEslintDirectory === null) { - bundledEslintDirectory = Path.join(FS.realpathSync(Path.join(__dirname, '..')), 'node_modules', 'eslint') + bundledEslintDirectory = Path.normalize(Path.join(__dirname, '..', 'node_modules', 'eslint')) } return bundledEslintDirectory } From 6465ecde34c09ae8efd26e17e5340cb68b7420d7 Mon Sep 17 00:00:00 2001 From: steelbrain Date: Mon, 30 Nov 2015 17:28:33 -0700 Subject: [PATCH 07/15] :new: Add getModulesDirectory helper --- src/helpers.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index ccabc34d..e33900ca 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -38,6 +38,10 @@ export function spawnWorker() { }}} } +export function getModulesDirectory(fileDir) { + return find(fileDir, 'node_modules') +} + export function getCliFromDirectory(path) { try { return require(Path.join(path, 'lib', 'cli.js')) @@ -71,7 +75,7 @@ export function getBundledEslintDirectory() { return bundledEslintDirectory } -export function getEslintDirectory(params) { +export function getEslintDirectory(params, modulesPath) { if (params.global) { const prefixPath = getNodePrefixPath() if (process.platform === 'win32') { @@ -80,7 +84,6 @@ export function getEslintDirectory(params) { return Path.join(params.nodePath || prefixPath, 'lib', 'node_modules', 'eslint') } } else { - const modulesPath = find(params.fileDir, 'node_modules') const eslintPath = Path.join(modulesPath, 'eslint') try { FS.accessSync(eslintPath, FS.R_OK) From 3d1227f37c0668035810dd8bed332a594ba35d10 Mon Sep 17 00:00:00 2001 From: steelbrain Date: Mon, 30 Nov 2015 17:33:49 -0700 Subject: [PATCH 08/15] :new: Add getIgnoresFile helper --- src/helpers.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/helpers.js b/src/helpers.js index e33900ca..41be3a91 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -42,6 +42,10 @@ export function getModulesDirectory(fileDir) { return find(fileDir, 'node_modules') } +export function getIgnoresFile(fileDir) { + return Path.dirname(find(fileDir, '.eslintignore')) +} + export function getCliFromDirectory(path) { try { return require(Path.join(path, 'lib', 'cli.js')) From 0e3cf19462ac46b4f781fb139e5f128698d4fca5 Mon Sep 17 00:00:00 2001 From: steelbrain Date: Mon, 30 Nov 2015 17:41:53 -0700 Subject: [PATCH 09/15] :arrow_up: Use the new helper modules in worker --- src/helpers.js | 2 +- src/worker.js | 60 ++++++++++++++++++++++---------------------------- 2 files changed, 27 insertions(+), 35 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index 41be3a91..072c5248 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -46,7 +46,7 @@ export function getIgnoresFile(fileDir) { return Path.dirname(find(fileDir, '.eslintignore')) } -export function getCliFromDirectory(path) { +export function getEslintFromDirectory(path) { try { return require(Path.join(path, 'lib', 'cli.js')) } catch (e) { diff --git a/src/worker.js b/src/worker.js index 3a950187..13117b1a 100644 --- a/src/worker.js +++ b/src/worker.js @@ -2,36 +2,33 @@ // Note: 'use babel' doesn't work in forked processes process.title = 'linter-eslint helper' -const CP = require('childprocess-promise') -const execFileSync = require('child_process').execFileSync -const Path = require('path') +import Path from 'path' +import {execFileSync} from 'child_process' +import CP from 'childprocess-promise' +import resolveEnv from 'resolve-end' +import * as Helpers from './helpers' -const resolveEnv = require('resolve-env') -const Helpers = require('./es5-helpers') - -const findEslintDir = Helpers.findEslintDir -const find = Helpers.find -const determineConfigFile = Helpers.determineConfigFile -const getEslintCli = Helpers.getEslintCli const Communication = new CP() -// closed-over module-scope variables -let eslintPath = null -let eslint = null +let eslint +let lastEslintDirectory -Communication.on('JOB', function(job) { - const params = job.Message - const modulesPath = find(params.fileDir, 'node_modules') - const eslintignoreDir = Path.dirname(find(params.fileDir, '.eslintignore')) - let configFile = null +Communication.on('JOB', function(Job) { global.__LINTER_RESPONSE = [] - // Check for config file and determine whether to bail out - configFile = determineConfigFile(params) + const params = Job.Message + const modulesPath = Helpers.getModulesDirectory(params.fileDir) + const ignoreFile = Helpers.getIgnoresFile(params.fileDir) + const configFile = Helpers.getEslintConfig(params.fileDir) + const eslintDirectory = Helpers.getEslintDirectory(params, modulesPath) if (params.canDisable && configFile === null) { - job.Response = [] - return + return Job.Response = [] + } + + if (eslintDirectory !== lastEslintDirectory) { + lastEslintDirectory = eslintDirectory + eslint = Helpers.getEslintFromDirectory(eslintDirectory) } if (modulesPath) { @@ -39,25 +36,19 @@ Communication.on('JOB', function(job) { } else process.env.NODE_PATH = '' require('module').Module._initPaths() - // Determine which eslint instance to use - const eslintNewPath = findEslintDir(params) - if (eslintNewPath !== eslintPath) { - eslint = getEslintCli(eslintNewPath) - eslintPath = eslintNewPath - } - - job.Response = new Promise(function(resolve) { + Job.Response = new Promise(function(resolve) { let filePath - if (eslintignoreDir) { - filePath = Path.relative(eslintignoreDir, params.filePath) - process.chdir(eslintignoreDir) + if (ignoreFile) { + filePath = Path.relative(ignoreFile, params.filePath) + process.chdir(ignoreFile) } else { filePath = Path.basename(params.filePath) process.chdir(params.fileDir) } + const argv = [ process.execPath, - eslintPath, + eslintDirectory, '--stdin', '--format', Path.join(__dirname, 'reporter.js') @@ -77,6 +68,7 @@ Communication.on('JOB', function(job) { } argv.push('--stdin-filename', filePath) process.argv = argv + eslint.execute(process.argv, params.contents) resolve(global.__LINTER_RESPONSE) }) From d34746ff9456b6a3468b641147eea909719c7854 Mon Sep 17 00:00:00 2001 From: steelbrain Date: Mon, 30 Nov 2015 17:52:14 -0700 Subject: [PATCH 10/15] :art: Further simplify worker Move more logic to helpers --- lib/es5-helpers.js | 80 ------------------------------ lib/helpers.js | 121 +++++++++++++++++++++++++++++++++++++++++++++ lib/worker.js | 84 ++++++++++++------------------- src/helpers.js | 25 +++++++++- src/worker.js | 42 ++++++---------- 5 files changed, 191 insertions(+), 161 deletions(-) delete mode 100644 lib/es5-helpers.js diff --git a/lib/es5-helpers.js b/lib/es5-helpers.js deleted file mode 100644 index a1591855..00000000 --- a/lib/es5-helpers.js +++ /dev/null @@ -1,80 +0,0 @@ -'use strict' - -const ChildProcess = require('child_process') -const Path = require('path') -const FS = require('fs') -const find = require('atom-linter').findFile - -let prefixPath = null -const atomEslintPath = Path.join(FS.realpathSync(Path.join(__dirname, '..')), 'node_modules', 'eslint') - -function findEslintDir(params) { - const modulesPath = find(params.fileDir, 'node_modules') - let eslintNewPath = null - - if (params.global) { - if (params.nodePath === '' && prefixPath === null) { - const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm' - try { - prefixPath = ChildProcess.spawnSync(npmCommand, ['get', 'prefix']).output[1].toString().trim() - } catch (e) { - throw new Error('Unable to execute `npm get prefix`. Please make sure Atom is getting $PATH correctly') - } - } - if (process.platform === 'win32') { - eslintNewPath = Path.join(params.nodePath || prefixPath, 'node_modules', 'eslint') - } else { - eslintNewPath = Path.join(params.nodePath || prefixPath, 'lib', 'node_modules', 'eslint') - } - } else { - try { - FS.accessSync(eslintNewPath = Path.join(modulesPath, 'eslint'), FS.R_OK) - } catch (_) { - eslintNewPath = atomEslintPath - } - } - - return eslintNewPath -} - -// Check for project config file or eslint config in package.json and determine -// whether to bail out or use config specified in package options -function determineConfigFile(params) { - // config file - const configFile = find(params.fileDir, ['.eslintrc.js', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json', '.eslintrc']) || null - if (configFile) { - return configFile - } - // package.json - const packagePath = find(params.fileDir, 'package.json') - if (packagePath && Boolean(require(packagePath).eslintConfig)) { - return packagePath - } - // Couldn't find a config - if (params.canDisable) { - return null - } - // If all else fails, use the configFile specified in the linter-eslint options - if (params.configFile) { - return params.configFile - } -} - -function getEslintCli(path) { - try { - const eslint = require(Path.join(path, 'lib', 'cli.js')) - return eslint - } catch (e) { - if (e.code === 'MODULE_NOT_FOUND') { - throw new Error('ESLint not found, Please install or make sure Atom is getting $PATH correctly') - } else throw e - } -} - - -module.exports = { - findEslintDir: findEslintDir, - find: find, - determineConfigFile: determineConfigFile, - getEslintCli: getEslintCli -} diff --git a/lib/helpers.js b/lib/helpers.js index 33743af8..e083cfd6 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -5,6 +5,22 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.spawnWorker = spawnWorker; +exports.getModulesDirectory = getModulesDirectory; +exports.getIgnoresFile = getIgnoresFile; +exports.getEslintFromDirectory = getEslintFromDirectory; +exports.getNodePrefixPath = getNodePrefixPath; +exports.getBundledEslintDirectory = getBundledEslintDirectory; +exports.getEslintDirectory = getEslintDirectory; +exports.getEslintConfig = getEslintConfig; +exports.getEslint = getEslint; + +var _path = require('path'); + +var _path2 = _interopRequireDefault(_path); + +var _fs = require('fs'); + +var _fs2 = _interopRequireDefault(_fs); var _child_process = require('child_process'); @@ -14,6 +30,8 @@ var _childprocessPromise = require('childprocess-promise'); var _childprocessPromise2 = _interopRequireDefault(_childprocessPromise); +var _atomLinter = require('atom-linter'); + function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function spawnWorker() { @@ -46,4 +64,107 @@ function spawnWorker() { killer(); process.removeListener('exit', killer); } } }; +} + +function getModulesDirectory(fileDir) { + return (0, _atomLinter.findFile)(fileDir, 'node_modules'); +} + +function getIgnoresFile(fileDir) { + return _path2.default.dirname((0, _atomLinter.findFile)(fileDir, '.eslintignore')); +} + +function getEslintFromDirectory(path) { + try { + return require(_path2.default.join(path, 'lib', 'cli.js')); + } catch (e) { + if (e.code === 'MODULE_NOT_FOUND') { + throw new Error('ESLint not found, Please install or make sure Atom is getting $PATH correctly'); + } else throw e; + } +} + +let nodePrefixPath = null; + +function getNodePrefixPath() { + if (nodePrefixPath === null) { + const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm'; + try { + nodePrefixPath = _child_process2.default.spawnSync(npmCommand, ['get', 'prefix']).output[1].toString().trim(); + } catch (e) { + throw new Error('Unable to execute `npm get prefix`. Please make sure Atom is getting $PATH correctly'); + } + } + return nodePrefixPath; +} + +let bundledEslintDirectory = null; + +function getBundledEslintDirectory() { + if (bundledEslintDirectory === null) { + bundledEslintDirectory = _path2.default.normalize(_path2.default.join(__dirname, '..', 'node_modules', 'eslint')); + } + return bundledEslintDirectory; +} + +function getEslintDirectory(params) { + let modulesPath = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1]; + + if (params.global) { + const prefixPath = getNodePrefixPath(); + if (process.platform === 'win32') { + return _path2.default.join(params.nodePath || prefixPath, 'node_modules', 'eslint'); + } else { + return _path2.default.join(params.nodePath || prefixPath, 'lib', 'node_modules', 'eslint'); + } + } else { + const eslintPath = _path2.default.join(modulesPath || getModulesDirectory(params.fileDir), 'eslint'); + try { + _fs2.default.accessSync(eslintPath, _fs2.default.R_OK); + return eslintPath; + } catch (_) { + return getBundledEslintDirectory(); + } + } +} + +function getEslintConfig(params) { + const configFile = (0, _atomLinter.findFile)(params.fileDir, ['.eslintrc.js', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json', '.eslintrc']) || null; + if (configFile) { + return configFile; + } + + const packagePath = (0, _atomLinter.findFile)(params.fileDir, 'package.json'); + if (packagePath && Boolean(require(packagePath).eslintConfig)) { + return packagePath; + } + + if (params.canDisable) { + return null; + } + + if (params.configFile) { + return params.configFile; + } +} + +let eslint; +let lastEslintDirectory; +let lastModulesPath; + +function getEslint(params) { + const modulesPath = getModulesDirectory(params.fileDir); + const eslintDirectory = getEslintDirectory(params, modulesPath); + if (eslintDirectory !== lastEslintDirectory) { + lastEslintDirectory = eslintDirectory; + eslint = getEslintFromDirectory(eslintDirectory); + } + if (lastModulesPath !== modulesPath) { + lastModulesPath = modulesPath; + if (modulesPath) { + process.env.NODE_PATH = modulesPath; + } else process.env.NODE_PATH = ''; + require('module').Module._initPaths(); + } + return { eslint: eslint, eslintDirectory: eslintDirectory }; } \ No newline at end of file diff --git a/lib/worker.js b/lib/worker.js index 3a950187..92364344 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -2,62 +2,40 @@ // Note: 'use babel' doesn't work in forked processes process.title = 'linter-eslint helper' -const CP = require('childprocess-promise') -const execFileSync = require('child_process').execFileSync -const Path = require('path') +import Path from 'path' +import {execFileSync} from 'child_process' +import CP from 'childprocess-promise' +import resolveEnv from 'resolve-end' +import * as Helpers from './helpers' -const resolveEnv = require('resolve-env') -const Helpers = require('./es5-helpers') - -const findEslintDir = Helpers.findEslintDir -const find = Helpers.find -const determineConfigFile = Helpers.determineConfigFile -const getEslintCli = Helpers.getEslintCli const Communication = new CP() -// closed-over module-scope variables -let eslintPath = null -let eslint = null - -Communication.on('JOB', function(job) { - const params = job.Message - const modulesPath = find(params.fileDir, 'node_modules') - const eslintignoreDir = Path.dirname(find(params.fileDir, '.eslintignore')) - let configFile = null +Communication.on('JOB', function(Job) { global.__LINTER_RESPONSE = [] - // Check for config file and determine whether to bail out - configFile = determineConfigFile(params) + const params = Job.Message + const ignoreFile = Helpers.getIgnoresFile(params.fileDir) + const configFile = Helpers.getEslintConfig(params.fileDir) + const {eslint, eslintDirectory} = Helpers.getEslint(params) if (params.canDisable && configFile === null) { - job.Response = [] - return + return Job.Response = [] } - if (modulesPath) { - process.env.NODE_PATH = modulesPath - } else process.env.NODE_PATH = '' - require('module').Module._initPaths() - // Determine which eslint instance to use - const eslintNewPath = findEslintDir(params) - if (eslintNewPath !== eslintPath) { - eslint = getEslintCli(eslintNewPath) - eslintPath = eslintNewPath - } - - job.Response = new Promise(function(resolve) { + Job.Response = new Promise(function(resolve) { let filePath - if (eslintignoreDir) { - filePath = Path.relative(eslintignoreDir, params.filePath) - process.chdir(eslintignoreDir) + if (ignoreFile) { + filePath = Path.relative(ignoreFile, params.filePath) + process.chdir(ignoreFile) } else { filePath = Path.basename(params.filePath) process.chdir(params.fileDir) } + const argv = [ process.execPath, - eslintPath, + eslintDirectory, '--stdin', '--format', Path.join(__dirname, 'reporter.js') @@ -77,33 +55,35 @@ Communication.on('JOB', function(job) { } argv.push('--stdin-filename', filePath) process.argv = argv + eslint.execute(process.argv, params.contents) resolve(global.__LINTER_RESPONSE) }) }) -Communication.on('FIX', function(fixJob) { - const params = fixJob.Message - const eslintDir = findEslintDir(params) - const configFile = determineConfigFile(params) - const eslintBinPath = Path.normalize(Path.join(eslintDir, 'bin', 'eslint.js')) +Communication.on('FIX', function(Job) { + const params = Job.Message + const {eslint, eslintDirectory} = Helpers.getEslint(params) + const configFile = Helpers.getEslintConfig(params) const argv = [ + process.execPath, + eslintDirectory, params.filePath, '--fix' ] if (configFile !== null) { argv.push('--config', resolveEnv(configFile)) } + process.argv = argv + process.chdir(params.fileDir) - fixJob.Response = new Promise(function(resolve, reject) { - try { - execFileSync(eslintBinPath, argv, {cwd: params.fileDir}) - } catch (err) { - reject('Linter-ESLint: Fix Attempt Completed, Linting Errors Remain') - } - resolve('Linter-ESLint: Fix Complete') - }) + try { + eslint.execute(process.argv) + } catch (_) { + throw new Error('Linter-ESLint: Fix Attempt Completed, Linting Errors Remain') + } + return 'Linter-ESLint: Fix Complete' }) process.exit = function() { /* Stop eslint from closing the daemon */ } diff --git a/src/helpers.js b/src/helpers.js index 072c5248..0224a1ee 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -79,7 +79,7 @@ export function getBundledEslintDirectory() { return bundledEslintDirectory } -export function getEslintDirectory(params, modulesPath) { +export function getEslintDirectory(params, modulesPath = null) { if (params.global) { const prefixPath = getNodePrefixPath() if (process.platform === 'win32') { @@ -88,7 +88,7 @@ export function getEslintDirectory(params, modulesPath) { return Path.join(params.nodePath || prefixPath, 'lib', 'node_modules', 'eslint') } } else { - const eslintPath = Path.join(modulesPath, 'eslint') + const eslintPath = Path.join(modulesPath || getModulesDirectory(params.fileDir), 'eslint') try { FS.accessSync(eslintPath, FS.R_OK) return eslintPath @@ -117,3 +117,24 @@ export function getEslintConfig(params) { return params.configFile } } + +let eslint +let lastEslintDirectory +let lastModulesPath + +export function getEslint(params) { + const modulesPath = getModulesDirectory(params.fileDir) + const eslintDirectory = getEslintDirectory(params, modulesPath) + if (eslintDirectory !== lastEslintDirectory) { + lastEslintDirectory = eslintDirectory + eslint = getEslintFromDirectory(eslintDirectory) + } + if (lastModulesPath !== modulesPath) { + lastModulesPath = modulesPath + if (modulesPath) { + process.env.NODE_PATH = modulesPath + } else process.env.NODE_PATH = '' + require('module').Module._initPaths() + } + return {eslint, eslintDirectory} +} diff --git a/src/worker.js b/src/worker.js index 13117b1a..92364344 100644 --- a/src/worker.js +++ b/src/worker.js @@ -10,31 +10,18 @@ import * as Helpers from './helpers' const Communication = new CP() -let eslint -let lastEslintDirectory - Communication.on('JOB', function(Job) { global.__LINTER_RESPONSE = [] const params = Job.Message - const modulesPath = Helpers.getModulesDirectory(params.fileDir) const ignoreFile = Helpers.getIgnoresFile(params.fileDir) const configFile = Helpers.getEslintConfig(params.fileDir) - const eslintDirectory = Helpers.getEslintDirectory(params, modulesPath) + const {eslint, eslintDirectory} = Helpers.getEslint(params) if (params.canDisable && configFile === null) { return Job.Response = [] } - if (eslintDirectory !== lastEslintDirectory) { - lastEslintDirectory = eslintDirectory - eslint = Helpers.getEslintFromDirectory(eslintDirectory) - } - - if (modulesPath) { - process.env.NODE_PATH = modulesPath - } else process.env.NODE_PATH = '' - require('module').Module._initPaths() Job.Response = new Promise(function(resolve) { let filePath @@ -74,28 +61,29 @@ Communication.on('JOB', function(Job) { }) }) -Communication.on('FIX', function(fixJob) { - const params = fixJob.Message - const eslintDir = findEslintDir(params) - const configFile = determineConfigFile(params) - const eslintBinPath = Path.normalize(Path.join(eslintDir, 'bin', 'eslint.js')) +Communication.on('FIX', function(Job) { + const params = Job.Message + const {eslint, eslintDirectory} = Helpers.getEslint(params) + const configFile = Helpers.getEslintConfig(params) const argv = [ + process.execPath, + eslintDirectory, params.filePath, '--fix' ] if (configFile !== null) { argv.push('--config', resolveEnv(configFile)) } + process.argv = argv + process.chdir(params.fileDir) - fixJob.Response = new Promise(function(resolve, reject) { - try { - execFileSync(eslintBinPath, argv, {cwd: params.fileDir}) - } catch (err) { - reject('Linter-ESLint: Fix Attempt Completed, Linting Errors Remain') - } - resolve('Linter-ESLint: Fix Complete') - }) + try { + eslint.execute(process.argv) + } catch (_) { + throw new Error('Linter-ESLint: Fix Attempt Completed, Linting Errors Remain') + } + return 'Linter-ESLint: Fix Complete' }) process.exit = function() { /* Stop eslint from closing the daemon */ } From 1b0aa254fc1ffda691636a580a8e273987cfa463 Mon Sep 17 00:00:00 2001 From: steelbrain Date: Mon, 30 Nov 2015 17:52:27 -0700 Subject: [PATCH 11/15] :art: Only run ucompiler on src directory --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e086c186..98bf63b3 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ }, "scripts": { "lint": "eslint .", - "compile": "ucompiler go" + "compile": "ucompiler go src" }, "dependencies": { "atom-linter": "^3.4.0", From 5f02fdcfa47d12f8e4c1b56b685b6b566397c0d6 Mon Sep 17 00:00:00 2001 From: steelbrain Date: Mon, 30 Nov 2015 18:20:30 -0700 Subject: [PATCH 12/15] :new: Add steelbrain-legacy as a babel preset --- .ucompilerrc | 2 +- lib/main.js | 36 ++++++++++++++++++++---------------- package.json | 10 +++++++--- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/.ucompilerrc b/.ucompilerrc index caf5754e..59cea202 100644 --- a/.ucompilerrc +++ b/.ucompilerrc @@ -4,7 +4,7 @@ "path": "src/*.js", "outputPath": "lib/{name}.js", "babel": { - "presets": ["steelbrain"] + "presets": ["steelbrain", "steelbrain-legacy"] } }] } diff --git a/lib/main.js b/lib/main.js index 22d30e3e..96c2e4e5 100644 --- a/lib/main.js +++ b/lib/main.js @@ -68,6 +68,8 @@ exports.default = { } }, activate: function () { + var _this = this; + require('atom-package-deps').install(); this.subscriptions = new _atom.CompositeDisposable(); @@ -76,17 +78,17 @@ exports.default = { this.scopes = ['source.js', 'source.jsx', 'source.js.jsx', 'source.babel', 'source.js-semantic']; const embeddedScope = 'source.js.embedded.html'; - this.subscriptions.add(atom.config.observe('linter-eslint.lintHtmlFiles', lintHtmlFiles => { + this.subscriptions.add(atom.config.observe('linter-eslint.lintHtmlFiles', function (lintHtmlFiles) { if (lintHtmlFiles) { - this.scopes.push(embeddedScope); + _this.scopes.push(embeddedScope); } else { - if (this.scopes.indexOf(embeddedScope) !== -1) { - this.scopes.splice(this.scopes.indexOf(embeddedScope), 1); + if (_this.scopes.indexOf(embeddedScope) !== -1) { + _this.scopes.splice(_this.scopes.indexOf(embeddedScope), 1); } } })); this.subscriptions.add(atom.commands.add('atom-text-editor', { - 'linter-eslint:fix-file': () => { + 'linter-eslint:fix-file': function () { const textEditor = atom.workspace.getActiveTextEditor(); const filePath = textEditor.getPath(); const fileDir = _path2.default.dirname(filePath); @@ -97,13 +99,13 @@ exports.default = { return; } - if (this.worker === null) { + if (_this.worker === null) { // Abort if worker is not yet ready atom.notifications.addError('Linter-ESLint: Not ready, please try again'); return; } - this.worker.request('FIX', { + _this.worker.request('FIX', { fileDir: fileDir, filePath: filePath, global: atom.config.get('linter-eslint.useGlobalEslint'), @@ -121,18 +123,18 @@ exports.default = { // process while atom is starting up, it can increase startup // time by several seconds, But if we do this after 5 seconds, // we barely feel a thing. - const initializeWorker = () => { - if (this.active) { + const initializeWorker = function () { + if (_this.active) { var _spawnWorker = (0, _helpers.spawnWorker)(); const child = _spawnWorker.child; const worker = _spawnWorker.worker; const subscription = _spawnWorker.subscription; - this.worker = worker; - this.subscriptions.add(subscription); - child.on('exit-linter', shouldLive => { - this.worker = null; + _this.worker = worker; + _this.subscriptions.add(subscription); + child.on('exit-linter', function (shouldLive) { + _this.worker = null; // Respawn if it crashed. See atom/electron#3446 if (shouldLive) { initializeWorker(); @@ -147,13 +149,15 @@ exports.default = { this.subscriptions.dispose(); }, provideLinter: function () { + var _this2 = this; + const Helpers = require('atom-linter'); return { name: 'ESLint', grammarScopes: this.scopes, scope: 'file', lintOnFly: true, - lint: textEditor => { + lint: function (textEditor) { const text = textEditor.getText(); if (text.length === 0) { return Promise.resolve([]); @@ -162,7 +166,7 @@ exports.default = { const fileDir = _path2.default.dirname(filePath); const showRule = atom.config.get('linter-eslint.showRuleIdInMessage'); - if (this.worker === null) { + if (_this2.worker === null) { return Promise.resolve([{ filePath: filePath, type: 'Info', @@ -171,7 +175,7 @@ exports.default = { }]); } - return this.worker.request('JOB', { + return _this2.worker.request('JOB', { fileDir: fileDir, filePath: filePath, contents: text, diff --git a/package.json b/package.json index 98bf63b3..2c616db2 100644 --- a/package.json +++ b/package.json @@ -23,12 +23,13 @@ }, "devDependencies": { "babel-eslint": "^4.1.5", + "babel-preset-steelbrain": "^1.0.0", + "babel-preset-steelbrain-legacy": "^1.0.0", "eslint-config-airbnb": "latest", "eslint-config-steelbrain": "latest", "eslint-plugin-import": "^0.11.0", "eslint-plugin-react": "^3.10.0", - "shelljs": "^0.5.3", - "babel-preset-steelbrain": "^1.0.0" + "shelljs": "^0.5.3" }, "package-deps": [ "linter" @@ -46,7 +47,10 @@ "no-empty": 0, "no-console": 0, "no-new": 0, - "semi": [2, "never"] + "semi": [ + 2, + "never" + ] }, "parser": "babel-eslint", "globals": { From 6e016abce5d6dc522db7b5bf7a7f73a8b3c302bf Mon Sep 17 00:00:00 2001 From: steelbrain Date: Mon, 30 Nov 2015 18:53:52 -0700 Subject: [PATCH 13/15] :fire: Remove timeout on worker spawn --- lib/main.js | 21 +-------------------- src/main.js | 21 +-------------------- 2 files changed, 2 insertions(+), 40 deletions(-) diff --git a/lib/main.js b/lib/main.js index 96c2e4e5..6ed315cc 100644 --- a/lib/main.js +++ b/lib/main.js @@ -99,12 +99,6 @@ exports.default = { return; } - if (_this.worker === null) { - // Abort if worker is not yet ready - atom.notifications.addError('Linter-ESLint: Not ready, please try again'); - return; - } - _this.worker.request('FIX', { fileDir: fileDir, filePath: filePath, @@ -119,10 +113,6 @@ exports.default = { } })); - // Reason: I (steelbrain) have observed that if we spawn a - // process while atom is starting up, it can increase startup - // time by several seconds, But if we do this after 5 seconds, - // we barely feel a thing. const initializeWorker = function () { if (_this.active) { var _spawnWorker = (0, _helpers.spawnWorker)(); @@ -142,7 +132,7 @@ exports.default = { }); } }; - setTimeout(initializeWorker, 5 * 1000); + initializeWorker(); }, deactivate: function () { this.active = false; @@ -166,15 +156,6 @@ exports.default = { const fileDir = _path2.default.dirname(filePath); const showRule = atom.config.get('linter-eslint.showRuleIdInMessage'); - if (_this2.worker === null) { - return Promise.resolve([{ - filePath: filePath, - type: 'Info', - text: 'Worker initialization is delayed. Please try saving or typing to begin linting.', - range: Helpers.rangeFromLineNumber(textEditor, 0) - }]); - } - return _this2.worker.request('JOB', { fileDir: fileDir, filePath: filePath, diff --git a/src/main.js b/src/main.js index e231856b..5f48bc12 100644 --- a/src/main.js +++ b/src/main.js @@ -83,12 +83,6 @@ export default { return } - if (this.worker === null) { - // Abort if worker is not yet ready - atom.notifications.addError('Linter-ESLint: Not ready, please try again') - return - } - this.worker.request('FIX', { fileDir: fileDir, filePath: filePath, @@ -103,10 +97,6 @@ export default { } })) - // Reason: I (steelbrain) have observed that if we spawn a - // process while atom is starting up, it can increase startup - // time by several seconds, But if we do this after 5 seconds, - // we barely feel a thing. const initializeWorker = () => { if (this.active) { const {child, worker, subscription} = spawnWorker() @@ -121,7 +111,7 @@ export default { }) } } - setTimeout(initializeWorker, 5 * 1000) + initializeWorker() }, deactivate: function() { this.active = false @@ -143,15 +133,6 @@ export default { const fileDir = Path.dirname(filePath) const showRule = atom.config.get('linter-eslint.showRuleIdInMessage') - if (this.worker === null) { - return Promise.resolve([{ - filePath: filePath, - type: 'Info', - text: 'Worker initialization is delayed. Please try saving or typing to begin linting.', - range: Helpers.rangeFromLineNumber(textEditor, 0) - }]) - } - return this.worker.request('JOB', { fileDir: fileDir, filePath: filePath, From 553f4541570fb157afa3e9b88ea8a2375399cced Mon Sep 17 00:00:00 2001 From: Spain Date: Tue, 1 Dec 2015 18:08:22 -0500 Subject: [PATCH 14/15] :traffic_light: CI: Update configs for CI services * Setup CI for a lint, build, test sequence * Remove ucompiler, just use babel directly * Add command for publishing with apm (`npm run apm-publish -- `) --- .eslintignore | 1 + .gitignore | 6 +-- .travis.yml | 5 +- appveyor.yml | 2 + circle.yml | 14 +++--- lib/helpers.js | 100 +++++++++++++------------------------ lib/main.js | 79 ++++++++++------------------- lib/reporter.js | 9 ++-- lib/worker.js | 105 ++++++++++++++++++--------------------- package.json | 9 ++-- spec/es5-helpers-spec.js | 2 +- src/helpers.js | 18 +++---- src/worker.js | 4 +- 13 files changed, 145 insertions(+), 209 deletions(-) diff --git a/.eslintignore b/.eslintignore index 59dc28f1..9011ad48 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1 +1,2 @@ spec/fixtures/**/*.js +lib/**/*.js diff --git a/.gitignore b/.gitignore index 07ff0ddf..360b2a64 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,4 @@ -node_modules +/lib/ +/node_modules/ *.log .idea - -# Keep node_modules test fixtures -!spec/fixtures/local-eslint/node_modules diff --git a/.travis.yml b/.travis.yml index 274d083f..743cd919 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,4 +10,7 @@ notifications: on_failure: change os: osx -script: 'curl -s https://raw.githubusercontent.com/atom/ci/master/build-package.sh | sh' +script: + - npm i + - npm run compile + - 'curl -s https://raw.githubusercontent.com/atom/ci/master/build-package.sh | sh' diff --git a/appveyor.yml b/appveyor.yml index 94066a95..51b2fc0f 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -14,5 +14,7 @@ build_script: - cd %APPVEYOR_BUILD_FOLDER% - SET PATH=%LOCALAPPDATA%\atom\bin;%PATH% - apm clean + - npm i + - npm run compile - apm install - apm test diff --git a/circle.yml b/circle.yml index cc91716f..daf8ff20 100644 --- a/circle.yml +++ b/circle.yml @@ -1,13 +1,15 @@ dependencies: - override: - - wget -O atom-amd64.deb https://atom.io/download/deb + pre: - sudo apt-get update + - sudo apt-get install wget + post: + - wget -O atom-amd64.deb https://atom.io/download/deb - sudo dpkg --install atom-amd64.deb || true - sudo apt-get -f install -y - - atom -v - - apm install test: override: - - ./node_modules/.bin/eslint lib - - ./node_modules/.bin/eslint spec + - npm run lint + - npm run compile + - atom -v + - apm install - apm test diff --git a/lib/helpers.js b/lib/helpers.js index e083cfd6..47f83d99 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -1,47 +1,19 @@ -'use strict'; 'use babel'; -Object.defineProperty(exports, "__esModule", { - value: true -}); -exports.spawnWorker = spawnWorker; -exports.getModulesDirectory = getModulesDirectory; -exports.getIgnoresFile = getIgnoresFile; -exports.getEslintFromDirectory = getEslintFromDirectory; -exports.getNodePrefixPath = getNodePrefixPath; -exports.getBundledEslintDirectory = getBundledEslintDirectory; -exports.getEslintDirectory = getEslintDirectory; -exports.getEslintConfig = getEslintConfig; -exports.getEslint = getEslint; +import Path from 'path'; +import FS from 'fs'; +import ChildProcess from 'child_process'; +import CP from 'childprocess-promise'; +import { findFile as find } from 'atom-linter'; -var _path = require('path'); - -var _path2 = _interopRequireDefault(_path); - -var _fs = require('fs'); - -var _fs2 = _interopRequireDefault(_fs); - -var _child_process = require('child_process'); - -var _child_process2 = _interopRequireDefault(_child_process); - -var _childprocessPromise = require('childprocess-promise'); - -var _childprocessPromise2 = _interopRequireDefault(_childprocessPromise); - -var _atomLinter = require('atom-linter'); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -function spawnWorker() { +export function spawnWorker() { let shouldLive = true; const env = Object.create(process.env); delete env.NODE_PATH; delete env.NODE_ENV; const data = { stdout: [], stderr: [] }; - const child = _child_process2.default.fork(__dirname + '/worker.js', [], { env: env, silent: true }); - const worker = new _childprocessPromise2.default(child); + const child = ChildProcess.fork(__dirname + '/worker.js', [], { env, silent: true }); + const worker = new CP(child); function killer() { shouldLive = false; child.kill(); @@ -60,23 +32,23 @@ function spawnWorker() { child.emit('exit-linter', shouldLive); }); process.on('exit', killer); - return { child: child, worker: worker, subscription: { dispose: function () { + return { child, worker, subscription: { dispose: function () { killer(); process.removeListener('exit', killer); } } }; } -function getModulesDirectory(fileDir) { - return (0, _atomLinter.findFile)(fileDir, 'node_modules'); +export function getModulesDirectory(fileDir) { + return find(fileDir, 'node_modules'); } -function getIgnoresFile(fileDir) { - return _path2.default.dirname((0, _atomLinter.findFile)(fileDir, '.eslintignore')); +export function getIgnoresFile(fileDir) { + return Path.dirname(find(fileDir, '.eslintignore')); } -function getEslintFromDirectory(path) { +export function getEslintFromDirectory(path) { try { - return require(_path2.default.join(path, 'lib', 'cli.js')); + return require(Path.join(path, 'lib', 'cli.js')); } catch (e) { if (e.code === 'MODULE_NOT_FOUND') { throw new Error('ESLint not found, Please install or make sure Atom is getting $PATH correctly'); @@ -86,11 +58,11 @@ function getEslintFromDirectory(path) { let nodePrefixPath = null; -function getNodePrefixPath() { +export function getNodePrefixPath() { if (nodePrefixPath === null) { const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm'; try { - nodePrefixPath = _child_process2.default.spawnSync(npmCommand, ['get', 'prefix']).output[1].toString().trim(); + nodePrefixPath = ChildProcess.spawnSync(npmCommand, ['get', 'prefix']).output[1].toString().trim(); } catch (e) { throw new Error('Unable to execute `npm get prefix`. Please make sure Atom is getting $PATH correctly'); } @@ -100,41 +72,37 @@ function getNodePrefixPath() { let bundledEslintDirectory = null; -function getBundledEslintDirectory() { +export function getBundledEslintDirectory() { if (bundledEslintDirectory === null) { - bundledEslintDirectory = _path2.default.normalize(_path2.default.join(__dirname, '..', 'node_modules', 'eslint')); + bundledEslintDirectory = Path.normalize(Path.join(__dirname, '..', 'node_modules', 'eslint')); } return bundledEslintDirectory; } -function getEslintDirectory(params) { - let modulesPath = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1]; - +export function getEslintDirectory(params, modulesPath = null) { if (params.global) { const prefixPath = getNodePrefixPath(); if (process.platform === 'win32') { - return _path2.default.join(params.nodePath || prefixPath, 'node_modules', 'eslint'); - } else { - return _path2.default.join(params.nodePath || prefixPath, 'lib', 'node_modules', 'eslint'); - } - } else { - const eslintPath = _path2.default.join(modulesPath || getModulesDirectory(params.fileDir), 'eslint'); - try { - _fs2.default.accessSync(eslintPath, _fs2.default.R_OK); - return eslintPath; - } catch (_) { - return getBundledEslintDirectory(); + return Path.join(params.nodePath || prefixPath, 'node_modules', 'eslint'); } + return Path.join(params.nodePath || prefixPath, 'lib', 'node_modules', 'eslint'); + } + const eslintPath = Path.join(modulesPath || getModulesDirectory(params.fileDir), 'eslint'); + try { + FS.accessSync(eslintPath, FS.R_OK); + return eslintPath; + } catch (_) { + return getBundledEslintDirectory(); } } -function getEslintConfig(params) { - const configFile = (0, _atomLinter.findFile)(params.fileDir, ['.eslintrc.js', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json', '.eslintrc']) || null; +export function getEslintConfig(params) { + const configFile = find(params.fileDir, ['.eslintrc.js', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json', '.eslintrc']) || null; if (configFile) { return configFile; } - const packagePath = (0, _atomLinter.findFile)(params.fileDir, 'package.json'); + const packagePath = find(params.fileDir, 'package.json'); if (packagePath && Boolean(require(packagePath).eslintConfig)) { return packagePath; } @@ -152,7 +120,7 @@ let eslint; let lastEslintDirectory; let lastModulesPath; -function getEslint(params) { +export function getEslint(params) { const modulesPath = getModulesDirectory(params.fileDir); const eslintDirectory = getEslintDirectory(params, modulesPath); if (eslintDirectory !== lastEslintDirectory) { @@ -166,5 +134,5 @@ function getEslint(params) { } else process.env.NODE_PATH = ''; require('module').Module._initPaths(); } - return { eslint: eslint, eslintDirectory: eslintDirectory }; + return { eslint, eslintDirectory }; } \ No newline at end of file diff --git a/lib/main.js b/lib/main.js index 6ed315cc..ecb1ddea 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1,25 +1,11 @@ -'use strict'; 'use babel'; -Object.defineProperty(exports, "__esModule", { - value: true -}); +import Path from 'path'; +import { CompositeDisposable } from 'atom'; +import { spawnWorker } from './helpers'; +import escapeHTML from 'escape-html'; -var _path = require('path'); - -var _path2 = _interopRequireDefault(_path); - -var _atom = require('atom'); - -var _helpers = require('./helpers'); - -var _escapeHtml = require('escape-html'); - -var _escapeHtml2 = _interopRequireDefault(_escapeHtml); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } - -exports.default = { +export default { config: { lintHtmlFiles: { title: 'Lint HTML Files', @@ -68,30 +54,28 @@ exports.default = { } }, activate: function () { - var _this = this; - require('atom-package-deps').install(); - this.subscriptions = new _atom.CompositeDisposable(); + this.subscriptions = new CompositeDisposable(); this.active = true; this.worker = null; this.scopes = ['source.js', 'source.jsx', 'source.js.jsx', 'source.babel', 'source.js-semantic']; const embeddedScope = 'source.js.embedded.html'; - this.subscriptions.add(atom.config.observe('linter-eslint.lintHtmlFiles', function (lintHtmlFiles) { + this.subscriptions.add(atom.config.observe('linter-eslint.lintHtmlFiles', lintHtmlFiles => { if (lintHtmlFiles) { - _this.scopes.push(embeddedScope); + this.scopes.push(embeddedScope); } else { - if (_this.scopes.indexOf(embeddedScope) !== -1) { - _this.scopes.splice(_this.scopes.indexOf(embeddedScope), 1); + if (this.scopes.indexOf(embeddedScope) !== -1) { + this.scopes.splice(this.scopes.indexOf(embeddedScope), 1); } } })); this.subscriptions.add(atom.commands.add('atom-text-editor', { - 'linter-eslint:fix-file': function () { + 'linter-eslint:fix-file': () => { const textEditor = atom.workspace.getActiveTextEditor(); const filePath = textEditor.getPath(); - const fileDir = _path2.default.dirname(filePath); + const fileDir = Path.dirname(filePath); if (!textEditor || textEditor.isModified()) { // Abort for invalid or unsaved text editors @@ -99,7 +83,7 @@ exports.default = { return; } - _this.worker.request('FIX', { + this.worker.request('FIX', { fileDir: fileDir, filePath: filePath, global: atom.config.get('linter-eslint.useGlobalEslint'), @@ -113,18 +97,13 @@ exports.default = { } })); - const initializeWorker = function () { - if (_this.active) { - var _spawnWorker = (0, _helpers.spawnWorker)(); - - const child = _spawnWorker.child; - const worker = _spawnWorker.worker; - const subscription = _spawnWorker.subscription; - - _this.worker = worker; - _this.subscriptions.add(subscription); - child.on('exit-linter', function (shouldLive) { - _this.worker = null; + const initializeWorker = () => { + if (this.active) { + const { child, worker, subscription } = spawnWorker(); + this.worker = worker; + this.subscriptions.add(subscription); + child.on('exit-linter', shouldLive => { + this.worker = null; // Respawn if it crashed. See atom/electron#3446 if (shouldLive) { initializeWorker(); @@ -139,24 +118,22 @@ exports.default = { this.subscriptions.dispose(); }, provideLinter: function () { - var _this2 = this; - const Helpers = require('atom-linter'); return { name: 'ESLint', grammarScopes: this.scopes, scope: 'file', lintOnFly: true, - lint: function (textEditor) { + lint: textEditor => { const text = textEditor.getText(); if (text.length === 0) { return Promise.resolve([]); } const filePath = textEditor.getPath(); - const fileDir = _path2.default.dirname(filePath); + const fileDir = Path.dirname(filePath); const showRule = atom.config.get('linter-eslint.showRuleIdInMessage'); - return _this2.worker.request('JOB', { + return this.worker.request('JOB', { fileDir: fileDir, filePath: filePath, contents: text, @@ -170,13 +147,7 @@ exports.default = { if (response.length === 1 && response[0].message === 'File ignored because of your .eslintignore file. Use --no-ignore to override.') { return []; } - return response.map(function (_ref) { - let message = _ref.message; - let line = _ref.line; - let severity = _ref.severity; - let ruleId = _ref.ruleId; - let column = _ref.column; - + return response.map(function ({ message, line, severity, ruleId, column }) { const range = Helpers.rangeFromLineNumber(textEditor, line - 1); if (column) { range[0][1] = column - 1; @@ -190,7 +161,7 @@ exports.default = { range: range }; if (showRule) { - ret.html = `${ ruleId || 'Fatal' } ${ (0, _escapeHtml2.default)(message) }`; + ret.html = `${ ruleId || 'Fatal' } ${ escapeHTML(message) }`; } else { ret.text = message; } diff --git a/lib/reporter.js b/lib/reporter.js index 1eea8bec..366917ea 100644 --- a/lib/reporter.js +++ b/lib/reporter.js @@ -1,5 +1,6 @@ -'use strict' +'use strict'; // This file is used by eslint to hand the errors over to the worker -module.exports = function(results) { - global.__LINTER_RESPONSE = results[0].messages -} + +module.exports = function (results) { + global.__LINTER_RESPONSE = results[0].messages; +}; \ No newline at end of file diff --git a/lib/worker.js b/lib/worker.js index 92364344..ed57636c 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -1,89 +1,78 @@ -'use strict' +'use strict'; // Note: 'use babel' doesn't work in forked processes -process.title = 'linter-eslint helper' -import Path from 'path' -import {execFileSync} from 'child_process' -import CP from 'childprocess-promise' -import resolveEnv from 'resolve-end' -import * as Helpers from './helpers' +process.title = 'linter-eslint helper'; -const Communication = new CP() +import Path from 'path'; +import CP from 'childprocess-promise'; +import resolveEnv from 'resolve-end'; +import * as Helpers from './helpers'; -Communication.on('JOB', function(Job) { - global.__LINTER_RESPONSE = [] +const Communication = new CP(); - const params = Job.Message - const ignoreFile = Helpers.getIgnoresFile(params.fileDir) - const configFile = Helpers.getEslintConfig(params.fileDir) - const {eslint, eslintDirectory} = Helpers.getEslint(params) +Communication.on('JOB', function (Job) { + global.__LINTER_RESPONSE = []; + + const params = Job.Message; + const ignoreFile = Helpers.getIgnoresFile(params.fileDir); + const configFile = Helpers.getEslintConfig(params.fileDir); + const { eslint, eslintDirectory } = Helpers.getEslint(params); if (params.canDisable && configFile === null) { - return Job.Response = [] + Job.Response = []; + return Job.Response; } - - Job.Response = new Promise(function(resolve) { - let filePath + Job.Response = new Promise(function (resolve) { + let filePath; if (ignoreFile) { - filePath = Path.relative(ignoreFile, params.filePath) - process.chdir(ignoreFile) + filePath = Path.relative(ignoreFile, params.filePath); + process.chdir(ignoreFile); } else { - filePath = Path.basename(params.filePath) - process.chdir(params.fileDir) + filePath = Path.basename(params.filePath); + process.chdir(params.fileDir); } - const argv = [ - process.execPath, - eslintDirectory, - '--stdin', - '--format', - Path.join(__dirname, 'reporter.js') - ] + const argv = [process.execPath, eslintDirectory, '--stdin', '--format', Path.join(__dirname, 'reporter.js')]; if (params.rulesDir) { - let rulesDir = resolveEnv(params.rulesDir) + let rulesDir = resolveEnv(params.rulesDir); if (!Path.isAbsolute(rulesDir)) { - rulesDir = find(params.fileDir, rulesDir) + rulesDir = find(params.fileDir, rulesDir); } - argv.push('--rulesdir', rulesDir) + argv.push('--rulesdir', rulesDir); } if (configFile !== null) { - argv.push('--config', resolveEnv(configFile)) + argv.push('--config', resolveEnv(configFile)); } if (params.disableIgnores) { - argv.push('--no-ignore') + argv.push('--no-ignore'); } - argv.push('--stdin-filename', filePath) - process.argv = argv + argv.push('--stdin-filename', filePath); + process.argv = argv; - eslint.execute(process.argv, params.contents) - resolve(global.__LINTER_RESPONSE) - }) -}) + eslint.execute(process.argv, params.contents); + resolve(global.__LINTER_RESPONSE); + }); +}); -Communication.on('FIX', function(Job) { - const params = Job.Message - const {eslint, eslintDirectory} = Helpers.getEslint(params) - const configFile = Helpers.getEslintConfig(params) +Communication.on('FIX', function (Job) { + const params = Job.Message; + const { eslint, eslintDirectory } = Helpers.getEslint(params); + const configFile = Helpers.getEslintConfig(params); - const argv = [ - process.execPath, - eslintDirectory, - params.filePath, - '--fix' - ] + const argv = [process.execPath, eslintDirectory, params.filePath, '--fix']; if (configFile !== null) { - argv.push('--config', resolveEnv(configFile)) + argv.push('--config', resolveEnv(configFile)); } - process.argv = argv - process.chdir(params.fileDir) + process.argv = argv; + process.chdir(params.fileDir); try { - eslint.execute(process.argv) + eslint.execute(process.argv); } catch (_) { - throw new Error('Linter-ESLint: Fix Attempt Completed, Linting Errors Remain') + throw new Error('Linter-ESLint: Fix Attempt Completed, Linting Errors Remain'); } - return 'Linter-ESLint: Fix Complete' -}) + return 'Linter-ESLint: Fix Complete'; +}); -process.exit = function() { /* Stop eslint from closing the daemon */ } +process.exit = function () {/* Stop eslint from closing the daemon */}; \ No newline at end of file diff --git a/package.json b/package.json index 2c616db2..e11c0b4a 100644 --- a/package.json +++ b/package.json @@ -7,11 +7,13 @@ "repository": "https://github.com/AtomLinter/linter-eslint.git", "license": "MIT", "engines": { - "atom": ">0.50.0" + "atom": ">=1.0.0" }, "scripts": { + "clean": "rm -rf lib", "lint": "eslint .", - "compile": "ucompiler go src" + "compile": "babel src --out-dir lib", + "apm-publish": "npm run clean && npm run compile && apm publish" }, "dependencies": { "atom-linter": "^3.4.0", @@ -22,7 +24,8 @@ "resolve-env": "^1.0.0" }, "devDependencies": { - "babel-eslint": "^4.1.5", + "babel-cli": "^6.2.0", + "babel-eslint": "^4.1.6", "babel-preset-steelbrain": "^1.0.0", "babel-preset-steelbrain-legacy": "^1.0.0", "eslint-config-airbnb": "latest", diff --git a/spec/es5-helpers-spec.js b/spec/es5-helpers-spec.js index 0c99df18..01bb16ec 100644 --- a/spec/es5-helpers-spec.js +++ b/spec/es5-helpers-spec.js @@ -7,7 +7,7 @@ import { determineConfigFile, findEslintDir, getEslintCli -} from '../lib/es5-helpers' +} from '../lib/helpers' let fixtureDir diff --git a/src/helpers.js b/src/helpers.js index 0224a1ee..27664a33 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -84,17 +84,15 @@ export function getEslintDirectory(params, modulesPath = null) { const prefixPath = getNodePrefixPath() if (process.platform === 'win32') { return Path.join(params.nodePath || prefixPath, 'node_modules', 'eslint') - } else { - return Path.join(params.nodePath || prefixPath, 'lib', 'node_modules', 'eslint') - } - } else { - const eslintPath = Path.join(modulesPath || getModulesDirectory(params.fileDir), 'eslint') - try { - FS.accessSync(eslintPath, FS.R_OK) - return eslintPath - } catch (_) { - return getBundledEslintDirectory() } + return Path.join(params.nodePath || prefixPath, 'lib', 'node_modules', 'eslint') + } + const eslintPath = Path.join(modulesPath || getModulesDirectory(params.fileDir), 'eslint') + try { + FS.accessSync(eslintPath, FS.R_OK) + return eslintPath + } catch (_) { + return getBundledEslintDirectory() } } diff --git a/src/worker.js b/src/worker.js index 92364344..32cb7506 100644 --- a/src/worker.js +++ b/src/worker.js @@ -3,7 +3,6 @@ process.title = 'linter-eslint helper' import Path from 'path' -import {execFileSync} from 'child_process' import CP from 'childprocess-promise' import resolveEnv from 'resolve-end' import * as Helpers from './helpers' @@ -19,7 +18,8 @@ Communication.on('JOB', function(Job) { const {eslint, eslintDirectory} = Helpers.getEslint(params) if (params.canDisable && configFile === null) { - return Job.Response = [] + Job.Response = [] + return Job.Response } From a386c09ee80647f421e7a2c84b782983c34e048c Mon Sep 17 00:00:00 2001 From: Spain Date: Tue, 1 Dec 2015 18:51:32 -0500 Subject: [PATCH 15/15] :shower: Remove compiled assets from source control --- lib/helpers.js | 138 -------------------------------------- lib/main.js | 174 ------------------------------------------------ lib/reporter.js | 6 -- lib/worker.js | 78 ---------------------- 4 files changed, 396 deletions(-) delete mode 100644 lib/helpers.js delete mode 100644 lib/main.js delete mode 100644 lib/reporter.js delete mode 100644 lib/worker.js diff --git a/lib/helpers.js b/lib/helpers.js deleted file mode 100644 index 47f83d99..00000000 --- a/lib/helpers.js +++ /dev/null @@ -1,138 +0,0 @@ -'use babel'; - -import Path from 'path'; -import FS from 'fs'; -import ChildProcess from 'child_process'; -import CP from 'childprocess-promise'; -import { findFile as find } from 'atom-linter'; - -export function spawnWorker() { - let shouldLive = true; - const env = Object.create(process.env); - delete env.NODE_PATH; - delete env.NODE_ENV; - const data = { stdout: [], stderr: [] }; - const child = ChildProcess.fork(__dirname + '/worker.js', [], { env, silent: true }); - const worker = new CP(child); - function killer() { - shouldLive = false; - child.kill(); - } - child.stdout.on('data', function (chunk) { - data.stdout.push(chunk); - }); - child.stderr.on('data', function (chunk) { - data.stderr.push(chunk); - }); - child.on('exit', function () { - if (shouldLive) { - console.log('ESLint Worker Info', { stdout: data.stdout.join(''), stderr: data.stderr.join('') }); - atom.notifications.addWarning('[Linter-ESLint] Worker died unexpectedly', { detail: 'Check your console for more info. A new worker will be spawned instantly.', dismissable: true }); - } - child.emit('exit-linter', shouldLive); - }); - process.on('exit', killer); - return { child, worker, subscription: { dispose: function () { - killer(); - process.removeListener('exit', killer); - } } }; -} - -export function getModulesDirectory(fileDir) { - return find(fileDir, 'node_modules'); -} - -export function getIgnoresFile(fileDir) { - return Path.dirname(find(fileDir, '.eslintignore')); -} - -export function getEslintFromDirectory(path) { - try { - return require(Path.join(path, 'lib', 'cli.js')); - } catch (e) { - if (e.code === 'MODULE_NOT_FOUND') { - throw new Error('ESLint not found, Please install or make sure Atom is getting $PATH correctly'); - } else throw e; - } -} - -let nodePrefixPath = null; - -export function getNodePrefixPath() { - if (nodePrefixPath === null) { - const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm'; - try { - nodePrefixPath = ChildProcess.spawnSync(npmCommand, ['get', 'prefix']).output[1].toString().trim(); - } catch (e) { - throw new Error('Unable to execute `npm get prefix`. Please make sure Atom is getting $PATH correctly'); - } - } - return nodePrefixPath; -} - -let bundledEslintDirectory = null; - -export function getBundledEslintDirectory() { - if (bundledEslintDirectory === null) { - bundledEslintDirectory = Path.normalize(Path.join(__dirname, '..', 'node_modules', 'eslint')); - } - return bundledEslintDirectory; -} - -export function getEslintDirectory(params, modulesPath = null) { - if (params.global) { - const prefixPath = getNodePrefixPath(); - if (process.platform === 'win32') { - return Path.join(params.nodePath || prefixPath, 'node_modules', 'eslint'); - } - return Path.join(params.nodePath || prefixPath, 'lib', 'node_modules', 'eslint'); - } - const eslintPath = Path.join(modulesPath || getModulesDirectory(params.fileDir), 'eslint'); - try { - FS.accessSync(eslintPath, FS.R_OK); - return eslintPath; - } catch (_) { - return getBundledEslintDirectory(); - } -} - -export function getEslintConfig(params) { - const configFile = find(params.fileDir, ['.eslintrc.js', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json', '.eslintrc']) || null; - if (configFile) { - return configFile; - } - - const packagePath = find(params.fileDir, 'package.json'); - if (packagePath && Boolean(require(packagePath).eslintConfig)) { - return packagePath; - } - - if (params.canDisable) { - return null; - } - - if (params.configFile) { - return params.configFile; - } -} - -let eslint; -let lastEslintDirectory; -let lastModulesPath; - -export function getEslint(params) { - const modulesPath = getModulesDirectory(params.fileDir); - const eslintDirectory = getEslintDirectory(params, modulesPath); - if (eslintDirectory !== lastEslintDirectory) { - lastEslintDirectory = eslintDirectory; - eslint = getEslintFromDirectory(eslintDirectory); - } - if (lastModulesPath !== modulesPath) { - lastModulesPath = modulesPath; - if (modulesPath) { - process.env.NODE_PATH = modulesPath; - } else process.env.NODE_PATH = ''; - require('module').Module._initPaths(); - } - return { eslint, eslintDirectory }; -} \ No newline at end of file diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index ecb1ddea..00000000 --- a/lib/main.js +++ /dev/null @@ -1,174 +0,0 @@ -'use babel'; - -import Path from 'path'; -import { CompositeDisposable } from 'atom'; -import { spawnWorker } from './helpers'; -import escapeHTML from 'escape-html'; - -export default { - config: { - lintHtmlFiles: { - title: 'Lint HTML Files', - description: 'You should also add `eslint-plugin-html` to your .eslintrc plugins', - type: 'boolean', - default: false - }, - useGlobalEslint: { - title: 'Use global ESLint installation', - description: 'Make sure you have it in your $PATH', - type: 'boolean', - default: false - }, - showRuleIdInMessage: { - title: 'Show Rule ID in Messages', - type: 'boolean', - default: true - }, - disableWhenNoEslintConfig: { - title: 'Disable when no ESLint config is found (in package.json or .eslintrc)', - type: 'boolean', - default: true - }, - eslintrcPath: { - title: '.eslintrc Path', - description: "It will only be used when there's no config file in project", - type: 'string', - default: '' - }, - globalNodePath: { - title: 'Global Node Installation Path', - description: 'Write the value of `npm get prefix` here', - type: 'string', - default: '' - }, - eslintRulesDir: { - title: 'ESLint Rules Dir', - description: 'Specify a directory for ESLint to load rules from', - type: 'string', - default: '' - }, - disableEslintIgnore: { - title: 'Disable using .eslintignore files', - type: 'boolean', - default: false - } - }, - activate: function () { - require('atom-package-deps').install(); - - this.subscriptions = new CompositeDisposable(); - this.active = true; - this.worker = null; - this.scopes = ['source.js', 'source.jsx', 'source.js.jsx', 'source.babel', 'source.js-semantic']; - - const embeddedScope = 'source.js.embedded.html'; - this.subscriptions.add(atom.config.observe('linter-eslint.lintHtmlFiles', lintHtmlFiles => { - if (lintHtmlFiles) { - this.scopes.push(embeddedScope); - } else { - if (this.scopes.indexOf(embeddedScope) !== -1) { - this.scopes.splice(this.scopes.indexOf(embeddedScope), 1); - } - } - })); - this.subscriptions.add(atom.commands.add('atom-text-editor', { - 'linter-eslint:fix-file': () => { - const textEditor = atom.workspace.getActiveTextEditor(); - const filePath = textEditor.getPath(); - const fileDir = Path.dirname(filePath); - - if (!textEditor || textEditor.isModified()) { - // Abort for invalid or unsaved text editors - atom.notifications.addError('Linter-ESLint: Please save before fixing'); - return; - } - - this.worker.request('FIX', { - fileDir: fileDir, - filePath: filePath, - global: atom.config.get('linter-eslint.useGlobalEslint'), - nodePath: atom.config.get('linter-eslint.globalNodePath'), - configFile: atom.config.get('linter-eslint.eslintrcPath') - }).then(function (response) { - atom.notifications.addSuccess(response); - }).catch(function (response) { - atom.notifications.addWarning(response); - }); - } - })); - - const initializeWorker = () => { - if (this.active) { - const { child, worker, subscription } = spawnWorker(); - this.worker = worker; - this.subscriptions.add(subscription); - child.on('exit-linter', shouldLive => { - this.worker = null; - // Respawn if it crashed. See atom/electron#3446 - if (shouldLive) { - initializeWorker(); - } - }); - } - }; - initializeWorker(); - }, - deactivate: function () { - this.active = false; - this.subscriptions.dispose(); - }, - provideLinter: function () { - const Helpers = require('atom-linter'); - return { - name: 'ESLint', - grammarScopes: this.scopes, - scope: 'file', - lintOnFly: true, - lint: textEditor => { - const text = textEditor.getText(); - if (text.length === 0) { - return Promise.resolve([]); - } - const filePath = textEditor.getPath(); - const fileDir = Path.dirname(filePath); - const showRule = atom.config.get('linter-eslint.showRuleIdInMessage'); - - return this.worker.request('JOB', { - fileDir: fileDir, - filePath: filePath, - contents: text, - global: atom.config.get('linter-eslint.useGlobalEslint'), - canDisable: atom.config.get('linter-eslint.disableWhenNoEslintConfig'), - nodePath: atom.config.get('linter-eslint.globalNodePath'), - rulesDir: atom.config.get('linter-eslint.eslintRulesDir'), - configFile: atom.config.get('linter-eslint.eslintrcPath'), - disableIgnores: atom.config.get('linter-eslint.disableEslintIgnore') - }).then(function (response) { - if (response.length === 1 && response[0].message === 'File ignored because of your .eslintignore file. Use --no-ignore to override.') { - return []; - } - return response.map(function ({ message, line, severity, ruleId, column }) { - const range = Helpers.rangeFromLineNumber(textEditor, line - 1); - if (column) { - range[0][1] = column - 1; - } - if (column > range[1][1]) { - range[1][1] = column - 1; - } - const ret = { - filePath: filePath, - type: severity === 1 ? 'Warning' : 'Error', - range: range - }; - if (showRule) { - ret.html = `${ ruleId || 'Fatal' } ${ escapeHTML(message) }`; - } else { - ret.text = message; - } - return ret; - }); - }); - } - }; - } -}; \ No newline at end of file diff --git a/lib/reporter.js b/lib/reporter.js deleted file mode 100644 index 366917ea..00000000 --- a/lib/reporter.js +++ /dev/null @@ -1,6 +0,0 @@ -'use strict'; -// This file is used by eslint to hand the errors over to the worker - -module.exports = function (results) { - global.__LINTER_RESPONSE = results[0].messages; -}; \ No newline at end of file diff --git a/lib/worker.js b/lib/worker.js deleted file mode 100644 index ed57636c..00000000 --- a/lib/worker.js +++ /dev/null @@ -1,78 +0,0 @@ -'use strict'; -// Note: 'use babel' doesn't work in forked processes - -process.title = 'linter-eslint helper'; - -import Path from 'path'; -import CP from 'childprocess-promise'; -import resolveEnv from 'resolve-end'; -import * as Helpers from './helpers'; - -const Communication = new CP(); - -Communication.on('JOB', function (Job) { - global.__LINTER_RESPONSE = []; - - const params = Job.Message; - const ignoreFile = Helpers.getIgnoresFile(params.fileDir); - const configFile = Helpers.getEslintConfig(params.fileDir); - const { eslint, eslintDirectory } = Helpers.getEslint(params); - - if (params.canDisable && configFile === null) { - Job.Response = []; - return Job.Response; - } - - Job.Response = new Promise(function (resolve) { - let filePath; - if (ignoreFile) { - filePath = Path.relative(ignoreFile, params.filePath); - process.chdir(ignoreFile); - } else { - filePath = Path.basename(params.filePath); - process.chdir(params.fileDir); - } - - const argv = [process.execPath, eslintDirectory, '--stdin', '--format', Path.join(__dirname, 'reporter.js')]; - if (params.rulesDir) { - let rulesDir = resolveEnv(params.rulesDir); - if (!Path.isAbsolute(rulesDir)) { - rulesDir = find(params.fileDir, rulesDir); - } - argv.push('--rulesdir', rulesDir); - } - if (configFile !== null) { - argv.push('--config', resolveEnv(configFile)); - } - if (params.disableIgnores) { - argv.push('--no-ignore'); - } - argv.push('--stdin-filename', filePath); - process.argv = argv; - - eslint.execute(process.argv, params.contents); - resolve(global.__LINTER_RESPONSE); - }); -}); - -Communication.on('FIX', function (Job) { - const params = Job.Message; - const { eslint, eslintDirectory } = Helpers.getEslint(params); - const configFile = Helpers.getEslintConfig(params); - - const argv = [process.execPath, eslintDirectory, params.filePath, '--fix']; - if (configFile !== null) { - argv.push('--config', resolveEnv(configFile)); - } - process.argv = argv; - process.chdir(params.fileDir); - - try { - eslint.execute(process.argv); - } catch (_) { - throw new Error('Linter-ESLint: Fix Attempt Completed, Linting Errors Remain'); - } - return 'Linter-ESLint: Fix Complete'; -}); - -process.exit = function () {/* Stop eslint from closing the daemon */}; \ No newline at end of file