Skip to content

Commit ebde0d5

Browse files
authored
feat: add TokenStore#get{Loc,Range}() (#253)
1 parent 7b00625 commit ebde0d5

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/external/token-store/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,4 +751,24 @@ export default class TokenStore {
751751
filter: isCommentToken,
752752
})
753753
}
754+
755+
/**
756+
* Returns the location of the given node or token.
757+
* @param nodeOrToken The node or token to get the location of.
758+
* @returns The location of the node or token.
759+
*/
760+
// eslint-disable-next-line class-methods-use-this
761+
public getLoc(nodeOrToken: HasLocation) {
762+
return nodeOrToken.loc
763+
}
764+
765+
/**
766+
* Returns the range of the given node or token.
767+
* @param nodeOrToken The node or token to get the range of.
768+
* @returns The range of the node or token.
769+
*/
770+
// eslint-disable-next-line class-methods-use-this
771+
public getRange(nodeOrToken: HasLocation) {
772+
return nodeOrToken.range
773+
}
754774
}

test/tokens.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,19 @@ describe("services.getTemplateBodyTokenStore", () => {
285285
assert.deepStrictEqual(actual, ["div", ">"])
286286
})
287287
})
288+
289+
describe("TokenStore#get{Range,Loc}()", () => {
290+
it("should return loc and range.", () => {
291+
const {
292+
templateBody: {
293+
children: [node],
294+
tokens: [token],
295+
},
296+
} = ast
297+
assert.equal(typeof tokens.getRange(node)[0], "number")
298+
assert.equal(typeof tokens.getRange(token)[1], "number")
299+
assert.equal(typeof tokens.getLoc(node).start.line, "number")
300+
assert.equal(typeof tokens.getLoc(node).end.column, "number")
301+
})
302+
})
288303
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy