-
-
Notifications
You must be signed in to change notification settings - Fork 63
feat: Better resolution error hints #2404
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
Merged
Merged
Changes from 23 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
207896e
Add a stub of implementation
aleksanderkatan 26a066a
Add a baseline statement and expression methods
aleksanderkatan 3d48eb4
Add tests
aleksanderkatan 292db54
Implement remaining nodes
aleksanderkatan eb61683
Better string handling
aleksanderkatan 3a30b58
sanity tests
aleksanderkatan 84fe830
More wrapIfComplex
aleksanderkatan edbc49f
Handle numeric literal member access
aleksanderkatan d453c8c
More sanity tests
aleksanderkatan 6b5270b
TS test
aleksanderkatan 5360076
Forbid "!="
aleksanderkatan a3b916e
Use `stringifyExpression` in some wgslGenerator errors
aleksanderkatan 6024c19
Add a test for parameter assignment
aleksanderkatan f5c04d6
Update more errors
aleksanderkatan 0f69e19
Update member access error
aleksanderkatan 2510f25
Merge remote-tracking branch 'origin/main' into feat/better-resolutio…
aleksanderkatan bef7d6f
Update index access error
aleksanderkatan 5d1563c
Rename files
aleksanderkatan d8b7811
Fix d.ref return
aleksanderkatan 6d4359f
nits
aleksanderkatan 0488701
Update isExpression
aleksanderkatan da67b28
Use stringifyNode instead of two separate functions
aleksanderkatan e04b702
Update tests, make indent mandatory
aleksanderkatan c80dad5
Review fixes
aleksanderkatan a744562
Merge branch 'main' into feat/better-resolution-error-hints
aleksanderkatan c532f31
Merge remote-tracking branch 'origin/main' into feat/better-resolutio…
aleksanderkatan eedab41
Merge remote-tracking branch 'origin/main' into feat/better-resolutio…
aleksanderkatan d0f3dc3
Merge branch 'main' into feat/better-resolution-error-hints
aleksanderkatan 082936c
Remove sanity tests
aleksanderkatan f588fa1
Merge branch 'main' into feat/better-resolution-error-hints
aleksanderkatan 1f932d5
Merge branch 'main' into feat/better-resolution-error-hints
aleksanderkatan 1c16e1d
Merge branch 'main' into feat/better-resolution-error-hints
aleksanderkatan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,213 @@ | ||
| import * as tinyest from 'tinyest'; | ||
|
|
||
| const { NodeTypeCatalog: NODE } = tinyest; | ||
|
|
||
| export function stringifyNode(node: tinyest.AnyNode): string { | ||
| if (isExpression(node)) { | ||
| return stringifyExpression(node, ''); | ||
| } | ||
| return stringifyStatement(node, ''); | ||
| } | ||
|
|
||
| function stringifyStatement(node: tinyest.Statement, ident: string): string { | ||
| if (isExpression(node)) { | ||
| return `${ident}${stringifyExpression(node, ident)};`; | ||
| } | ||
|
|
||
| if (node[0] === NODE.block) { | ||
| const statements = node[1].map((n) => stringifyStatement(n, ident + ' ')); | ||
| return `{\n${statements.join('\n')}\n${ident}}`; | ||
| } | ||
|
|
||
| if (node[0] === NODE.return) { | ||
| const expr = node[1] === undefined ? '' : ` ${stringifyExpression(node[1], '')}`; | ||
| return `${ident}return${expr};`; | ||
| } | ||
|
|
||
| if (node[0] === NODE.if) { | ||
| const cond = stringifyExpression(node[1], ident); | ||
| const then = stringifyStatement(node[2], ident); | ||
| const base = `${ident}if (${cond}) ${then}`; | ||
| if (node[3] !== undefined) { | ||
| return `${base} else ${stringifyStatement(node[3], ident)}`; | ||
| } | ||
| return base; | ||
| } | ||
|
|
||
| if (node[0] === NODE.let) { | ||
| if (node[2] !== undefined) { | ||
| return `${ident}let ${node[1]} = ${stringifyExpression(node[2], ident)};`; | ||
| } | ||
| return `${ident}let ${node[1]};`; | ||
| } | ||
|
|
||
| if (node[0] === NODE.const) { | ||
| if (node[2] !== undefined) { | ||
| return `${ident}const ${node[1]} = ${stringifyExpression(node[2], ident)};`; | ||
| } | ||
| return `${ident}const ${node[1]};`; | ||
| } | ||
|
|
||
| if (node[0] === NODE.for) { | ||
| const init = node[1] ? stringifyStatement(node[1], '') : ';'; | ||
| const cond = node[2] ? stringifyExpression(node[2], ident) : ''; | ||
| const update = node[3] ? stringifyStatement(node[3], '') : ''; | ||
| const body = stringifyStatement(node[4], ident); | ||
| return `${ident}for (${init} ${cond}; ${update.slice(0, -1) /* trim the ';' */}) ${body}`; | ||
| } | ||
|
|
||
| if (node[0] === NODE.while) { | ||
| const cond = stringifyExpression(node[1], ident); | ||
| const body = stringifyStatement(node[2], ident); | ||
| return `${ident}while (${cond}) ${body}`; | ||
| } | ||
|
|
||
| if (node[0] === NODE.continue) { | ||
| return `${ident}continue;`; | ||
| } | ||
|
|
||
| if (node[0] === NODE.break) { | ||
| return `${ident}break;`; | ||
| } | ||
|
|
||
| if (node[0] === NODE.forOf) { | ||
| const leftKind = node[1][0] === NODE.const ? 'const' : 'let'; | ||
| const leftName = node[1][1]; | ||
| const right = stringifyExpression(node[2], ident); | ||
| const body = stringifyStatement(node[3], ident); | ||
| return `${ident}for (${leftKind} ${leftName} of ${right}) ${body}`; | ||
| } | ||
|
|
||
| assertExhaustive(node); | ||
| } | ||
|
|
||
| function stringifyExpression(node: tinyest.Expression, ident: string): string { | ||
| if (typeof node === 'string') { | ||
| return node; | ||
| } | ||
|
|
||
| if (typeof node === 'boolean') { | ||
| return `${node}`; | ||
| } | ||
|
|
||
| if (node[0] === NODE.numericLiteral) { | ||
| return node[1]; | ||
| } | ||
|
|
||
| if (node[0] === NODE.stringLiteral) { | ||
| return JSON.stringify(node[1]); | ||
| } | ||
|
|
||
| if (node[0] === NODE.arrayExpr) { | ||
| const elements = node[1].map((n) => stringifyExpression(n, ident)); | ||
| return `[${elements.join(', ')}]`; | ||
| } | ||
|
|
||
| if (node[0] === NODE.binaryExpr) { | ||
| return `${wrapIfComplex(node[1], ident)} ${node[2]} ${wrapIfComplex(node[3], ident)}`; | ||
| } | ||
|
|
||
| if (node[0] === NODE.assignmentExpr) { | ||
| return `${stringifyExpression(node[1], ident)} ${node[2]} ${stringifyExpression(node[3], ident)}`; | ||
| } | ||
|
|
||
| if (node[0] === NODE.logicalExpr) { | ||
| return `${wrapIfComplex(node[1], ident)} ${node[2]} ${wrapIfComplex(node[3], ident)}`; | ||
| } | ||
|
|
||
| if (node[0] === NODE.unaryExpr) { | ||
| // void, instanceof and delete require a space | ||
| const sep = node[1].length > 1 ? ' ' : ''; | ||
| return `${node[1]}${sep}${wrapIfComplex(node[2], ident)}`; | ||
| } | ||
|
|
||
| if (node[0] === NODE.call) { | ||
| const callee = wrapIfComplex(node[1], ident); | ||
| const args = node[2].map((n) => stringifyExpression(n, ident)).join(', '); | ||
| return `${callee}(${args})`; | ||
| } | ||
|
|
||
| if (node[0] === NODE.memberAccess) { | ||
| if (Array.isArray(node[1]) && node[1][0] === NODE.numericLiteral) { | ||
| return `(${stringifyExpression(node[1], ident)}).${node[2]}`; | ||
| } | ||
| return `${wrapIfComplex(node[1], ident)}.${node[2]}`; | ||
| } | ||
|
|
||
| if (node[0] === NODE.indexAccess) { | ||
| return `${wrapIfComplex(node[1], ident)}[${stringifyExpression(node[2], ident)}]`; | ||
| } | ||
|
|
||
| if (node[0] === NODE.preUpdate) { | ||
| return `${node[1]}${wrapIfComplex(node[2], ident)}`; | ||
| } | ||
|
|
||
| if (node[0] === NODE.postUpdate) { | ||
| return `${wrapIfComplex(node[2], ident)}${node[1]}`; | ||
| } | ||
|
|
||
| if (node[0] === NODE.objectExpr) { | ||
| const entries = Object.entries(node[1]).map( | ||
| ([key, val]) => `${key}: ${stringifyExpression(val, ident)}`, | ||
| ); | ||
| return `{ ${entries.join(', ')} }`; | ||
| } | ||
|
|
||
| if (node[0] === NODE.conditionalExpr) { | ||
| return `${wrapIfComplex(node[1], ident)} ? ${wrapIfComplex(node[2], ident)} : ${wrapIfComplex(node[3], ident)}`; | ||
| } | ||
|
|
||
| assertExhaustive(node); | ||
| } | ||
|
|
||
| function assertExhaustive(value: never): never { | ||
| throw new Error(`'${JSON.stringify(value)}' was not handled by the stringify function.`); | ||
| } | ||
|
|
||
| function isExpression(node: tinyest.AnyNode): node is tinyest.Expression { | ||
| if ( | ||
| typeof node === 'string' || | ||
| typeof node === 'boolean' || | ||
| node[0] === NODE.numericLiteral || | ||
| node[0] === NODE.stringLiteral || | ||
| node[0] === NODE.arrayExpr || | ||
| node[0] === NODE.binaryExpr || | ||
| node[0] === NODE.assignmentExpr || | ||
| node[0] === NODE.logicalExpr || | ||
| node[0] === NODE.unaryExpr || | ||
| node[0] === NODE.call || | ||
| node[0] === NODE.memberAccess || | ||
| node[0] === NODE.indexAccess || | ||
| node[0] === NODE.preUpdate || | ||
| node[0] === NODE.postUpdate || | ||
| node[0] === NODE.objectExpr || | ||
| node[0] === NODE.conditionalExpr | ||
| ) { | ||
| node satisfies tinyest.Expression; | ||
| return true; | ||
| } | ||
| node satisfies Exclude<tinyest.AnyNode, tinyest.Expression>; | ||
| return false; | ||
| } | ||
|
|
||
| const SIMPLE_NODES: number[] = [ | ||
| NODE.memberAccess, // highest precedence | ||
| NODE.indexAccess, // highest precedence | ||
| NODE.call, // highest precedence | ||
| NODE.arrayExpr, // [] make thinks not ambiguous | ||
|
aleksanderkatan marked this conversation as resolved.
Outdated
|
||
| NODE.stringLiteral, | ||
| NODE.numericLiteral, | ||
| ]; | ||
| /** | ||
| * Stringifies expression, and wraps it in parentheses if they cannot be trivially omitted | ||
| */ | ||
| function wrapIfComplex(node: tinyest.Expression, ident: string): string { | ||
| const s = stringifyExpression(node, ident); | ||
| if (typeof node === 'string' || typeof node === 'boolean') { | ||
| return s; | ||
| } | ||
| if (SIMPLE_NODES.includes(node[0])) { | ||
| return s; | ||
| } | ||
| return `(${s})`; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.