Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

feat: strip prefix from methods in code generation #1750

Merged
merged 6 commits into from
Nov 10, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions crates/rslint_parser/src/ast/generated/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,7 @@ impl TsTypeAliasDecl {
pub fn eq_token(&self) -> SyntaxResult<SyntaxToken> {
support::as_mandatory_token(&self.syntax, T ! [=])
}
pub fn ts_type(&self) -> SyntaxResult<TsType> { support::as_mandatory_node(&self.syntax) }
pub fn ty(&self) -> SyntaxResult<TsType> { support::as_mandatory_node(&self.syntax) }
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TsNamespaceDecl {
Expand All @@ -1424,9 +1424,7 @@ impl TsNamespaceDecl {
pub fn dot_token(&self) -> Option<SyntaxToken> {
support::as_optional_token(&self.syntax, T ! [.])
}
pub fn ts_namespace_body(&self) -> SyntaxResult<TsNamespaceBody> {
support::as_mandatory_node(&self.syntax)
}
pub fn body(&self) -> SyntaxResult<TsNamespaceBody> { support::as_mandatory_node(&self.syntax) }
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TsModuleDecl {
Expand Down
82 changes: 41 additions & 41 deletions xtask/js.ungram
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,26 @@ Stmt =


// Block statement
BlockStmt = '{' stmts:Stmt* '}'
BlockStmt = '{' stmts: Stmt* '}'


// Empty statemnet
EmptyStmt = ';'?


// Expression statement
ExprStmt = Expr
ExprStmt = expr: Expr


// while statement
WhileStmt = 'while' Condition cons:Stmt
WhileStmt = 'while' condition: Condition cons:Stmt

// do while statement
DoWhileStmt = 'do' cons:Stmt 'while' condition:Condition ';'?


// if statement
IfStmt = 'if' Condition manual__cons:Stmt 'else' manual__alt:Stmt
IfStmt = 'if' condition: Condition manual__cons:Stmt 'else' manual__alt:Stmt


// for..in statement
Expand Down Expand Up @@ -126,7 +126,7 @@ ForOfStmt = 'for' '(' left:ForStmtInit 'of' right:Expr ')' cons:Stmt


// continue statement
ContinueStmt = 'continue' NameRef ';'?
ContinueStmt = 'continue' name_ref: NameRef ';'?


// break statement
Expand All @@ -140,7 +140,7 @@ ReturnStmt = 'return' value:Expr ';'?
WithStmt = 'with' condition:Condition cons:Stmt

// labelled statement
LabelledStmt = label:Name ':' Stmt
LabelledStmt = label:Name ':' stmt: Stmt

// switch statement
SwitchStmt =
Expand Down Expand Up @@ -230,7 +230,7 @@ NewExpr = 'new' type_args:TsTypeArgs? object:Expr arguments:ArgList
CallExpr = type_args:TsTypeArgs? callee:Expr arguments:ArgList

// await expression
AwaitExpr = 'await' Expr
AwaitExpr = 'await' expr: Expr

// yield expressoin
YieldExpr = 'yield' '*'? value:Expr
Expand Down Expand Up @@ -282,7 +282,7 @@ ObjectExpr = '{' props:(ObjectProp (',' ObjectProp)* ','?) '}'
// class expression
ClassExpr =
'class'
Name
name: Name
type_params:TsTypeParams?
parent:Expr?
'implements'?
Expand Down Expand Up @@ -325,7 +325,7 @@ ConstructorParamOrPat =
// This type is here only to matain compatiblity with rslit_parser
// A private property should only have the hash (#) or have the
// keyword `private` in the typescript version
PrivateProp = ClassProp
PrivateProp = class_prop: ClassProp
// @ematipico: they way it's written now is awful and can be improved a lot
ClassProp =
'declare'?
Expand Down Expand Up @@ -365,7 +365,7 @@ PatternOrExpr = Pattern | Expr
// sequence expression
SequenceExpr =
exprs:(Expr (',' Expr)*)
BinExpr
bin_expr: BinExpr


// grouping expression
Expand Down Expand Up @@ -463,7 +463,7 @@ KeyValuePattern = key:PropName ':' manual__value:Pattern


// expression pattern
ExprPattern = Expr
ExprPattern = expr: Expr



Expand Down Expand Up @@ -494,7 +494,7 @@ PropName =


// computed property
ComputedPropertyName = '[' Expr ']'
ComputedPropertyName = '[' expr: Expr ']'

// private property access
PrivatePropAccess = lhs:Expr '.' rhs:PrivateName
Expand All @@ -506,7 +506,7 @@ SpreadProp = '...' value:Expr
InitializedProp = key:Name '=' value:Expr

// property identifier
IdentProp = Name
IdentProp = name: Name

///////////////
// MISCELLANEOUS
Expand All @@ -529,7 +529,7 @@ Method =

SpreadElement = '...' element:Expr

PrivateName = '#' Name
PrivateName = '#' name: Name

Name = 'ident'

Expand All @@ -555,7 +555,7 @@ FnDecl =
'async'?
'function'
'*'?
Name
name: Name
type_parameters:TsTypeParams?
parameters:ParameterList
':'?
Expand All @@ -565,7 +565,7 @@ FnDecl =
VarDecl = ('var' | 'const' | manual__:'let') declared:(Declarator (',' Declarator)*) ';'?

// @ematipico: should we consider ClassDecl = ClassExpr ?
ClassDecl = 'class' Name ('extends' parent:NameRef)? body:ClassBody
ClassDecl = 'class' name: Name ('extends' parent:NameRef)? body:ClassBody

Declarator = pattern:Pattern '!' '=' value:Expr

Expand Down Expand Up @@ -607,19 +607,19 @@ DefaultDecl =
ExportDefaultExpr = 'export' 'type'? 'default'? expr:Expr

// export wildcard
ExportWildcard = 'export' 'type'? '*' 'as'? Ident? 'from'
ExportWildcard = 'export' 'type'? '*' 'as'? ident: Ident? 'from'


// export declaration
ExportDecl = 'export' 'type'? Decl
ExportDecl = 'export' 'type'? decl: Decl


// typescript export assignment
TsExportAssignment = 'export' '=' Expr ';'?
TsExportAssignment = 'export' '=' expr: Expr ';'?


// typescript namespace export
TsNamespaceExportDecl = 'export' 'as' 'namespace' Ident? ';'?
TsNamespaceExportDecl = 'export' 'as' 'namespace' ident: Ident? ';'?

ImportClause =
WildcardImport
Expand All @@ -638,7 +638,7 @@ Specifier = name:Ident manual__:'as'? manual__alias:Name
ImportStringSpecifier = 'string'


WildcardImport = '*' 'as'? Ident?
WildcardImport = '*' 'as'? ident: Ident?

// @ematipico this one is not entirely correct I think..
ExportNamed = 'export' 'type'? 'from'? '{' specifiers:(Specifier (',' Specifier)* ','?) * '}'
Expand All @@ -652,7 +652,7 @@ ExportNamed = 'export' 'type'? 'from'? '{' specifiers:(Specifier (',' Specifier
//

TsImportEqualsDecl =
('import' | 'export') Ident '=' module:TsModuleRef ';'?
('import' | 'export') ident: Ident '=' module:TsModuleRef ';'?


TsModuleRef =
Expand Down Expand Up @@ -698,25 +698,25 @@ TsType =

TsAny = 'any'
TsUnknown = 'unknown'
TsNumber = Ident
TsObject = Ident
TsBoolean = Ident
TsBigint = Ident
TsString = Ident
TsSymbol = Ident
TsNumber = ident: Ident
TsObject = ident: Ident
TsBoolean = ident: Ident
TsBigint = ident: Ident
TsString = ident: Ident
TsSymbol = ident: Ident
TsVoid = 'void'
TsUndefined = 'undefined'
TsNull= 'null'
TsNever = 'never'
TsThis = 'this'
TsLiteral = Ident
TsLiteral = ident: Ident


// typescript tuples
TsTuple = '[' elements:TsTupleElement ']'
// @ematipico: this is ugly and it's here only to maintain compatibility
// with the current parser API
TsTupleElement = Ident ':' '?' '...'? ty:TsType
TsTupleElement = ident: Ident ':' '?' '...'? ty:TsType

TsParen = '(' ty:TsType ')'

Expand All @@ -725,8 +725,8 @@ TsTypeRef = name:TsEntityName type_args:TsTypeArgs


// typescript enum
TsEnum = 'const'? 'enum' Ident '{' members:TsEnumMember* '}'
TsEnumMember = Ident '=' value:Expr
TsEnum = 'const'? 'enum' ident: Ident '{' members:TsEnumMember* '}'
TsEnumMember = ident: Ident '=' value:Expr

// typescript template literal types
TsTemplate = elements:TsTemplateElement
Expand All @@ -751,7 +751,7 @@ TsMappedType =
TsMappedTypeReadonly = '-'? '+'? 'readonly'?

// @ematipico: this type is correct and it's here only for compatibility
TsMappedTypeParam = '['? name:TsTypeName? ']'? Ident? ty:TsType
TsMappedTypeParam = '['? name:TsTypeName? ']'? ident: Ident? ty:TsType


// typescript array
Expand Down Expand Up @@ -798,7 +798,7 @@ TsObjectType = '{' members:TsTypeElement* '}'


// typescript infer
TsInfer = 'infer' Ident
TsInfer = 'infer' ident: Ident

// typescript import
// @ematipico not sure where the dot token should be placed
Expand All @@ -809,15 +809,15 @@ TsInfer = 'infer' Ident
// typescrit namesapce
// TODO: not sure where to put the dot
// https://github.com/rome/tools/issues/1729
TsNamespaceDecl = 'declare' Ident '.'? TsNamespaceBody
TsNamespaceDecl = 'declare' ident: Ident '.'? body: TsNamespaceBody
TsNamespaceBody = TsModuleBlock | TsNamespaceDecl
TsModuleBlock = '{' items:ModuleItem '}'


// type alias declaration
TsTypeAliasDecl = 'type' type_params:TsTypeParams '=' TsType
TsTypeAliasDecl = 'type' type_params:TsTypeParams '=' ty:TsType
TsTypeParams = '<'? params:TsTypeParam '>'?
TsTypeParam = Ident constraint:TsConstraint default:TsDefault
TsTypeParam = ident: Ident constraint:TsConstraint default:TsDefault

TsDefault = '=' ty:TsType
TsConstraint = 'extends' ty:TsType
Expand All @@ -826,7 +826,7 @@ TsConstraint = 'extends' ty:TsType

// typescript module declaration
// TODO: where does the dot go exactly?
TsModuleDecl = 'declare' 'global'? 'module' '.'? Ident body:TsNamespaceBody
TsModuleDecl = 'declare' 'global'? 'module' '.'? ident: Ident body:TsNamespaceBody


// typescript interface declaration
Expand All @@ -849,7 +849,7 @@ TsEntityName =
TsTypeName
| TsQualifiedPath

TsTypeName = Ident
TsTypeName = ident: Ident
TsQualifiedPath = lhs:TsEntityName '.' rhs:TsTypeName


Expand Down Expand Up @@ -892,6 +892,6 @@ TsConstructorParam = 'readonly' pat:Pattern

TsNonNull = expr:Expr '!'

TsAssertion = expr:Expr Ident '<' ty:TsType '>'
TsAssertion = expr:Expr ident: Ident '<' ty:TsType '>'

TsConstAssertion = expr:Expr Ident '<' 'const' '>'
TsConstAssertion = expr:Expr ident: Ident '<' 'const' '>'
2 changes: 1 addition & 1 deletion xtask/src/codegen/generate_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ pub fn generate_nodes(ast: &AstSrc) -> Result<String> {
}
}
Field::Node {
name: _,
ty,
optional,
has_many,
separated,
..
} => {
let is_built_in_tpe = &ty.eq(BUILT_IN_TYPE);
let ty = format_ident!("{}", &ty);
Expand Down
16 changes: 14 additions & 2 deletions xtask/src/codegen/kinds_src.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

use quote::{format_ident, quote};

const LANGUAGE_PREFIXES: [&str; 4] = ["js_", "ts_", "jsx_", "tsx_"];

pub struct KindsSrc<'a> {
pub punct: &'a [(&'a str, &'a str)],
pub keywords: &'a [&'a str],
Expand Down Expand Up @@ -504,10 +506,20 @@ impl Field {
format_ident!("{}_token", name)
}
Field::Node { name, .. } => {
if name == "type" {
let name = name;
let (prefix, tail) = name.split_once('_').unwrap_or(("", name));
let final_name = if LANGUAGE_PREFIXES.contains(&prefix) {
tail
} else {
name.as_str()
};

// this check here is to avoid emitting methods called "type()",
// where "type" is a reserved word
if final_name == "type" {
format_ident!("ty")
} else {
format_ident!("{}", name)
format_ident!("{}", final_name)
}
}
}
Expand Down