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

Commit 4b092fa

Browse files
author
steelbrain
committed
🆕 Add src folder with source js files
1 parent 284b585 commit 4b092fa

File tree

7 files changed

+553
-92
lines changed

7 files changed

+553
-92
lines changed

lib/helpers.js

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,49 @@
1-
'use babel'
1+
'use strict';
2+
'use babel';
23

3-
import ChildProcess from 'child_process'
4-
import CP from 'childprocess-promise'
4+
Object.defineProperty(exports, "__esModule", {
5+
value: true
6+
});
7+
exports.spawnWorker = spawnWorker;
58

6-
export function spawnWorker() {
7-
let shouldLive = true
8-
const env = Object.create(process.env)
9-
delete env.NODE_PATH
10-
delete env.NODE_ENV
11-
const data = {stdout: [], stderr: []}
12-
const child = ChildProcess.fork(__dirname + '/worker.js', [], {env, silent: true})
13-
const worker = new CP(child)
9+
var _child_process = require('child_process');
10+
11+
var _child_process2 = _interopRequireDefault(_child_process);
12+
13+
var _childprocessPromise = require('childprocess-promise');
14+
15+
var _childprocessPromise2 = _interopRequireDefault(_childprocessPromise);
16+
17+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18+
19+
function spawnWorker() {
20+
let shouldLive = true;
21+
const env = Object.create(process.env);
22+
delete env.NODE_PATH;
23+
delete env.NODE_ENV;
24+
const data = { stdout: [], stderr: [] };
25+
const child = _child_process2.default.fork(__dirname + '/worker.js', [], { env: env, silent: true });
26+
const worker = new _childprocessPromise2.default(child);
1427
function killer() {
15-
shouldLive = false
16-
child.kill()
28+
shouldLive = false;
29+
child.kill();
1730
}
18-
child.stdout.on('data', function(chunk) {
19-
data.stdout.push(chunk)
20-
})
21-
child.stderr.on('data', function(chunk) {
22-
data.stderr.push(chunk)
23-
})
24-
child.on('exit', function() {
31+
child.stdout.on('data', function (chunk) {
32+
data.stdout.push(chunk);
33+
});
34+
child.stderr.on('data', function (chunk) {
35+
data.stderr.push(chunk);
36+
});
37+
child.on('exit', function () {
2538
if (shouldLive) {
26-
console.log('ESLint Worker Info', {stdout: data.stdout.join(''), stderr: data.stderr.join('')})
27-
atom.notifications.addWarning('[Linter-ESLint] Worker died unexpectedly', {detail: 'Check your console for more info. A new worker will be spawned instantly.', dismissable: true})
39+
console.log('ESLint Worker Info', { stdout: data.stdout.join(''), stderr: data.stderr.join('') });
40+
atom.notifications.addWarning('[Linter-ESLint] Worker died unexpectedly', { detail: 'Check your console for more info. A new worker will be spawned instantly.', dismissable: true });
2841
}
29-
child.emit('exit-linter', shouldLive)
30-
})
31-
process.on('exit', killer)
32-
return {child, worker, subscription: {dispose: function() {
33-
killer()
34-
process.removeListener('exit', killer)
35-
}}}
36-
}
42+
child.emit('exit-linter', shouldLive);
43+
});
44+
process.on('exit', killer);
45+
return { child: child, worker: worker, subscription: { dispose: function () {
46+
killer();
47+
process.removeListener('exit', killer);
48+
} } };
49+
}

lib/main.js

Lines changed: 87 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1-
'use babel'
1+
'use strict';
2+
'use babel';
23

3-
import Path from 'path'
4-
import {CompositeDisposable} from 'atom'
5-
import {spawnWorker} from './helpers'
6-
import escapeHTML from 'escape-html'
4+
Object.defineProperty(exports, "__esModule", {
5+
value: true
6+
});
77

