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

Refactor Class AST Tree Structure #1791

Merged
merged 12 commits into from
Nov 18, 2021
Merged

Refactor Class AST Tree Structure #1791

merged 12 commits into from
Nov 18, 2021

Conversation

MichaReiser
Copy link
Contributor

@MichaReiser MichaReiser commented Nov 17, 2021

Summary

This PR refactors the AST structures for class declarations and class expressions as defined in #1725.

  • Class declaration & Expression
    • Rename from ClassDecl/ClassExpr to JsClassDeclaration/JsClassExpression
    • Renamed name to id
    • Introduced a JsExtendsClause that wraps the extends keyword and the parent class
    • Renamed body to members and removed the ClassBody node
  • Constructor
    • Rename from Constructor to JsConstructorClassMember
    • Rename name to id
    • Parse "constructor" and 'constructor' methods as JsConstructorClassMembers
  • Method
    • Renamed Method to JsMethodClassMember
  • Properties
    • Renamed ClassProp to JsPropertyClassMember
  • Getter
    • Renamed Getter to JsGetterClassMember
    • Removed the parameter list (getters can't have parameters)
  • Setter
    • Renamed Setter to JsSetterClassMember
    • Replaced the parameter list with a single parameter
  • Private properties
    • Deleted the PrivateProp and instead introduce a JsPrivatePropertyClassMemberName type since all members can be private (except constructors)
  • Replaced JsEmptyStatement with JsEmptyMember (these aren't proper statements)
  • TS Accessibility: Rename to access modifier to match typescripts class documentation
  • Rename JsComputedObjectMemberName and JsStaticObjectMemberName to JsComputedMemberName and JsLiteralMember because they can be shared between classes and objects
  • Rename TsReturnType to TsTypeAnnotation so that it can also be used for properties, parameters, etc.

This PR also adds a set of new tests for classes to verify the member parsing. I did so because I had to refactor much of the parsing logic to make sense of it (and reduce some repetitive code and fix some parsing issues related to semicolons).

I further extracted the class parsing from the decl.rs into its own class.rs. You can see my refactorings if you look at the individual commits.

Test Plan

Added new tests, verified that coverage isn't changing.

Disclaimer

I didn't verify if the typescript parsing is working correctly or if the typescript grammar is correct. We need to tackle typescript support separately.

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Nov 17, 2021

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 25b1fe2
Status: ✅  Deploy successful!
Preview URL: https://5035af02.tools-8rn.pages.dev

View logs

@@ -0,0 +1,854 @@
use crate::state::StateGuard;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Extracted from decl. You can review the individual commits if you want to see what I've refactored (quiet a bit)

@github-actions
Copy link

github-actions bot commented Nov 17, 2021

Test262 comparison coverage results on windows-latest

Test result main count This PR count Difference
Total 17608 17608 0
Passed 16771 16771 0
Failed 836 836 0
Panics 1 1 0
Coverage 95.25% 95.25% 0.00%

1 similar comment
@github-actions
Copy link

Test262 comparison coverage results on windows-latest

Test result main count This PR count Difference
Total 17608 17608 0
Passed 16771 16771 0
Failed 836 836 0
Panics 1 1 0
Coverage 95.25% 95.25% 0.00%

// class bodies are implicitly strict
let mut guard = p.with_state(ParserState {
strict: Some(StrictMode::Class(p.cur_tok().range)),
..p.state.clone()
});

let idt = if (guard.at_ts(token_set![T![await], T![yield], T![ident]])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The opt_binding_identifier already checks (guard.at_ts(token_set![T![await], T![yield], T![ident]]) so we can remove it from here

Comment on lines 64 to 68
if guard.at(T![this]) {
let m = guard.start();
guard.bump_remap(T![ident]);
Some(m.complete(&mut *guard, NAME))
} else {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I played around with typescript but didn't find a way how to make this a valid class id. So I decided to remove it

Comment on lines 253 to 255
if p.nth_at(offset, T![?]) {
offset += 1;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

is_method_class_member and the other is helpers already handle this.

@@ -534,97 +537,97 @@ fn class_member_no_semi(p: &mut Parser) -> Option<CompletedMarker> {
None
}

fn is_prop(p: &Parser, offset: usize) -> bool {
(p.at(T![?]) && is_prop(p, offset + 1))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This must-have recursed forever because p.at(T![?]) doesn't respect the offset

@@ -0,0 +1,39 @@
use crate::{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, sorry. Don't know why git didn't recognise my renames of the formatter files, maybe because I rebased the commits?

@github-actions
Copy link

github-actions bot commented Nov 17, 2021

Test262 comparison coverage results on ubuntu-latest

Test result main count This PR count Difference
Total 17608 17608 0
Passed 16771 16771 0
Failed 836 836 0
Panics 1 1 0
Coverage 95.25% 95.25% 0.00%

@github-actions
Copy link

Test262 comparison coverage results on windows-latest

Test result main count This PR count Difference
Total 17608 17608 0
Passed 16771 16771 0
Failed 836 836 0
Panics 1 1 0
Coverage 95.25% 95.25% 0.00%

@github-actions
Copy link

Test262 comparison coverage results on ubuntu-latest

Test result main count This PR count Difference
Total 17608 17608 0
Passed 16771 16771 0
Failed 836 836 0
Panics 1 1 0
Coverage 95.25% 95.25% 0.00%

@github-actions
Copy link

Test262 comparison coverage results on windows-latest

Test result main count This PR count Difference
Total 17608 17608 0
Passed 16771 16771 0
Failed 836 836 0
Panics 1 1 0
Coverage 95.25% 95.25% 0.00%

@MichaReiser MichaReiser changed the title Feature/classes Refactor Class AST Tree Structure Nov 17, 2021
@MichaReiser MichaReiser requested review from ematipico and jamiebuilds and removed request for ematipico November 17, 2021 13:22
@MichaReiser MichaReiser marked this pull request as ready for review November 17, 2021 13:22
@MichaReiser MichaReiser mentioned this pull request Nov 17, 2021
47 tasks
@github-actions
Copy link

Test262 comparison coverage results on ubuntu-latest

Test result main count This PR count Difference
Total 17608 17608 0
Passed 16771 16771 0
Failed 836 836 0
Panics 1 1 0
Coverage 95.25% 95.25% 0.00%

@github-actions
Copy link

Test262 comparison coverage results on windows-latest

Test result main count This PR count Difference
Total 17608 17608 0
Passed 16771 16771 0
Failed 836 836 0
Panics 1 1 0
Coverage 95.25% 95.25% 0.00%

Copy link
Contributor

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Nothing major popped into my eye! I am glad that we decoupled the parsing logic of classes into a separate file!

xtask/js.ungram Show resolved Hide resolved
xtask/js.ungram Show resolved Hide resolved
crates/rslint_parser/src/syntax/class.rs Show resolved Hide resolved
crates/rslint_parser/src/syntax/stmt.rs Show resolved Hide resolved
@rome rome deleted a comment from github-actions bot Nov 17, 2021
@rome rome deleted a comment from github-actions bot Nov 17, 2021
@rome rome deleted a comment from github-actions bot Nov 17, 2021
@rome rome deleted a comment from github-actions bot Nov 17, 2021
@MichaReiser MichaReiser merged commit cbeeaaf into main Nov 18, 2021
@MichaReiser MichaReiser deleted the feature/classes branch November 18, 2021 07:31
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants