|
| 1 | +'use babel'; |
| 2 | + |
| 3 | +/* eslint-disable import/no-extraneous-dependencies, import/extensions */ |
| 4 | +import { CompositeDisposable } from 'atom'; |
| 5 | +/* eslint-enable import/no-extraneous-dependencies, import/extensions */ |
| 6 | + |
| 7 | +// Some internal variables |
| 8 | +const baseUrl = 'https://github.com/koalaman/shellcheck/wiki'; |
| 9 | +const errorCodeRegex = /SC\d{4}/; |
| 10 | +const regex = /.+?:(\d+):(\d+):\s(\w+?):\s(.+)/g; |
| 11 | + |
| 12 | +const linkifyErrorCode = text => |
| 13 | + text.replace(errorCodeRegex, `<a href="${baseUrl}/$&">$&</a>`); |
| 14 | + |
| 15 | +export default { |
| 16 | + activate() { |
| 17 | + require('atom-package-deps').install('linter-shellcheck'); |
| 18 | + |
| 19 | + this.subscriptions = new CompositeDisposable(); |
| 20 | + this.subscriptions.add( |
| 21 | + atom.config.observe('linter-shellcheck.shellcheckExecutablePath', (value) => { |
| 22 | + this.executablePath = value; |
| 23 | + }) |
| 24 | + ); |
| 25 | + this.subscriptions.add( |
| 26 | + atom.config.observe('linter-shellcheck.enableNotice', (value) => { |
| 27 | + this.enableNotice = value; |
| 28 | + }) |
| 29 | + ); |
| 30 | + this.subscriptions.add( |
| 31 | + atom.config.observe('linter-shellcheck.userParameters', (value) => { |
| 32 | + this.userParameters = value.trim().split(' ').filter(Boolean); |
| 33 | + }) |
| 34 | + ); |
| 35 | + }, |
| 36 | + |
| 37 | + deactivate() { |
| 38 | + this.subscriptions.dispose(); |
| 39 | + }, |
| 40 | + |
| 41 | + provideLinter() { |
| 42 | + const helpers = require('atom-linter'); |
| 43 | + const path = require('path'); |
| 44 | + |
| 45 | + return { |
| 46 | + name: 'ShellCheck', |
| 47 | + grammarScopes: ['source.shell'], |
| 48 | + scope: 'file', |
| 49 | + lintOnFly: true, |
| 50 | + lint: (textEditor) => { |
| 51 | + const filePath = textEditor.getPath(); |
| 52 | + const text = textEditor.getText(); |
| 53 | + const cwd = path.dirname(filePath); |
| 54 | + const showAll = this.enableNotice; |
| 55 | + // The first -f parameter overrides any others |
| 56 | + const parameters = [].concat(['-f', 'gcc'], this.userParameters, ['-']); |
| 57 | + const options = { stdin: text, cwd, ignoreExitCode: true }; |
| 58 | + return helpers.exec(this.executablePath, parameters, options).then((output) => { |
| 59 | + if (textEditor.getText() !== text) { |
| 60 | + // The text has changed since the lint was triggered, tell Linter not to update |
| 61 | + return null; |
| 62 | + } |
| 63 | + const messages = []; |
| 64 | + let match = regex.exec(output); |
| 65 | + while (match !== null) { |
| 66 | + const type = match[3]; |
| 67 | + if (showAll || type === 'warning' || type === 'error') { |
| 68 | + const line = Number.parseInt(match[1], 10) - 1; |
| 69 | + const col = Number.parseInt(match[2], 10) - 1; |
| 70 | + messages.push({ |
| 71 | + type, |
| 72 | + filePath, |
| 73 | + range: helpers.rangeFromLineNumber(textEditor, line, col), |
| 74 | + html: linkifyErrorCode(match[4]), |
| 75 | + }); |
| 76 | + } |
| 77 | + match = regex.exec(output); |
| 78 | + } |
| 79 | + return messages; |
| 80 | + }); |
| 81 | + }, |
| 82 | + }; |
| 83 | + }, |
| 84 | +}; |
0 commit comments