This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #362 from AtomLinter/arcanemagus/eslint-config-air…
…bnb-base-11.0.0 Update to eslint-config-airbnb-base v11.0.0
- Loading branch information
Showing
3 changed files
with
150 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,166 +1,173 @@ | ||
'use babel' | ||
'use babel'; | ||
|
||
/* @flow */ | ||
|
||
import Path from 'path' | ||
// eslint-disable-next-line import/extensions | ||
import { CompositeDisposable } from 'atom' | ||
|
||
type Linter$Provider = Object | ||
import Path from 'path'; | ||
// eslint-disable-next-line import/extensions, import/no-extraneous-dependencies | ||
import { CompositeDisposable } from 'atom'; | ||
|
||
module.exports = { | ||
config: { | ||
executablePath: { | ||
type: 'string', | ||
default: Path.join(__dirname, '..', 'node_modules', 'jshint', 'bin', 'jshint'), | ||
description: 'Path of the `jshint` node script' | ||
description: 'Path of the `jshint` node script', | ||
}, | ||
lintInlineJavaScript: { | ||
type: 'boolean', | ||
default: false, | ||
description: 'Lint JavaScript inside `<script>` blocks in HTML or PHP files.' | ||
description: 'Lint JavaScript inside `<script>` blocks in HTML or PHP files.', | ||
}, | ||
disableWhenNoJshintrcFileInPath: { | ||
type: 'boolean', | ||
default: false, | ||
description: 'Disable linter when no `.jshintrc` is found in project.' | ||
description: 'Disable linter when no `.jshintrc` is found in project.', | ||
}, | ||
jshintFileName: { | ||
type: 'string', | ||
default: '.jshintrc', | ||
description: 'jshint file name' | ||
} | ||
description: 'jshint file name', | ||
}, | ||
}, | ||
|
||
activate() { | ||
require('atom-package-deps').install('linter-jshint') | ||
require('atom-package-deps').install('linter-jshint'); | ||
|
||
this.scopes = ['source.js', 'source.js-semantic'] | ||
this.subscriptions = new CompositeDisposable() | ||
this.scopes = ['source.js', 'source.js-semantic']; | ||
this.subscriptions = new CompositeDisposable(); | ||
this.subscriptions.add(atom.config.observe('linter-jshint.executablePath', (executablePath) => { | ||
this.executablePath = executablePath | ||
})) | ||
this.executablePath = executablePath; | ||
})); | ||
this.subscriptions.add( | ||
atom.config.observe('linter-jshint.disableWhenNoJshintrcFileInPath', | ||
(disableWhenNoJshintrcFileInPath) => { | ||
this.disableWhenNoJshintrcFileInPath = disableWhenNoJshintrcFileInPath | ||
} | ||
) | ||
) | ||
this.disableWhenNoJshintrcFileInPath = disableWhenNoJshintrcFileInPath; | ||
}, | ||
), | ||
); | ||
|
||
this.subscriptions.add(atom.config.observe('linter-jshint.jshintFileName', (jshintFileName) => { | ||
this.jshintFileName = jshintFileName | ||
})) | ||
this.jshintFileName = jshintFileName; | ||
})); | ||
|
||
const scopeEmbedded = 'source.js.embedded.html' | ||
const scopeEmbedded = 'source.js.embedded.html'; | ||
this.subscriptions.add(atom.config.observe('linter-jshint.lintInlineJavaScript', | ||
(lintInlineJavaScript) => { | ||
this.lintInlineJavaScript = lintInlineJavaScript | ||
this.lintInlineJavaScript = lintInlineJavaScript; | ||
if (lintInlineJavaScript) { | ||
this.scopes.push(scopeEmbedded) | ||
this.scopes.push(scopeEmbedded); | ||
} else if (this.scopes.indexOf(scopeEmbedded) !== -1) { | ||
this.scopes.splice(this.scopes.indexOf(scopeEmbedded), 1) | ||
this.scopes.splice(this.scopes.indexOf(scopeEmbedded), 1); | ||
} | ||
} | ||
)) | ||
}, | ||
)); | ||
}, | ||
|
||
deactivate() { | ||
this.subscriptions.dispose() | ||
this.subscriptions.dispose(); | ||
}, | ||
|
||
provideLinter(): Linter$Provider { | ||
const Helpers = require('atom-linter') | ||
const Reporter = require('jshint-json') | ||
provideLinter() { | ||
const Helpers = require('atom-linter'); | ||
const Reporter = require('jshint-json'); | ||
|
||
return { | ||
name: 'JSHint', | ||
grammarScopes: this.scopes, | ||
scope: 'file', | ||
lintOnFly: true, | ||
lint: async (textEditor) => { | ||
const results = [] | ||
const filePath = textEditor.getPath() | ||
const fileContents = textEditor.getText() | ||
const parameters = ['--reporter', Reporter, '--filename', filePath] | ||
const results = []; | ||
const filePath = textEditor.getPath(); | ||
const fileContents = textEditor.getText(); | ||
const parameters = ['--reporter', Reporter, '--filename', filePath]; | ||
|
||
const configFile = await Helpers.findCachedAsync( | ||
Path.dirname(filePath), this.jshintFileName | ||
) | ||
Path.dirname(filePath), this.jshintFileName, | ||
); | ||
|
||
if (configFile) { | ||
parameters.push('--config', configFile) | ||
parameters.push('--config', configFile); | ||
} else if (this.disableWhenNoJshintrcFileInPath) { | ||
return results | ||
return results; | ||
} | ||
|
||
if (this.lintInlineJavaScript && | ||
textEditor.getGrammar().scopeName.indexOf('text.html') !== -1 | ||
) { | ||
parameters.push('--extract', 'always') | ||
parameters.push('--extract', 'always'); | ||
} | ||
parameters.push('-') | ||
parameters.push('-'); | ||
|
||
const execOpts = { stdin: fileContents, ignoreExitCode: true } | ||
const execOpts = { stdin: fileContents, ignoreExitCode: true }; | ||
const result = await Helpers.execNode( | ||
this.executablePath, parameters, execOpts | ||
) | ||
this.executablePath, parameters, execOpts, | ||
); | ||
|
||
if (textEditor.getText() !== fileContents) { | ||
// File has changed since the lint was triggered, tell Linter not to update | ||
return null | ||
return null; | ||
} | ||
|
||
let parsed | ||
let parsed; | ||
try { | ||
parsed = JSON.parse(result) | ||
parsed = JSON.parse(result); | ||
} catch (_) { | ||
console.error('[Linter-JSHint]', _, result) | ||
// eslint-disable-next-line no-console | ||
console.error('[Linter-JSHint]', _, result); | ||
atom.notifications.addWarning('[Linter-JSHint]', | ||
{ detail: 'JSHint return an invalid response, check your console for more info' } | ||
) | ||
return results | ||
{ detail: 'JSHint return an invalid response, check your console for more info' }, | ||
); | ||
return results; | ||
} | ||
|
||
for (const entry of parsed.result) { | ||
Object.keys(parsed.result).forEach((entryID) => { | ||
const entry = parsed.result[entryID]; | ||
|
||
if (!entry.error.id) { | ||
continue | ||
return; | ||
} | ||
|
||
const error = entry.error | ||
const errorType = error.code.substr(0, 1) | ||
const errorLine = error.line > 0 ? error.line - 1 : 0 | ||
let range | ||
const error = entry.error; | ||
const errorType = error.code.substr(0, 1); | ||
let type = 'Info'; | ||
if (errorType === 'E') { | ||
type = 'Error'; | ||
} else if (errorType === 'W') { | ||
type = 'Warning'; | ||
} | ||
const errorLine = error.line > 0 ? error.line - 1 : 0; | ||
let range; | ||
|
||
// TODO: Remove workaround of jshint/jshint#2846 | ||
if (error.character === null) { | ||
range = Helpers.rangeFromLineNumber(textEditor, errorLine) | ||
range = Helpers.rangeFromLineNumber(textEditor, errorLine); | ||
} else { | ||
let character = error.character > 0 ? error.character - 1 : 0 | ||
let line = errorLine | ||
const buffer = textEditor.getBuffer() | ||
const maxLine = buffer.getLineCount() | ||
let character = error.character > 0 ? error.character - 1 : 0; | ||
let line = errorLine; | ||
const buffer = textEditor.getBuffer(); | ||
const maxLine = buffer.getLineCount(); | ||
// TODO: Remove workaround of jshint/jshint#2894 | ||
if (errorLine >= maxLine) { | ||
line = maxLine | ||
line = maxLine; | ||
} | ||
const maxCharacter = buffer.lineLengthForRow(line) | ||
const maxCharacter = buffer.lineLengthForRow(line); | ||
// TODO: Remove workaround of jquery/esprima#1457 | ||
if (character > maxCharacter) { | ||
character = maxCharacter | ||
character = maxCharacter; | ||
} | ||
range = Helpers.rangeFromLineNumber(textEditor, line, character) | ||
range = Helpers.rangeFromLineNumber(textEditor, line, character); | ||
} | ||
|
||
results.push({ | ||
type: errorType === 'E' ? 'Error' : errorType === 'W' ? 'Warning' : 'Info', | ||
type, | ||
text: `${error.code} - ${error.reason}`, | ||
filePath, | ||
range | ||
}) | ||
} | ||
return results | ||
} | ||
} | ||
} | ||
} | ||
range, | ||
}); | ||
}); | ||
return results; | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.