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

feat(rome_js_parser): Support optional variance annotation #4250

Merged
merged 26 commits into from
Mar 8, 2023

Conversation

nissy-dev
Copy link
Contributor

@nissy-dev nissy-dev commented Mar 1, 2023

Summary

Fix #2400

This PR is based on #2556

Test Plan

Documentation

  • The PR requires documentation
  • I will create a new PR to update the documentation

@netlify
Copy link

netlify bot commented Mar 1, 2023

Deploy Preview for docs-rometools canceled.

Built without sensitive environment variables

Name Link
🔨 Latest commit 7f1ec7e
🔍 Latest deploy log https://app.netlify.com/sites/docs-rometools/deploys/6407478bc3e6cc00074c23ef

@nissy-dev
Copy link
Contributor Author

I'm still working in progress. Mainly, I'm thinking about how to build AST and parse syntax for type parameter modifiers. Considering in/out modifier, it is not tough task and there is a reference PR like #2556. But const modifier also will land in TS 5.0. Considering about in/out/const modifier, the parse logic become more complex.

ex) https://www.typescriptlang.org/play?ts=5.1.0-dev.20230227#code/MYGwhgzhAECCA8B7ArgF2sRA7C6CWW0AKgHzQDeAvgLABQdokMAQvJjuivoaRTfbUZRoAYXgEM2XMTJU6DcMIAiSNLP51UATwAOAU2gBRNlPS8AvMQDc82gDNkWYKjzZoAMXE8SACgCUfDa0QA

Therefore, it have taken much time than I expected. If anyone have mush time for this task, it is ok if this task is assigned to others.

@nissy-dev
Copy link
Contributor Author

nissy-dev commented Mar 2, 2023

I will work for reporting error about duplicated modifiers and modifiers order. I update the PR in a few days.

@nissy-dev nissy-dev marked this pull request as ready for review March 6, 2023 13:03
@nissy-dev
Copy link
Contributor Author

This PR is ready for review.

pub(crate) fn parse_ts_type_parameters_with_modifiers(
p: &mut JsParser,
context: TypeContext,
allow_in_out_modifier: bool,
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see any cases where allow_in_out_modifier is false. Is it a bug or did I miss some code?

Copy link
Contributor Author

@nissy-dev nissy-dev Mar 7, 2023

Choose a reason for hiding this comment

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

This argument was currently not used. But, when supporting const modifier (see: #4227), it will be used. parse_ts_type_parameters_with_modifiers function need to supports these cases.

function, method: parse only const modifier
interface, type alias: parse only in/out modifier
class: parse const and in/out modifier

I think it is ok to remove this argument.

kind: TypeParameterModifierKind::Out,
range: p.cur_range(),
},
_ => unreachable!(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Usually, it's helpful to add a message about why this is unreachable. I suppose here it's unreachable because keywords that are not "in" and "out" are checked earlier?

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 added comments 👍

m.complete(p, TS_TYPE_PARAMETER)
})
// test_err ts type_parameter_modifier1
// export default function foo<in T>() {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we test cases where we use incorrect words?

type Foo<innn T> = {}

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 added the test 👍

fn parse_ts_type_parameter(
p: &mut JsParser,
context: TypeContext,
allow_in_out_modifier: bool,
Copy link
Contributor

Choose a reason for hiding this comment

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

Would you mind turning this into an enum?

InOutModifier {
	Allowed,
	Disallowed
}

The reason why I am asking is that we can document when the modifier is allowed or when it's not. Or, we in alternative, we should document the function and explains when allow_in_out_modifier is true and when it's false

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 agreed 👍 I refactor bool flag to bitflags like DISALLOW_CONDITIONAL_TYPES in TypeContext.

@@ -61,15 +61,15 @@ impl JsSyntaxKind {
/// Returns `true` for any contextual (await) or non-contextual keyword
#[inline]
pub const fn is_keyword(self) -> bool {
(self as u16) <= (JsSyntaxKind::OF_KW as u16)
(self as u16) <= (JsSyntaxKind::OUT_KW as u16)
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these changes correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. This function judge whether the kind is keyword or not by enum order in JsSyntaxKind
see: https://github.com/nissy-dev/tools/blob/979d302e9d0360c1108f56c5ff6ccd2d3b9ef3d6/crates/rome_js_syntax/src/generated/kind.rs#L8

// declare class Foo<in T> {}
// declare class Foo<out T> {}
// declare interface Foo<in T> {}
// declare interface Foo<out T> {}
Copy link
Contributor

Choose a reason for hiding this comment

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

It's HUGE! 🔥🔥

/// Whether 'in' and 'out' modifiers are allowed in the current context.
///
/// By default, 'in' and 'out' modifiers are not allowed.
const ALLOW_IN_OUT_MODIFIER = 1 << 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

Great use of context!

@nissy-dev nissy-dev merged commit d1dc465 into rome:main Mar 8, 2023
ematipico pushed a commit that referenced this pull request Mar 10, 2023
* feat: 🎸 init

* fix: 🐛 parse error

* test: 💍 update more test case

* test: 💍 update test case

* test: 💍 update error test case

* test: 💍 update test case

* style: 💄 clippy and fmt

* test: 💍 add extra test case

* chore: 🤖 conflict resolve

* test: 💍 update snapshot

* chore: 🤖 update test result

* test: 💍 update snapshot

* chore: 🤖 update test parser

* style: 💄 fmt

* test: 💍 update format test snapshot

* feat: update parse logic

* feat: separate parse_ts_type_parameters and parse_ts_type_parameters_with_modifiers

* fix: update test case

* feat: update parser

* fix: update tests

* feat: refactor allow_in_out_modifier arguments

* feat: revert unnecessary diff

---------

Co-authored-by: IWANABETHATGUY <iwanabethatguy@qq.com>
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.

📎 TypeScript 4.7
4 participants