From f9de578125a029e3aac08d4fc94bdbde4789a0d6 Mon Sep 17 00:00:00 2001 From: Cyrille David Date: Wed, 27 Oct 2021 19:00:48 +0200 Subject: [PATCH] Use @glimmer/syntax' sortByLoc Added in @glimmer/syntax v0.76.0: https://github.com/glimmerjs/glimmer-vm/pull/1266 --- src/utils.ts | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 952bea29..55522023 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,7 @@ const reLines = /(.*?(?:\r\n?|\n|$))/gm; import type { ASTv1 as AST } from '@glimmer/syntax'; +import { sortByLoc as glimmerSortByLoc } from '@glimmer/syntax'; export function sourceForLoc(source: string | string[], loc?: AST.SourceLocation): string { if (!loc) { @@ -52,24 +53,15 @@ export function isSyntheticWithNoLocation(node: AST.Node): boolean { } export function sortByLoc(a: AST.Node, b: AST.Node): -1 | 0 | 1 { - // be conservative about the location where a new node is inserted + // the sortByLoc function in @glimmmer/syntax + // sorts the synthetic nodes that don't have any location + // at the beginning of the range + // in ember-template-recast, we preserve the index at which they were inserted if (isSyntheticWithNoLocation(a) || isSyntheticWithNoLocation(b)) { return 0; } - if (a.loc.start.line < b.loc.start.line) { - return -1; - } - - if (a.loc.start.line === b.loc.start.line && a.loc.start.column < b.loc.start.column) { - return -1; - } - - if (a.loc.start.line === b.loc.start.line && a.loc.start.column === b.loc.start.column) { - return 0; - } - - return 1; + return glimmerSortByLoc(a, b); } export function compact(array: unknown[]): unknown[] {