mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-01 17:32:25 +00:00
Document LinterResult::has_syntax_error
and add Parsed::has_no_syntax_errors
(#16443)
Summary -- This is a follow up addressing the comments on #16425. As @dhruvmanila pointed out, the naming is a bit tricky. I went with `has_no_errors` to try to differentiate it from `is_valid`. It actually ends up negated in most uses, so it would be more convenient to have `has_any_errors` or `has_errors`, but I thought it would sound too much like the opposite of `is_valid` in that case. I'm definitely open to suggestions here. Test Plan -- Existing tests.
This commit is contained in:
parent
a3ae76edc0
commit
37fbe58b13
6 changed files with 50 additions and 26 deletions
|
@ -342,21 +342,47 @@ impl<T> Parsed<T> {
|
|||
self.errors
|
||||
}
|
||||
|
||||
/// Returns `true` if the parsed source code is valid i.e., it has no syntax errors.
|
||||
/// Returns `true` if the parsed source code is valid i.e., it has no [`ParseError`]s.
|
||||
///
|
||||
/// Note that this does not include version-related
|
||||
/// [`unsupported_syntax_errors`](Parsed::unsupported_syntax_errors).
|
||||
pub fn is_valid(&self) -> bool {
|
||||
/// Note that this does not include version-related [`UnsupportedSyntaxError`]s.
|
||||
///
|
||||
/// See [`Parsed::has_no_syntax_errors`] for a version that takes these into account.
|
||||
pub fn has_valid_syntax(&self) -> bool {
|
||||
self.errors.is_empty()
|
||||
}
|
||||
|
||||
/// Returns `true` if the parsed source code is invalid i.e., it has [`ParseError`]s.
|
||||
///
|
||||
/// Note that this does not include version-related [`UnsupportedSyntaxError`]s.
|
||||
///
|
||||
/// See [`Parsed::has_no_syntax_errors`] for a version that takes these into account.
|
||||
pub fn has_invalid_syntax(&self) -> bool {
|
||||
!self.has_valid_syntax()
|
||||
}
|
||||
|
||||
/// Returns `true` if the parsed source code does not contain any [`ParseError`]s *or*
|
||||
/// [`UnsupportedSyntaxError`]s.
|
||||
///
|
||||
/// See [`Parsed::has_valid_syntax`] for a version specific to [`ParseError`]s.
|
||||
pub fn has_no_syntax_errors(&self) -> bool {
|
||||
self.has_valid_syntax() && self.unsupported_syntax_errors.is_empty()
|
||||
}
|
||||
|
||||
/// Returns `true` if the parsed source code contains any [`ParseError`]s *or*
|
||||
/// [`UnsupportedSyntaxError`]s.
|
||||
///
|
||||
/// See [`Parsed::has_invalid_syntax`] for a version specific to [`ParseError`]s.
|
||||
pub fn has_syntax_errors(&self) -> bool {
|
||||
!self.has_no_syntax_errors()
|
||||
}
|
||||
|
||||
/// Returns the [`Parsed`] output as a [`Result`], returning [`Ok`] if it has no syntax errors,
|
||||
/// or [`Err`] containing the first [`ParseError`] encountered.
|
||||
///
|
||||
/// Note that any [`unsupported_syntax_errors`](Parsed::unsupported_syntax_errors) will not
|
||||
/// cause [`Err`] to be returned.
|
||||
pub fn as_result(&self) -> Result<&Parsed<T>, &[ParseError]> {
|
||||
if self.is_valid() {
|
||||
if self.has_valid_syntax() {
|
||||
Ok(self)
|
||||
} else {
|
||||
Err(&self.errors)
|
||||
|
@ -369,7 +395,7 @@ impl<T> Parsed<T> {
|
|||
/// Note that any [`unsupported_syntax_errors`](Parsed::unsupported_syntax_errors) will not
|
||||
/// cause [`Err`] to be returned.
|
||||
pub(crate) fn into_result(self) -> Result<Parsed<T>, ParseError> {
|
||||
if self.is_valid() {
|
||||
if self.has_valid_syntax() {
|
||||
Ok(self)
|
||||
} else {
|
||||
Err(self.into_errors().into_iter().next().unwrap())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue