Skip to content

Commit

Permalink
Fixes for TypeUtils.isCompatible (microsoft#266)
Browse files Browse the repository at this point in the history
* initial commit

* better signature for createNumberLiteral

* allows Map for OrderedMap constructor

* better error message

* added LogicalLiteral to isLiteral

* added tests, fixing issues

* simplifying primitive/literal checks

* fn rename

* some squashing of cases

* more tests

* correcting DefinedListType, still need to resolve build error

* updated typescript to solve build issue

* fixing ANyNonNull

* updating package version

* simplified logic for logical literal factory
  • Loading branch information
JordanBoltonMN authored Jul 19, 2021
1 parent 60eb91f commit 5c91196
Show file tree
Hide file tree
Showing 11 changed files with 460 additions and 204 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft/powerquery-parser",
"version": "0.4.8",
"version": "0.4.9",
"description": "A parser for the Power Query/M formula language.",
"author": "Microsoft",
"license": "MIT",
Expand Down Expand Up @@ -46,7 +46,7 @@
"tslint-config-prettier": "^1.18.0",
"tslint-microsoft-contrib": "^6.2.0",
"tslint-plugin-prettier": "^2.3.0",
"typescript": "^3.8.3"
"typescript": "^4.3.5"
},
"files": [
"lib/powerquery-parser/**/*"
Expand Down
13 changes: 9 additions & 4 deletions src/powerquery-parser/common/orderedMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,23 @@ export class OrderedMap<K, V> implements Map<K, V> {
private readonly map: Map<K, V>;
private order: ReadonlyArray<K>;

constructor(entries?: readonly (readonly [K, V])[] | null | undefined) {
constructor(entries?: readonly (readonly [K, V])[] | null | undefined | Map<K, V>) {
if (!entries) {
this.map = new Map();
this.order = [];
this.size = 0;
} else {
this.map = new Map(entries);
this.order = entries.map(pair => pair[0]);
this.size = entries.length;
if (entries instanceof Map) {
this.order = [...entries.keys()];
this.size = entries.size;
} else {
this.order = entries.map(pair => pair[0]);
this.size = entries.length;
}
}
}

public [Symbol.iterator](): IterableIterator<[K, V]> {
return this.entries();
}
Expand Down
11 changes: 5 additions & 6 deletions src/powerquery-parser/language/type/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,9 @@ export interface IExtendedType extends IType {
readonly maybeExtendedKind: ExtendedTypeKind;
}

export interface IPrimitiveLiteral extends IExtendedType {
export interface IPrimitiveLiteral<T> extends IExtendedType {
readonly literal: string;
readonly normalizedLiteral: T;
}

export interface IPrimitiveType<T extends TypeKind = TypeKind> extends IType<T> {
Expand Down Expand Up @@ -297,16 +298,14 @@ export interface ListType extends IExtendedType {
readonly itemType: TPowerQueryType;
}

export interface LogicalLiteral extends IPrimitiveLiteral {
export interface LogicalLiteral extends IPrimitiveLiteral<boolean> {
readonly kind: TypeKind.Logical;
readonly maybeExtendedKind: ExtendedTypeKind.LogicalLiteral;
readonly normalizedLiteral: boolean;
}

export interface NumberLiteral extends IPrimitiveLiteral {
export interface NumberLiteral extends IPrimitiveLiteral<number> {
readonly kind: TypeKind.Number;
readonly maybeExtendedKind: ExtendedTypeKind.NumberLiteral;
readonly normalizedLiteral: number;
}

export interface PrimaryPrimitiveType extends IExtendedType {
Expand All @@ -333,7 +332,7 @@ export interface TableTypePrimaryExpression extends IExtendedType {
readonly primaryExpression: TPowerQueryType;
}

export interface TextLiteral extends IPrimitiveLiteral {
export interface TextLiteral extends IPrimitiveLiteral<string> {
readonly kind: TypeKind.Text;
readonly maybeExtendedKind: ExtendedTypeKind.TextLiteral;
}
Expand Down
45 changes: 40 additions & 5 deletions src/powerquery-parser/language/type/typeUtils/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,26 @@ export function createFunctionType(
};
}

export function createNumberLiteral(isNullable: boolean, literal: string): Type.NumberLiteral {
export function createLogicalLiteral(isNullable: boolean, literal: string | boolean): Type.LogicalLiteral {
let parsedLiteral: string;
let normalizedLiteral: boolean;

if (literal === true || literal === "true") {
parsedLiteral = "true";
normalizedLiteral = true;
} else if (literal === false || literal === "false") {
parsedLiteral = "false";
normalizedLiteral = false;
} else {
throw new CommonError.InvariantError(`invalid boolean string`);
}

return {
isNullable,
kind: Type.TypeKind.Number,
maybeExtendedKind: Type.ExtendedTypeKind.NumberLiteral,
literal,
normalizedLiteral: Number.parseFloat(Assert.asDefined(StringUtils.maybeNormalizeNumber(literal))),
kind: Type.TypeKind.Logical,
maybeExtendedKind: Type.ExtendedTypeKind.LogicalLiteral,
literal: parsedLiteral,
normalizedLiteral,
};
}

Expand All @@ -129,6 +142,27 @@ export function createListType(isNullable: boolean, itemType: Type.TPowerQueryTy
};
}

export function createNumberLiteral(isNullable: boolean, literal: string | number): Type.NumberLiteral {
let parsedLiteral: string;
let normalizedLiteral: number;

if (typeof literal === "number") {
parsedLiteral = literal.toString();
normalizedLiteral = literal;
} else {
parsedLiteral = literal;
normalizedLiteral = Number.parseFloat(Assert.asDefined(StringUtils.maybeNormalizeNumber(literal)));
}

return {
isNullable,
kind: Type.TypeKind.Number,
maybeExtendedKind: Type.ExtendedTypeKind.NumberLiteral,
literal: parsedLiteral,
normalizedLiteral,
};
}

export function createPrimaryPrimitiveType(
isNullable: boolean,
primitiveType: Type.TPrimitiveType,
Expand Down Expand Up @@ -179,6 +213,7 @@ export function createTextLiteral(isNullable: boolean, literal: string): Type.Te
kind: Type.TypeKind.Text,
maybeExtendedKind: Type.ExtendedTypeKind.TextLiteral,
literal,
normalizedLiteral: literal,
};
}

Expand Down
Loading

0 comments on commit 5c91196

Please sign in to comment.