Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve parse for module sources #177

Merged
merged 1 commit into from
Aug 7, 2019

Conversation

rbuckton
Copy link
Member

@rbuckton rbuckton commented Aug 3, 2019

This changes the parsing behavior of DeclarationReference.parse when parsing module sources and components:

  • @ is not allowed in an un-quoted symbol component (i.e. @a is not valid, but "@a" is).
  • @microsoft/rush-stack-compiler-3.5 will throw, as @ and 5 are not valid identifiers and there is no trailing ! token.
    • NOTE: While we could detect the leading @ and treat it as a path, I believe it is better to be unambiguous. An @ is only allowed if we scan ahead and find a ! token.
  • @microsoft/rush-stack-compiler-3.5! will parse with a module source of @microsoft/rush-stack-compiler-3.5 as we will scan ahead and find the ! token.
  • If for some reason a module source includes a ! token in its text, the module source must be quoted (i.e. a!b!c is an error, but "a!b"!c is valid, with "a!b" as the module source).

Also there are a few additional changes to make the API easier to use:

  • DeclarationReference.isWellFormedModuleSourceString(text) - Returns whether the text is a valid module source.
  • DeclarationReference.escapeModuleSourceString(text) - Surrounds a module source in quotemarks if it is not well-formed.
  • DeclarationReference.unescapeModuleSourceString(text) - Unquotes a module source if necessary.
  • ModuleSource#escapedPath - Preserves the module source as written (including quotemarks). This was previously the value for ModuleSource#path
  • ModuleSource#path - The unescaped module source.
  • ModuleSource#scopeName - The scope name for the module, if it has one. Includes the leading @.
  • ModuleSource#unscopedPackageName - The package name excluding the scope name.
  • ModuleSource.fromScopedPacage(scopeName, unscopedPackageName, importPath) - Create a module source from a scope.

I've also added several suites of tests for various behaviors.

Fixes #174

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
@rbuckton rbuckton force-pushed the declarationReferenceUpdate3 branch from 2c5a969 to c4bedf9 Compare August 3, 2019 00:48
@rbuckton
Copy link
Member Author

rbuckton commented Aug 6, 2019

@octogonz do you have some time to take a look?

@octogonz
Copy link
Collaborator

octogonz commented Aug 6, 2019

I looked over the code this weekend and it seems good, but I wanted to test it with my PR branch before we merge it. I should have time in the next few days. I've been super busy with another project this past week.

Copy link
Collaborator

@octogonz octogonz left a comment

Choose a reason for hiding this comment

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

Thanks a ton for fixing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DeclarationReference: Parser does not handle certain inputs correctly
3 participants