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

refactor(swc): Change AST parsing error to return struct with message and location #10911

Merged
merged 3 commits into from
Jun 11, 2021

Conversation

dsherret
Copy link
Member

@dsherret dsherret commented Jun 9, 2021

  1. For type stripping in the REPL it is nicer to get a diagnostic without the file name.
  2. There actually will only ever be one diagnostic returned currently, so I removed DiagnosticBuffer. We can add back something similar, if we ever look at parser.take_errors() for diagnostics.
  3. I was confused about what EmitOptions#check_js did then realized it was never used for emitting (therefore, not an emit option). I removed this and changed the other code to just look at the value from the tsconfig.json.

@@ -89,57 +82,18 @@ impl std::fmt::Display for Location {
}
}

/// A buffer for collecting diagnostic messages from the AST parser.
#[derive(Debug)]
pub struct DiagnosticBuffer(Vec<String>);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code only possibly returns one diagnostic. Removed and replaced with a single Diagnostic error struct. This will also allow me to format a diagnostic specifically for the REPL (no file name).

@@ -192,9 +146,6 @@ pub enum ImportsNotUsedAsValues {
/// Options which can be adjusted when transpiling a module.
#[derive(Debug, Clone)]
pub struct EmitOptions {
/// Indicate if JavaScript is being checked/transformed as well, or if it is
/// only TypeScript.
pub check_js: bool,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used by the emit code, so removed (see PR description for more details).

})
let module = parser.parse_module().map_err(move |err| Diagnostic {
location: sm.lookup_char_pos(err.span().lo).into(),
message: err.into_kind().msg().to_string(),
})?;
Copy link
Member Author

@dsherret dsherret Jun 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the code above was copied from dprint-plugin-typescript when I didn't know the full swc API (my bad). This new code is a bit more straightforward and we can use parser.take_errors() in the future to get all the errors if necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the code above was copied from dprint-plugin-typescript

That's very likely

@dsherret dsherret marked this pull request as ready for review June 9, 2021 16:43
@dsherret dsherret changed the title chore(swc): Change AST parsing error to return struct with message and location refactor(swc): Change AST parsing error to return struct with message and location Jun 9, 2021
Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

})
let module = parser.parse_module().map_err(move |err| Diagnostic {
location: sm.lookup_char_pos(err.span().lo).into(),
message: err.into_kind().msg().to_string(),
})?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the code above was copied from dprint-plugin-typescript

That's very likely

@dsherret dsherret merged commit 1a92c39 into denoland:main Jun 11, 2021
@dsherret dsherret deleted the ast-parsing-diagnostic-error branch June 11, 2021 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants