-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Capture parse and source errors and make them redirect comments
- Loading branch information
1 parent
5cabefa
commit fbe45c6
Showing
12 changed files
with
189 additions
and
30 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
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
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
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,24 @@ | ||
import { NoSourceError } from "~src/errors/NoSourceError" | ||
import { AnalyzerOutput } from "./AnalyzerOutput" | ||
import { ERROR_CAPTURED_NO_SOURCE } from "~src/comments/shared" | ||
|
||
/** | ||
* Makes a generic output based on a NoSourceError | ||
* | ||
* @export | ||
* @param {NoSourceError} err | ||
* @returns {Output} | ||
*/ | ||
export function makeNoSourceOutput(err: NoSourceError): Output { | ||
const output = new AnalyzerOutput() | ||
|
||
output.add(ERROR_CAPTURED_NO_SOURCE({ | ||
expected: err.expected, | ||
available: err.available.join(', ') | ||
})) | ||
|
||
output.redirect() | ||
process.exitCode = err.code | ||
|
||
return output | ||
} |
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,61 @@ | ||
import { AnalyzerOutput } from "./AnalyzerOutput" | ||
import { ParserError } from "~src/errors/ParserError" | ||
import { Source } from "~src/analyzers/SourceImpl" | ||
import { AST_NODE_TYPES } from "@typescript-eslint/typescript-estree" | ||
import { PARSE_ERROR } from "~src/comments/shared" | ||
|
||
/** | ||
* Makes a generic output, based on a ParserError | ||
* | ||
* @export | ||
* @param {ParserError} err | ||
* @returns {Output} | ||
*/ | ||
export function makeParseErrorOutput(err: ParserError): Output { | ||
const output = new AnalyzerOutput() | ||
|
||
const { message, ...details } = err.original | ||
const source = new Source(err.source || '') | ||
|
||
const startLine = details.lineNumber - 2 | ||
const endLine = details.lineNumber + 3 /* last line might be empty */ | ||
|
||
// Select all the source code until a few lines after the error. The end line | ||
// is denoted by endline. The rest of the options fakes a "parsed source". | ||
// | ||
const surroundingSource = source.getLines({ | ||
loc: { | ||
start: { line: 0, column: 0 }, | ||
end: { line: endLine, column: -1 } | ||
}, | ||
sourceType: 'module', | ||
body: [], | ||
range: [0, Infinity], | ||
type: AST_NODE_TYPES.Program | ||
}) | ||
|
||
// Insert the marker BELOW, where the parse error occurred | ||
// -------^ | ||
// | ||
surroundingSource.splice( | ||
details.lineNumber, | ||
0, | ||
'^'.padStart(details.column + 1, '-') | ||
) | ||
|
||
// Create the error message, but only show few lines before ... after the | ||
// parse error location. These are denoted by startLine (and the source | ||
// array was already limited to endLine). | ||
// | ||
output.add(PARSE_ERROR({ | ||
error: message, | ||
details: surroundingSource | ||
.slice(Math.max(0, startLine - 1)) | ||
.join('\n') | ||
})) | ||
|
||
output.redirect() | ||
process.exitCode = err.code | ||
|
||
return output | ||
} |
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