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

Refactor sortByLoc to use @glimmer/syntax' sortByLoc function #662

Closed
wants to merge 2 commits into from

Conversation

dcyriller
Copy link
Member

@dcyriller dcyriller commented Oct 28, 2021

Description

Starting from v0.76.0, a sortByLoc function is exposed in @glimmer/syntax. An idea is to use it here in template-recast to replace a very similar function (see glimmerjs/glimmer-vm#1266 (comment)).

This PR is a possible implementation. But IMO an other implementation should be done. Indeed, this PR would add another dependency and would cause a slow down.

See commit messages for more details.

Links

Previously a node was cloned with `JSON.parse(JSON.stringify(node))`.

This approach needs to be sophisticated because the node.loc property
has been changed from a plain JSON object to a `SourceSpan` instance in
in v0.66.0 of @glimmer/syntax. (See PR 1170 in
https://github.com/glimmerjs/glimmer-vm if you want to take a look at
the changes in @glimmer/syntax.)

node.loc in v0.65.4 has the following interface:
```
export interface SourceLocation {
  source?: Option<string>;
  start: Position;
  end: Position;
}
```
reference: https://github.com/glimmerjs/glimmer-vm/blob/v0.65.4/packages/%40glimmer/syntax/lib/types/nodes.ts#L36-L40

in v0.80.0, node.loc is an instance of SourceSpan, which is defined
here:
https://github.com/glimmerjs/glimmer-vm/blob/v0.80.0/packages/%40glimmer/syntax/lib/source/loc/span.ts#L106-L309
@dcyriller dcyriller marked this pull request as draft October 28, 2021 17:43
@dcyriller dcyriller changed the title Draft: Refactor sortByLoc to use @glimmer/syntax' sortByLoc function Refactor sortByLoc to use @glimmer/syntax' sortByLoc function Oct 28, 2021
@@ -1,5 +1,6 @@
import { preprocess, builders, print as _print, traverse, ASTv1 as AST } from '@glimmer/syntax';
import { getLines, sortByLoc, sourceForLoc } from './utils';
import { klona } from 'klona/full';
Copy link
Member

Choose a reason for hiding this comment

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

How is this related to the changes to move to use sortByLoc from @glimmer/syntax?

Also, FWIW, the sortByLoc that landed in @glimmer/syntax was originally extracted from us 😄

Copy link
Member

Choose a reason for hiding this comment

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

Also, wouldn't we want to use klona/json? Or do we need other structures now?

Copy link
Member Author

Choose a reason for hiding this comment

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

The sortByLoc function expects the node.loc property to be an instance of the SourceSpan class. But JSON.parse(JSON.stringify()) and klona/json can only clone JSON data types. When cloned with JSON.parse(JSON.stringify()), the data structure is simplified (to a SourceLocation?). So sortByLoc fails on startPosition being undefined.

Copy link
Member Author

Choose a reason for hiding this comment

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

I am closing the PR as I don't have a good solution for now.

@dcyriller dcyriller closed this Nov 2, 2021
@dcyriller dcyriller deleted the sortby branch November 2, 2021 23:51
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.

2 participants