8-
export default {
8+
var _path = require('path');
9+
10+
var _path2 = _interopRequireDefault(_path);
11+
12+
var _atom = require('atom');
13+
14+
var _helpers = require('./helpers');
15+
16+
var _escapeHtml = require('escape-html');
17+
18+
var _escapeHtml2 = _interopRequireDefault(_escapeHtml);
19+
20+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21+
22+
exports.default = {
923
config: {
1024
lintHtmlFiles: {
1125
title: 'Lint HTML Files',
@@ -53,40 +67,40 @@ export default {
5367
default: false
5468
}
5569
},
56-
activate: function() {
57-
require('atom-package-deps').install()
70+
activate: function () {
71+
require('atom-package-deps').install();
5872

59-
this.subscriptions = new CompositeDisposable()
60-
this.active = true
61-
this.worker = null
62-
this.scopes = ['source.js', 'source.jsx', 'source.js.jsx', 'source.babel', 'source.js-semantic']
73+
this.subscriptions = new _atom.CompositeDisposable();
74+
this.active = true;
75+
this.worker = null;
76+
this.scopes = ['source.js', 'source.jsx', 'source.js.jsx', 'source.babel', 'source.js-semantic'];
6377

64-
const embeddedScope = 'source.js.embedded.html'
78+
const embeddedScope = 'source.js.embedded.html';
6579
this.subscriptions.add(atom.config.observe('linter-eslint.lintHtmlFiles', lintHtmlFiles => {
6680
if (lintHtmlFiles) {
67-
this.scopes.push(embeddedScope)
81+
this.scopes.push(embeddedScope);
6882
} else {
6983
if (this.scopes.indexOf(embeddedScope) !== -1) {
70-
this.scopes.splice(this.scopes.indexOf(embeddedScope), 1)
84+
this.scopes.splice(this.scopes.indexOf(embeddedScope), 1);
7185
}
7286
}
73-
}))
87+
}));
7488
this.subscriptions.add(atom.commands.add('atom-text-editor', {
7589
'linter-eslint:fix-file': () => {
76-
const textEditor = atom.workspace.getActiveTextEditor()
77-
const filePath = textEditor.getPath()
78-
const fileDir = Path.dirname(filePath)
90+
const textEditor = atom.workspace.getActiveTextEditor();
91+
const filePath = textEditor.getPath();
92+
const fileDir = _path2.default.dirname(filePath);
7993

8094
if (!textEditor || textEditor.isModified()) {
8195
// Abort for invalid or unsaved text editors
82-
atom.notifications.addError('Linter-ESLint: Please save before fixing')
83-
return
96+
atom.notifications.addError('Linter-ESLint: Please save before fixing');
97+
return;
8498
}
8599

86100
if (this.worker === null) {
87101
// Abort if worker is not yet ready
88-
atom.notifications.addError('Linter-ESLint: Not ready, please try again')
89-
return
102+
atom.notifications.addError('Linter-ESLint: Not ready, please try again');
103+
return;
90104
}
91105

92106
this.worker.request('FIX', {
@@ -95,61 +109,66 @@ export default {
95109
global: atom.config.get('linter-eslint.useGlobalEslint'),
96110
nodePath: atom.config.get('linter-eslint.globalNodePath'),
97111
configFile: atom.config.get('linter-eslint.eslintrcPath')
98-
}).then(function(response) {
99-
atom.notifications.addSuccess(response)
100-
}).catch(function(response) {
101-
atom.notifications.addWarning(response)
102-
})
112+
}).then(function (response) {
113+
atom.notifications.addSuccess(response);
114+
}).catch(function (response) {
115+
atom.notifications.addWarning(response);
116+
});
103117
}
104-
}))
118+
}));
105119

106120
// Reason: I (steelbrain) have observed that if we spawn a
107121
// process while atom is starting up, it can increase startup
108122
// time by several seconds, But if we do this after 5 seconds,
109123
// we barely feel a thing.
110124
const initializeWorker = () => {
111125
if (this.active) {
112-
const {child, worker, subscription} = spawnWorker()
113-
this.worker = worker
114-
this.subscriptions.add(subscription)
126+
var _spawnWorker = (0, _helpers.spawnWorker)();
127+
128+
const child = _spawnWorker.child;
129+
const worker = _spawnWorker.worker;
130+
const subscription = _spawnWorker.subscription;
131+
132+
this.worker = worker;
133+
this.subscriptions.add(subscription);
115134
child.on('exit-linter', shouldLive => {
116-
this.worker = null
135+
this.worker = null;
117136
// Respawn if it crashed. See atom/electron#3446
118137
if (shouldLive) {
119-
initializeWorker()
138+
initializeWorker();
120139
}
121-
})
140+
});
122141
}
123-
}
124-
setTimeout(initializeWorker, 5 * 1000)
142+
};
143+
setTimeout(initializeWorker, 5 * 1000);
125144
},
126-
deactivate: function() {
127-
this.active = false
128-
this.subscriptions.dispose()
145+
deactivate: function () {
146+
this.active = false;
147+
this.subscriptions.dispose();
129148
},
130-
provideLinter: function() {
131-
const Helpers = require('atom-linter')
149+
provideLinter: function () {
150+
const Helpers = require('atom-linter');
132151
return {
133152
name: 'ESLint',
134153
grammarScopes: this.scopes,
135154
scope: 'file',
136155
lintOnFly: true,
137156
lint: textEditor => {
138-
const text = textEditor.getText()
157+
const text = textEditor.getText();
139158
if (text.length === 0) {
140-
return Promise.resolve([])
159+
return Promise.resolve([]);
141160
}
142-
const filePath = textEditor.getPath()
143-
const fileDir = Path.dirname(filePath)
144-
const showRule = atom.config.get('linter-eslint.showRuleIdInMessage')
161+
const filePath = textEditor.getPath();
162+
const fileDir = _path2.default.dirname(filePath);
163+
const showRule = atom.config.get('linter-eslint.showRuleIdInMessage');
145164

146165
if (this.worker === null) {
147166
return Promise.resolve([{
148167
filePath: filePath,
149168
type: 'Info',
150169
text: 'Worker initialization is delayed. Please try saving or typing to begin linting.',
151170
range: Helpers.rangeFromLineNumber(textEditor, 0)
152-
}])
171+
}]);
153172
}
154173

155174
return this.worker.request('JOB', {
@@ -162,32 +181,38 @@ export default {
162181
rulesDir: atom.config.get('linter-eslint.eslintRulesDir'),
163182
configFile: atom.config.get('linter-eslint.eslintrcPath'),
164183
disableIgnores: atom.config.get('linter-eslint.disableEslintIgnore')
165-
}).then(function(response) {
184+
}).then(function (response) {
166185
if (response.length === 1 && response[0].message === 'File ignored because of your .eslintignore file. Use --no-ignore to override.') {
167-
return []
186+
return [];
168187
}
169-
return response.map(function({message, line, severity, ruleId, column}) {
170-
const range = Helpers.rangeFromLineNumber(textEditor, line - 1)
188+
return response.map(function (_ref) {
189+
let message = _ref.message;
190+
let line = _ref.line;
191+
let severity = _ref.severity;
192+
let ruleId = _ref.ruleId;
193+
let column = _ref.column;
194+
195+
const range = Helpers.rangeFromLineNumber(textEditor, line - 1);
171196
if (column) {
172-
range[0][1] = column - 1
197+
range[0][1] = column - 1;
173198
}
174199
if (column > range[1][1]) {
175-
range[1][1] = column - 1
200+
range[1][1] = column - 1;
176201
}
177202
const ret = {
178203
filePath: filePath,
179204
type: severity === 1 ? 'Warning' : 'Error',
180205
range: range
181-
}
206+
};
182207
if (showRule) {
183-
ret.html = `<span class="badge badge-flexible">${ruleId || 'Fatal'}</span> ${escapeHTML(message)}`
208+
ret.html = `<span class="badge badge-flexible">${ ruleId || 'Fatal' }</span> ${ (0, _escapeHtml2.default)(message) }`;
184209
} else {
185-
ret.text = message
210+
ret.text = message;
186211
}
187-
return ret
188-
})
189-
})
212+
return ret;
213+
});
214+
});
190215
}
191-
}
216+
};
192217
}
193-
}
218+
};

src/es5-helpers.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'use strict'
2+
3+
const ChildProcess = require('child_process')
4+
const Path = require('path')
5+
const FS = require('fs')
6+
const find = require('atom-linter').findFile
7+
8+
let prefixPath = null
9+
const atomEslintPath = Path.join(FS.realpathSync(Path.join(__dirname, '..')), 'node_modules', 'eslint')
10+
11+
function findEslintDir(params) {
12+
const modulesPath = find(params.fileDir, 'node_modules')
13+
let eslintNewPath = null
14+
15+
if (params.global) {
16+
if (params.nodePath === '' && prefixPath === null) {
17+
const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm'
18+
try {
19+
prefixPath = ChildProcess.spawnSync(npmCommand, ['get', 'prefix']).output[1].toString().trim()
20+
} catch (e) {
21+
throw new Error('Unable to execute `npm get prefix`. Please make sure Atom is getting $PATH correctly')
22+
}
23+
}
24+
if (process.platform === 'win32') {
25+
eslintNewPath = Path.join(params.nodePath || prefixPath, 'node_modules', 'eslint')
26+
} else {
27+
eslintNewPath = Path.join(params.nodePath || prefixPath, 'lib', 'node_modules', 'eslint')
28+
}
29+
} else {
30+
try {
31+
FS.accessSync(eslintNewPath = Path.join(modulesPath, 'eslint'), FS.R_OK)
32+
} catch (_) {
33+
eslintNewPath = atomEslintPath
34+
}
35+
}
36+
37+
return eslintNewPath
38+
}
39+
40+
// Check for project config file or eslint config in package.json and determine
41+
// whether to bail out or use config specified in package options
42+
function determineConfigFile(params) {
43+
// config file
44+
const configFile = find(params.fileDir, ['.eslintrc.js', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json', '.eslintrc']) || null
45+
if (configFile) {
46+
return configFile
47+
}
48+
// package.json
49+
const packagePath = find(params.fileDir, 'package.json')
50+
if (packagePath && Boolean(require(packagePath).eslintConfig)) {
51+
return packagePath
52+
}
53+
// Couldn't find a config
54+
if (params.canDisable) {
55+
return null
56+
}
57+
// If all else fails, use the configFile specified in the linter-eslint options
58+
if (params.configFile) {
59+
return params.configFile
60+
}
61+
}
62+
63+
function getEslintCli(path) {
64+
try {
65+
const eslint = require(Path.join(path, 'lib', 'cli.js'))
66+
return eslint
67+
} catch (e) {
68+
if (e.code === 'MODULE_NOT_FOUND') {
69+
throw new Error('ESLint not found, Please install or make sure Atom is getting $PATH correctly')
70+
} else throw e
71+
}
72+
}
73+
74+
75+
module.exports = {
76+
findEslintDir: findEslintDir,
77+
find: find,
78+
determineConfigFile: determineConfigFile,
79+
getEslintCli: getEslintCli
80+
}

0 commit comments

Comments
 (0)