Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add parse error details #6

Merged
merged 3 commits into from Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silly-ladybugs-study.md
@@ -0,0 +1,5 @@
---
"sh-syntax": patch
---

feat: add parse error details
1 change: 1 addition & 0 deletions .prettierignore
@@ -1 +1,2 @@
test/fixtures
vendors
17 changes: 16 additions & 1 deletion main.go
Expand Up @@ -69,7 +69,22 @@ func main() {

file, err := parser.Parse(bytes.NewReader([]byte(*text)), "path")
if err != nil {
result["error"] = err.Error()
error, ok := err.(syntax.ParseError)
if ok {
result["error"] = map[string]interface{}{
"filename": error.Filename,
"incomplete": error.Incomplete,
"text": error.Text,
"pos": map[string]interface{}{
"col": error.Col(),
"line": error.Line(),
"offset": error.Offset(),
},
"message": error.Error(),
}
} else {
result["error"] = err.Error()
}
} else {
var buf bytes.Buffer
writer := io.Writer(&buf)
Expand Down
Binary file modified main.wasm
Binary file not shown.
40 changes: 39 additions & 1 deletion src/print.ts
Expand Up @@ -3,6 +3,41 @@ import { ShOptions } from './types.js'

let id = 0

export class ParseError extends Error {
filename: string
incomplete: boolean
text: string
pos: {
col: number
line: number
offset: number
}

constructor({
filename,
incomplete,
text,
pos,
message,
}: {
filename: string
incomplete: boolean
text: string
pos: {
col: number
line: number
offset: number
}
message: string
}) {
super(message)
this.filename = filename
this.incomplete = incomplete
this.text = text
this.pos = pos
}
}

export const getPrinter = (
getWasmFile: () => BufferSource | Promise<BufferSource>,
) => {
Expand Down Expand Up @@ -78,7 +113,10 @@ export const getPrinter = (
delete Go.__shProcessing[uid]

if ('error' in processed) {
throw new Error(processed.error)
/* istanbul ignore next */
throw typeof processed.error === 'string'
? new Error(processed.error)
: new ParseError(processed.error)
}

return processed.text
Expand Down
2 changes: 2 additions & 0 deletions test/__snapshots__/fixture.spec.ts.snap
Expand Up @@ -38,6 +38,8 @@ EXPOSE 2015
"
`;

exports[`parser and printer should format all fixtures: error.sh 1`] = `[Error: path:1:6: a command can only contain words and redirects; encountered )]`;

exports[`parser and printer should format all fixtures: hosts 1`] = `
"##
# Host Database
Expand Down
14 changes: 9 additions & 5 deletions test/fixture.spec.ts
Expand Up @@ -15,11 +15,15 @@ describe('parser and printer', () => {
for (const filepath of await fs.promises.readdir(fixtures)) {
const input = fs.readFileSync(path.resolve(fixtures, filepath), 'utf8')

const output = await print(input, {
filepath,
})

expect(output).toMatchSnapshot(filepath)
try {
const output = await print(input, {
filepath,
})
expect(output).toMatchSnapshot(filepath)
} catch (err: unknown) {
// eslint-disable-next-line jest/no-conditional-expect
expect(err).toMatchSnapshot(filepath)
}
}
})
})
1 change: 1 addition & 0 deletions test/fixtures/error.sh
@@ -0,0 +1 @@
echo )
19 changes: 17 additions & 2 deletions vendors/wasm_exec.d.ts
Expand Up @@ -7,7 +7,22 @@ declare global {
class Go {
static __shProcessing: Record<
number,
{ error: string } | { text: string }
| {
error:
| string
| {
filename: string
incomplete: boolean
text: string
pos: {
col: number
line: number
offset: number
}
message: string
}
}
| { text: string }
>

_pendingEvent: { id: number }
Expand All @@ -23,4 +38,4 @@ declare global {
run(instance: WebAssembly.Instance): Promise<void>
}
}
}
}