Skip to content
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft/powerquery-parser",
"version": "0.18.0",
"version": "0.18.1",
"description": "A parser for the Power Query/M formula language.",
"author": "Microsoft",
"license": "MIT",
Expand Down
25 changes: 25 additions & 0 deletions src/powerquery-parser/language/identifierExpressionUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { CommonIdentifierUtilsOptions, getNormalizedIdentifier } from "./identifierUtils";
import { Assert } from "../common";

export function assertNormalizedIdentifierExpression(text: string, options?: CommonIdentifierUtilsOptions): string {
return Assert.asDefined(
getNormalizedIdentifierExpression(text, options),
`Expected a valid identifier expression but received '${text}'`,
);
}

// Removes the '@' and quotes from a quoted identifier if possible.
// When given an invalid identifier, returns undefined.
export function getNormalizedIdentifierExpression(
text: string,
options?: CommonIdentifierUtilsOptions,
): string | undefined {
if (text.startsWith("@")) {
text = text.substring(1);
}

return getNormalizedIdentifier(text, options);
}
27 changes: 27 additions & 0 deletions src/powerquery-parser/language/identifierUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

import { Assert, Pattern, StringUtils } from "../common";
import { KeywordKind } from "./keyword/keyword";

export enum IdentifierKind {
Generalized = "Generalized",
Expand All @@ -21,6 +22,14 @@ export interface GetAllowedIdentifiersOptions extends CommonIdentifierUtilsOptio
readonly allowRecursive?: boolean;
}

// Wraps an assert around the getNormalizedIdentifier method
export function assertNormalizedIdentifier(text: string, options?: CommonIdentifierUtilsOptions): string {
return Assert.asDefined(
getNormalizedIdentifier(text, options),
`Expected a valid identifier but received '${text}'`,
);
}

// Identifiers have multiple forms that can be used interchangeably.
// For example, if you have `[key = 1]`, you can use `key` or `#""key""`.
// The `getAllowedIdentifiers` function returns all the forms of the identifier that are allowed in the current context.
Expand Down Expand Up @@ -180,6 +189,10 @@ export function getIdentifierLength(
// Removes the quotes from a quoted identifier if possible.
// When given an invalid identifier, returns undefined.
export function getNormalizedIdentifier(text: string, options?: CommonIdentifierUtilsOptions): string | undefined {
if (AllowedHashKeywords.has(text)) {
return text;
}

const allowGeneralizedIdentifier: boolean =
options?.allowGeneralizedIdentifier ?? DefaultAllowGeneralizedIdentifier;

Expand Down Expand Up @@ -367,3 +380,17 @@ function stripQuotes(text: string): string {

const DefaultAllowTrailingPeriod: boolean = false;
const DefaultAllowGeneralizedIdentifier: boolean = false;

const AllowedHashKeywords: ReadonlySet<string> = new Set([
KeywordKind.HashBinary,
KeywordKind.HashDate,
KeywordKind.HashDateTime,
KeywordKind.HashDateTimeZone,
KeywordKind.HashDuration,
KeywordKind.HashInfinity,
KeywordKind.HashNan,
KeywordKind.HashSections,
KeywordKind.HashShared,
KeywordKind.HashTable,
KeywordKind.HashTime,
]);
3 changes: 2 additions & 1 deletion src/powerquery-parser/language/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import * as Comment from "./comment";
export * as IdentifierExpressionUtils from "./identifierExpressionUtils";
export * as IdentifierUtils from "./identifierUtils";
export * as TextUtils from "./textUtils";
import * as Comment from "./comment";
import * as Token from "./token";

export { Comment, Token };
Expand Down
64 changes: 64 additions & 0 deletions src/test/libraryTest/identifierExpressionUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import "mocha";
import { expect } from "chai";

import { IdentifierExpressionUtils, IdentifierUtils } from "../../powerquery-parser/language";

describe("IdentifierUtils", () => {
function createCommonIdentifierUtilsOptions(
overrides?: Partial<IdentifierUtils.CommonIdentifierUtilsOptions>,
): IdentifierUtils.CommonIdentifierUtilsOptions {
return {
allowTrailingPeriod: false,
allowGeneralizedIdentifier: false,
...overrides,
};
}

describe(`getNormalizedIdentifierExpression`, () => {
function runGetNormalizedIdentifierExpressionTest(params: {
readonly text: string;
readonly expectedSuccess: string | undefined;
readonly options?: Partial<IdentifierUtils.CommonIdentifierUtilsOptions>;
}): void {
const text: string = params.text;

const identifierUtilsOptions: IdentifierUtils.CommonIdentifierUtilsOptions =
createCommonIdentifierUtilsOptions(params.options);

const actual: string | undefined = IdentifierExpressionUtils.getNormalizedIdentifierExpression(
text,
identifierUtilsOptions,
);

if (params.expectedSuccess !== undefined) {
expect(actual).to.equal(params.expectedSuccess);
} else {
expect(actual).to.be.undefined;
}
}

it("foo", () => {
runGetNormalizedIdentifierExpressionTest({
text: "foo",
expectedSuccess: "foo",
});
});

it("@foo", () => {
runGetNormalizedIdentifierExpressionTest({
text: "@foo",
expectedSuccess: "foo",
});
});

it("#table", () => {
runGetNormalizedIdentifierExpressionTest({
text: "#table",
expectedSuccess: "#table",
});
});
});
});
7 changes: 7 additions & 0 deletions src/test/libraryTest/identifierUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,5 +368,12 @@ describe("IdentifierUtils", () => {
expectedSuccess: "quoted generalized identifier",
});
});

it("#table", () => {
runGetNormalizedIdentifierTest({
text: "#table",
expectedSuccess: "#table",
});
});
});
});
Loading