Skip to content

Commit

Permalink
fix: fixes AtomLinter#339
Browse files Browse the repository at this point in the history
forward non timeout execution errors to linter
  • Loading branch information
vzamanillo committed Jun 21, 2019
1 parent ac7f134 commit 97509d0
Showing 1 changed file with 58 additions and 39 deletions.
97 changes: 58 additions & 39 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ let path
let pluralize
let semver

const severityMapping = {
refactor: 'info',
convention: 'info',
warning: 'warning',
error: 'error',
fatal: 'error',
}

const loadDeps = () => {
if (!helpers) {
helpers = require('atom-linter')
Expand Down Expand Up @@ -73,6 +81,15 @@ const executeRubocop = async (execOptions, command) => {
return output
}

const forwardErrorToLinter = (message, editor) => [{
excerpt: `Linter-Rubocop: ${message}`,
severity: severityMapping.error,
location: {
file: editor.getPath(),
position: helpers.generateRange(editor, 0),
},
}]

const forwardRubocopToLinter = (version, {
message: rawMessage, location, severity, cop_name: copName,
}, file, editor) => {
Expand All @@ -86,14 +103,6 @@ const forwardRubocopToLinter = (version, {
position = helpers.generateRange(editor, 0)
}

const severityMapping = {
refactor: 'info',
convention: 'info',
warning: 'warning',
error: 'error',
fatal: 'error',
}

const linterMessage = {
url,
excerpt: hasCopName ? excerpt : `${copName}: ${excerpt}`,
Expand Down Expand Up @@ -204,25 +213,31 @@ export default {
command.push('--auto-correct')
command.push(filePath)

const output = await executeRubocop(getBaseExecutionOpts(filePath), command)
const {
files,
summary: { offense_count: offenseCount },
} = parseFromStd(output.stdout, output.stderr)

const offenses = files && files[0] && files[0].offenses

if (offenseCount === 0) {
atom.notifications.addInfo('Linter-Rubocop: No fixes were made')
} else {
const corrections = Object.values(offenses)
.reduce((off, { corrected }) => off + corrected, 0)
const message = `Linter-Rubocop: Fixed ${pluralize('offenses', corrections, true)} of ${offenseCount}`
if (corrections < offenseCount) {
atom.notifications.addInfo(message)
let output
try {
output = await executeRubocop(getBaseExecutionOpts(filePath), command)

const {
files,
summary: { offense_count: offenseCount },
} = parseFromStd(output.stdout, output.stderr)

const offenses = files && files[0] && files[0].offenses

if (offenseCount === 0) {
atom.notifications.addInfo('Linter-Rubocop: No fixes were made')
} else {
atom.notifications.addSuccess(message)
const corrections = Object.values(offenses)
.reduce((off, { corrected }) => off + corrected, 0)
const message = `Linter-Rubocop: Fixed ${pluralize('offenses', corrections, true)} of ${offenseCount}`
if (corrections < offenseCount) {
atom.notifications.addInfo(message)
} else {
atom.notifications.addSuccess(message)
}
}
} catch (e) {
atom.notifications.addError('Linter-Rubocop: Unexpected error', { description: e.message })
}
},

Expand Down Expand Up @@ -256,24 +271,28 @@ export default {
command.push(filePath)
}

const output = await executeRubocop(execOptions, command)

// Process was canceled by newer process
if (output === null) { return null }
let output
try {
output = await executeRubocop(execOptions, command)
// Process was canceled by newer process
if (output === null) { return null }

const {
metadata: { rubocop_version: rubocopVersion }, files,
} = parseFromStd(output.stdout, output.stderr)
const {
metadata: { rubocop_version: rubocopVersion }, files,
} = parseFromStd(output.stdout, output.stderr)

if (rubocopVersion == null || rubocopVersion === '') {
throw new Error('Unable to get rubocop version from linting output results.')
}
if (rubocopVersion == null || rubocopVersion === '') {
throw new Error('Unable to get rubocop version from linting output results.')
}

const offenses = files && files[0] && files[0].offenses
const offenses = files && files[0] && files[0].offenses

return (offenses || []).map(
offense => forwardRubocopToLinter(rubocopVersion, offense, filePath, editor),
)
return (offenses || []).map(
offense => forwardRubocopToLinter(rubocopVersion, offense, filePath, editor),
)
} catch (e) {
return forwardErrorToLinter(e.message, editor)
}
},
}
},
Expand Down

0 comments on commit 97509d0

Please sign in to comment.