Skip to content

feat: add TokenStore#{getLoc,getRange}() #253

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

Merged
merged 1 commit into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/external/token-store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -751,4 +751,24 @@ export default class TokenStore {
filter: isCommentToken,
})
}

/**
* Returns the location of the given node or token.
* @param nodeOrToken The node or token to get the location of.
* @returns The location of the node or token.
*/
// eslint-disable-next-line class-methods-use-this
public getLoc(nodeOrToken: HasLocation) {
return nodeOrToken.loc
}

/**
* Returns the range of the given node or token.
* @param nodeOrToken The node or token to get the range of.
* @returns The range of the node or token.
*/
// eslint-disable-next-line class-methods-use-this
public getRange(nodeOrToken: HasLocation) {
return nodeOrToken.range
}
}
15 changes: 15 additions & 0 deletions test/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,19 @@ describe("services.getTemplateBodyTokenStore", () => {
assert.deepStrictEqual(actual, ["div", ">"])
})
})

describe("TokenStore#get{Range,Loc}()", () => {
it("should return loc and range.", () => {
const {
templateBody: {
children: [node],
tokens: [token],
},
} = ast
assert.equal(typeof tokens.getRange(node)[0], "number")
assert.equal(typeof tokens.getRange(token)[1], "number")
assert.equal(typeof tokens.getLoc(node).start.line, "number")
assert.equal(typeof tokens.getLoc(node).end.column, "number")
})
})
})
Loading