-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #910 from demergent-labs/902_tighten_external_cani…
…sters Changed External Canister Parsing Errors
- Loading branch information
Showing
7 changed files
with
179 additions
and
55 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/compiler/typescript_to_rust/azle_generate/src/errors/external_canister_method.rs
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,31 @@ | ||
#[derive(Clone, Debug)] | ||
pub enum ParseError { | ||
InvalidDecorator, | ||
InvalidReturnType, | ||
MissingCanisterResultAnnotation, | ||
MissingDecorator, | ||
MissingTypeAnnotation, | ||
MissingTypeArgument, | ||
MultipleDecorators, | ||
NamespaceQualifiedType, | ||
TooManyReturnTypes, | ||
UnallowedComputedProperty, | ||
} | ||
|
||
impl ParseError { | ||
pub fn error_message(&self) -> String { | ||
let str = match self { | ||
Self::InvalidDecorator => "Invalid decorator. Only @query and @update are permitted.", | ||
Self::InvalidReturnType => "Method has an invalid return type. Only function return types are permitted.", | ||
Self::MissingCanisterResultAnnotation => "Invalid return type. External canister methods must wrap their return types in the CanisterResult<T> generic type.", | ||
Self::MissingDecorator => "Missing decorator. External canister methods must be decorated with either @query or @update.", | ||
Self::MissingTypeAnnotation => "Missing type annotation. External canister methods must specify a return type.", | ||
Self::MissingTypeArgument => "Missing type argument. Generic type CanisterResult requires 1 type argument.", | ||
Self::MultipleDecorators => "Too many decorators. External canister methods can only specify one decorator: @query or @update.", | ||
Self::NamespaceQualifiedType => "Unsupported data type. Qualified types are not currently supported. Try importing the type directly.", | ||
Self::TooManyReturnTypes => "Too many return types. Generic type CanisterResult requires 1 type argument.", | ||
Self::UnallowedComputedProperty => "Unallowed computed property. Computed properties in external canister definitions aren't currently supported.", | ||
}; | ||
str.to_string() | ||
} | ||
} |
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
66 changes: 66 additions & 0 deletions
66
src/compiler/typescript_to_rust/azle_generate/src/ts_ast/class_decl/errors.rs
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,66 @@ | ||
use swc_ecma_ast::{ClassDecl, ClassMember, ClassProp}; | ||
|
||
use crate::{ | ||
errors::external_canister_method::ParseError, | ||
ts_ast::{ | ||
source_map::{GetSourceFileInfo, SourceMapped}, | ||
GetName, | ||
}, | ||
}; | ||
|
||
impl SourceMapped<'_, ClassDecl> { | ||
pub fn build_invalid_class_member_error_message(&self, class_member: &ClassMember) -> String { | ||
let member_type = match class_member { | ||
ClassMember::Constructor(_) => "constructor", | ||
ClassMember::Method(_) => "method", | ||
ClassMember::PrivateMethod(_) => "private method", | ||
ClassMember::ClassProp(_) => "class prop", | ||
ClassMember::PrivateProp(_) => "private prop", | ||
ClassMember::TsIndexSignature(_) => "TS index signature", | ||
ClassMember::Empty(_) => "empty block", | ||
ClassMember::StaticBlock(_) => "static block", | ||
}; | ||
|
||
let span = match class_member { | ||
ClassMember::Constructor(constructor) => constructor.span, | ||
ClassMember::Method(method) => method.span, | ||
ClassMember::PrivateMethod(private_method) => private_method.span, | ||
ClassMember::ClassProp(class_prop) => class_prop.span, | ||
ClassMember::PrivateProp(private_prop) => private_prop.span, | ||
ClassMember::TsIndexSignature(ts_index_signature) => ts_index_signature.span, | ||
ClassMember::Empty(empty) => empty.span, | ||
ClassMember::StaticBlock(static_block) => static_block.span, | ||
}; | ||
|
||
let external_canister_class_name = self.ident.get_name().to_string(); | ||
|
||
let origin = self.source_map.get_origin(span); | ||
let line_number = self.source_map.get_line_number(span); | ||
let column_number = self.source_map.get_range(span).0 + 1; | ||
|
||
format!( | ||
"Invalid {} in class {}\nat {}:{}:{}\n\nHelp: Remove this member or make it a property", | ||
member_type, external_canister_class_name, origin, line_number, column_number | ||
) | ||
} | ||
|
||
pub fn build_invalid_class_prop_error_message( | ||
&self, | ||
class_prop: &ClassProp, | ||
error_message: ParseError, | ||
) -> String { | ||
let external_canister_class_name = self.ident.get_name().to_string(); | ||
|
||
let origin = self.source_map.get_origin(class_prop.span); | ||
let line_number = self.source_map.get_line_number(class_prop.span); | ||
let column_number = self.source_map.get_range(class_prop.span).0 + 1; | ||
let location = format!("{}:{}:{}", origin, line_number, column_number); | ||
|
||
format!( | ||
"{}\n\nin class {}\nat {}", | ||
error_message.error_message(), | ||
external_canister_class_name, | ||
location | ||
) | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/compiler/typescript_to_rust/azle_generate/src/ts_ast/class_decl/mod.rs
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
mod errors; | ||
mod get_dependencies; | ||
mod to_act_external_canister; |
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