Skip to content

Commit

Permalink
Deep clone the nodes with klona
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dcyriller committed Oct 27, 2021
1 parent 650d44e commit 2da9e09
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"colors": "^1.4.0",
"commander": "^6.2.1",
"globby": "^11.0.3",
"klona": "^2.0.5",
"ora": "^5.4.0",
"slash": "^3.0.0",
"tmp": "^0.2.1",
Expand Down
3 changes: 2 additions & 1 deletion src/parse-result.ts
Original file line number Diff line number Diff line change
@@ -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';

const leadingWhitespace = /(^\s+)/;
const attrNodeParts = /(^[^=]+)(\s+)?(=)?(\s+)?(['"])?(\S+)?/;
Expand Down Expand Up @@ -132,7 +133,7 @@ export default class ParseResult {

const nodeInfo = {
node,
original: JSON.parse(JSON.stringify(node)),
original: klona(node),
source: this.sourceForLoc(node.loc),
};

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4280,6 +4280,11 @@ kleur@^3.0.3:
resolved "https://registry.yarnpkg.com/kleur/-/kleur-3.0.3.tgz#a79c9ecc86ee1ce3fa6206d1216c501f147fc07e"
integrity sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==

klona@^2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/klona/-/klona-2.0.5.tgz#d166574d90076395d9963aa7a928fabb8d76afbc"
integrity sha512-pJiBpiXMbt7dkzXe8Ghj/u4FfXOOa98fPW+bihOJ4SjnoijweJrNThJfd3ifXpXhREjpoF2mZVH1GfS9LV3kHQ==

latest-version@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/latest-version/-/latest-version-5.1.0.tgz#119dfe908fe38d15dfa43ecd13fa12ec8832face"
Expand Down

0 comments on commit 2da9e09

Please sign in to comment.