Skip to content

Commit 3f1f066

Browse files
Add AstUtils.getIdentifierLiteral (#392)
* initial commit * few more tests * updating package version
1 parent 4798fda commit 3f1f066

File tree

10 files changed

+81
-8
lines changed

10 files changed

+81
-8
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@microsoft/powerquery-parser",
3-
"version": "0.16.0",
3+
"version": "0.16.1",
44
"description": "A parser for the Power Query/M formula language.",
55
"author": "Microsoft",
66
"license": "MIT",

src/powerquery-parser/language/ast/astUtils/astUtils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ export function assertIsTNullablePrimitiveType(node: Ast.TNode): asserts node is
3030
}
3131
}
3232

33+
export function getIdentifierLiteral(node: Ast.Identifier | Ast.IdentifierExpression): string {
34+
switch (node.kind) {
35+
case Ast.NodeKind.Identifier:
36+
return node.literal;
37+
38+
case Ast.NodeKind.IdentifierExpression:
39+
return node.inclusiveConstant ? `@${node.identifier.literal}` : node.identifier.literal;
40+
41+
default:
42+
throw Assert.isNever(node);
43+
}
44+
}
45+
3346
export function isTAnyLiteral(node: Ast.TNode): node is Ast.TAnyLiteral {
3447
return Ast.NodeKindsForTAnyLiteral.has(node.kind);
3548
}

src/test/libraryTest/identifierUtils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { expect } from "chai";
66

77
import { IdentifierUtils } from "../../powerquery-parser/language";
88

9-
describe("TextUtils", () => {
9+
describe("IdentifierUtils", () => {
1010
describe(`isRegularIdentifier`, () => {
1111
describe(`valid`, () => {
1212
it(`foo`, () => expect(IdentifierUtils.isRegularIdentifier("foo", false), "should be true").to.be.true);
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
import "mocha";
5+
import { expect } from "chai";
6+
7+
import { Ast, AstUtils } from "../../../powerquery-parser/language";
8+
import { AssertTestUtils } from "../../testUtils";
9+
import { DefaultSettings } from "../../../powerquery-parser";
10+
import { ParseOk } from "../../../powerquery-parser/parser";
11+
12+
describe(`AstUtils`, () => {
13+
describe(`getIdentifierLiteral`, () => {
14+
async function runTest(params: {
15+
readonly text: string;
16+
readonly expectedIdentifierLiteral: string;
17+
readonly expectedIdentifierExpressionLiteral: string;
18+
}): Promise<void> {
19+
const parseOk: ParseOk = await AssertTestUtils.assertGetParseOk(DefaultSettings, params.text);
20+
21+
// First, ensure the root node is an IdentifierExpression.
22+
const rootAsIdentifierExpression: Ast.IdentifierExpression =
23+
AstUtils.assertAsNodeKind<Ast.IdentifierExpression>(parseOk.root, Ast.NodeKind.IdentifierExpression);
24+
25+
// Second, grab our actual literals.
26+
const actualIdentifierExpressionLiteral: string = AstUtils.getIdentifierLiteral(rootAsIdentifierExpression);
27+
28+
const actualIdentifierLiteral: string = AstUtils.getIdentifierLiteral(
29+
rootAsIdentifierExpression.identifier,
30+
);
31+
32+
expect(
33+
actualIdentifierExpressionLiteral,
34+
`expected identifier expression literal to be ${params.expectedIdentifierExpressionLiteral}`,
35+
).to.equal(params.expectedIdentifierExpressionLiteral);
36+
37+
// Finally, assert that our actual literals match the expected literals.
38+
expect(
39+
actualIdentifierLiteral,
40+
`expected identifier literal to be ${params.expectedIdentifierLiteral}`,
41+
).to.equal(params.expectedIdentifierLiteral);
42+
}
43+
44+
it(`foo`, async () => {
45+
await runTest({
46+
text: "foo",
47+
expectedIdentifierLiteral: "foo",
48+
expectedIdentifierExpressionLiteral: "foo",
49+
});
50+
});
51+
52+
it(`@foo`, async () => {
53+
await runTest({
54+
text: "@foo",
55+
expectedIdentifierExpressionLiteral: "@foo",
56+
expectedIdentifierLiteral: "foo",
57+
});
58+
});
59+
});
60+
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/test/libraryTest/parser/parseNodeIdMapUtils.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ describe(`nodeIdMapUtils`, () => {
257257
);
258258

259259
expect(invokeExpressionNodeIds.size).to.equal(1);
260-
const invokeExpressionNodeId: number = invokeExpressionNodeIds.values().next().value;
260+
const invokeExpressionNodeId: number = Assert.asDefined(invokeExpressionNodeIds.values().next().value);
261261

262262
const invokeExpressionIdentifier: XorNode<Ast.IdentifierExpression> = Assert.asDefined(
263263
NodeIdMapUtils.invokeExpressionIdentifier(nodeIdMapCollection, invokeExpressionNodeId),
@@ -282,7 +282,7 @@ describe(`nodeIdMapUtils`, () => {
282282
);
283283

284284
expect(invokeExpressionNodeIds.size).to.equal(1);
285-
const invokeExpressionNodeId: number = invokeExpressionNodeIds.values().next().value;
285+
const invokeExpressionNodeId: number = Assert.asDefined(invokeExpressionNodeIds.values().next().value);
286286

287287
const invokeExpressionIdentifier: XorNode<Ast.IdentifierExpression> = Assert.asDefined(
288288
NodeIdMapUtils.invokeExpressionIdentifier(nodeIdMapCollection, invokeExpressionNodeId),
@@ -304,7 +304,7 @@ describe(`nodeIdMapUtils`, () => {
304304
);
305305

306306
expect(recordExpressionNodeIds.size).to.equal(1);
307-
const recordExpressionNodeId: number = recordExpressionNodeIds.values().next().value;
307+
const recordExpressionNodeId: number = Assert.asDefined(recordExpressionNodeIds.values().next().value);
308308

309309
XorNodeUtils.assertAstChecked(
310310
Assert.asDefined(NodeIdMapUtils.wrappedContentXor(nodeIdMapCollection, recordExpressionNodeId)),
@@ -327,7 +327,7 @@ describe(`nodeIdMapUtils`, () => {
327327
);
328328

329329
expect(recordExpressionNodeIds.size).to.equal(1);
330-
const recordExpressionNodeId: number = recordExpressionNodeIds.values().next().value;
330+
const recordExpressionNodeId: number = Assert.asDefined(recordExpressionNodeIds.values().next().value);
331331

332332
XorNodeUtils.assertAstChecked(
333333
Assert.asDefined(NodeIdMapUtils.wrappedContentXor(nodeIdMapCollection, recordExpressionNodeId)),

0 commit comments

Comments
 (0)