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.
Add debug command and show nice position errors
This does two things: * Add a debug command showing several bits of information about the current setup * Handle invalid points from JSHint in a much better manner, translating them into a message on the current file with a link to report the issue.
- Loading branch information
1 parent
aaee44b
commit d860c3d
Showing
5 changed files
with
209 additions
and
29 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
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 |
---|---|---|
@@ -0,0 +1,123 @@ | ||
'use babel'; | ||
|
||
import path from 'path'; | ||
import * as atomlinter from 'atom-linter'; | ||
import escapeHTML from 'escape-html'; | ||
import { readFile as fsReadFile } from 'fs'; | ||
// eslint-disable-next-line import/extensions, import/no-extraneous-dependencies | ||
import type { TextEditor } from 'atom'; | ||
|
||
async function readFile(filePath) { | ||
return new Promise((resolve, reject) => { | ||
fsReadFile(filePath, 'utf8', (err, data) => { | ||
if (err) { | ||
reject(err); | ||
} | ||
resolve(data); | ||
}); | ||
}); | ||
} | ||
|
||
export async function readIgnoreList(ignorePath) { | ||
return (await readFile(ignorePath)).split(/[\r\n]/); | ||
} | ||
|
||
export async function getDebugInfo() { | ||
const textEditor = atom.workspace.getActiveTextEditor(); | ||
let editorScopes; | ||
if (atom.workspace.isTextEditor(textEditor)) { | ||
editorScopes = textEditor.getLastCursor().getScopeDescriptor().getScopesArray(); | ||
} else { | ||
// Somehow this can be called with no active TextEditor, impossible I know... | ||
editorScopes = ['unknown']; | ||
} | ||
|
||
const packagePath = atom.packages.resolvePackagePath('linter-jshint'); | ||
let linterJSHintMeta; | ||
if (packagePath === undefined) { | ||
// Apparently for some users the package path fails to resolve | ||
linterJSHintMeta = { version: 'unknown!' }; | ||
} else { | ||
// eslint-disable-next-line import/no-dynamic-require | ||
const metaPath = path.join(packagePath, 'package.json'); | ||
linterJSHintMeta = JSON.parse(await readFile(metaPath)); | ||
} | ||
|
||
const config = atom.config.get('linter-jshint'); | ||
const hoursSinceRestart = Math.round((process.uptime() / 3600) * 10) / 10; | ||
// NOTE: Yes, `jshint --version` gets output on STDERR... | ||
const jshintVersion = await atomlinter.execNode( | ||
config.executablePath, ['--version'], { stream: 'stderr' }); | ||
|
||
const returnVal = { | ||
atomVersion: atom.getVersion(), | ||
linterJSHintVersion: linterJSHintMeta.version, | ||
linterJSHintConfig: config, | ||
// eslint-disable-next-line import/no-dynamic-require | ||
jshintVersion, | ||
hoursSinceRestart, | ||
platform: process.platform, | ||
editorScopes, | ||
}; | ||
return returnVal; | ||
} | ||
|
||
export async function generateDebugString() { | ||
const debug = await getDebugInfo(); | ||
const details = [ | ||
`Atom version: ${debug.atomVersion}`, | ||
`linter-jshint version: ${debug.linterJSHintVersion}`, | ||
`JSHint version: ${debug.jshintVersion}`, | ||
`Hours since last Atom restart: ${debug.hoursSinceRestart}`, | ||
`Platform: ${debug.platform}`, | ||
`Current file's scopes: ${JSON.stringify(debug.editorScopes, null, 2)}`, | ||
`linter-jshint configuration: ${JSON.stringify(debug.linterJSHintConfig, null, 2)}`, | ||
]; | ||
return details.join('\n'); | ||
} | ||
|
||
export async function generateInvalidTrace( | ||
msgLine: number, msgCol: number, filePath: string, textEditor: TextEditor, | ||
error: Object, | ||
) { | ||
const errMsgRange = `${msgLine + 1}:${msgCol}`; | ||
const rangeText = `Requested start point: ${errMsgRange}`; | ||
const issueURL = 'https://github.com/AtomLinter/linter-eslint/issues/new'; | ||
const titleText = `Invalid position given by '${error.code}'`; | ||
const title = encodeURIComponent(titleText); | ||
const body = encodeURIComponent([ | ||
'JSHint returned a point that did not exist in the document being edited.', | ||
`Rule: \`${error.code}\``, | ||
rangeText, | ||
'', '', | ||
'<!-- If at all possible, please include code to reproduce this issue! -->', | ||
'', '', | ||
'Debug information:', | ||
'```json', | ||
JSON.stringify(await getDebugInfo(), null, 2), | ||
'```', | ||
].join('\n')); | ||
const newIssueURL = `${issueURL}?title=${title}&body=${body}`; | ||
return { | ||
type: 'Error', | ||
severity: 'error', | ||
html: `${escapeHTML(titleText)}. See the trace for details. ` + | ||
`<a href="${newIssueURL}">Report this!</a>`, | ||
filePath, | ||
range: atomlinter.generateRange(textEditor, 0), | ||
trace: [ | ||
{ | ||
type: 'Trace', | ||
text: `Original message: ${error.code} - ${error.reason}`, | ||
filePath, | ||
severity: 'info', | ||
}, | ||
{ | ||
type: 'Trace', | ||
text: rangeText, | ||
filePath, | ||
severity: 'info', | ||
}, | ||
], | ||
}; | ||
} |
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
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
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