Skip to content

Commit

Permalink
Update code style (#341)
Browse files Browse the repository at this point in the history
* initial commit

* more changes

* more changes

* purged last maybe

* removing Fn suffix

* updating package
  • Loading branch information
JordanBoltonMN authored Sep 2, 2022
1 parent a5d3340 commit 01701b2
Show file tree
Hide file tree
Showing 72 changed files with 1,867 additions and 2,235 deletions.
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.9.8",
"version": "0.10.0",
"description": "A parser for the Power Query/M formula language.",
"author": "Microsoft",
"license": "MIT",
Expand Down
10 changes: 4 additions & 6 deletions src/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function lexText(text: string): void {

// An error state is returned if the argument can't be lexed.
// Use either the typeguard Lexer.isErrorState to discover if it's an error state,
// or Lexer.maybeErrorLineMap to quickly get all lines with errors.
// or Lexer.errorLineMap to quickly get all lines with errors.
// Note: At this point all errors are isolated to a single line.
// Checks for multiline errors, such as an unterminated string, have not been processed.
let triedLex: Lexer.TriedLex = Lexer.tryLex(DefaultSettings, text);
Expand All @@ -58,12 +58,10 @@ function lexText(text: string): void {

// The lexer state might have an error.
// To be sure either use the typeguard Lexer.isErrorState,
// or Lexer.maybeErrorLineMap to get an option containing a map of all lines with errors.
const maybeErrorLineMap: Lexer.ErrorLineMap | undefined = Lexer.maybeErrorLineMap(lexerState);

if (maybeErrorLineMap !== undefined) {
const errorLineMap: Lexer.ErrorLineMap = maybeErrorLineMap;
// or Lexer.errorLineMap to get an option containing a map of all lines with errors.
const errorLineMap: Lexer.ErrorLineMap | undefined = Lexer.errorLineMap(lexerState);

if (errorLineMap !== undefined) {
for (const [lineNumber, errorLine] of errorLineMap.entries()) {
console.log(`lineNumber ${lineNumber} has the following error: ${errorLine.error.message}`);
}
Expand Down
66 changes: 26 additions & 40 deletions src/powerquery-parser/common/arrayUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,55 @@ import { Assert } from ".";

export function all<T>(
collection: ReadonlyArray<T>,
predicateFn: (value: T) => boolean = (value: T): boolean => Boolean(value),
predicate: (value: T) => boolean = (value: T): boolean => Boolean(value),
): boolean {
for (const element of collection) {
if (!predicateFn(element)) {
if (!predicate(element)) {
return false;
}
}

return true;
}

export function assertIn<T>(
collection: ReadonlyArray<T>,
item: T,
maybeMessage?: string,
maybeDetails?: object,
): number {
export function assertIn<T>(collection: ReadonlyArray<T>, item: T, message?: string, details?: object): number {
const index: number = collection.indexOf(item);
Assert.isTrue(index !== -1, maybeMessage, maybeDetails ?? { item });
Assert.isTrue(index !== -1, message, details ?? { item });

return index;
}

export function assertGet<T>(
collection: ReadonlyArray<T>,
index: number,
maybeMessage?: string,
maybeDetails?: object,
): T {
return Assert.asDefined(collection[index], maybeMessage, maybeDetails);
export function assertGet<T>(collection: ReadonlyArray<T>, index: number, message?: string, details?: object): T {
return Assert.asDefined(collection[index], message, details);
}

export function assertIndexOfPredicate<T>(
collection: ReadonlyArray<T>,
predicateFn: (element: T) => boolean,
maybeMessage?: string,
maybeDetails?: object,
predicate: (element: T) => boolean,
message?: string,
details?: object,
): number {
const index: number = indexOfPredicate(collection, predicateFn);
Assert.isTrue(index !== -1, maybeMessage, maybeDetails);
const index: number = indexOfPredicate(collection, predicate);
Assert.isTrue(index !== -1, message, details);

return index;
}

export function assertNonZeroLength<T>(
collection: ReadonlyArray<T>,
maybeMessage?: string,
maybeDetails?: object,
): void {
export function assertNonZeroLength<T>(collection: ReadonlyArray<T>, message?: string, details?: object): void {
Assert.isTrue(
collection.length > 0,
maybeMessage ?? `collection should have at least one element in it`,
maybeDetails ?? {
message ?? `collection should have at least one element in it`,
details ?? {
collectionLength: collection.length,
},
);
}

export async function mapAsync<T, U>(
collection: ReadonlyArray<T>,
mapFn: (value: T) => Promise<U>,
map: (value: T) => Promise<U>,
): Promise<ReadonlyArray<U>> {
const tasks: ReadonlyArray<Promise<U>> = collection.map(mapFn);
const tasks: ReadonlyArray<Promise<U>> = collection.map(map);

return await Promise.all(tasks);
}
Expand Down Expand Up @@ -102,11 +88,11 @@ export function findReverse<T>(collection: ReadonlyArray<T>, predicate: (t: T) =
return undefined;
}

export function includesPredicate<T>(collection: ReadonlyArray<T>, predicateFn: (element: T) => boolean): boolean {
export function includesPredicate<T>(collection: ReadonlyArray<T>, predicate: (element: T) => boolean): boolean {
const numElements: number = collection.length;

for (let index: number = 0; index < numElements; index += 1) {
if (predicateFn(collection[index])) {
if (predicate(collection[index])) {
return true;
}
}
Expand All @@ -117,16 +103,16 @@ export function includesPredicate<T>(collection: ReadonlyArray<T>, predicateFn:
export function includesUnique<T>(
collection: ReadonlyArray<T>,
testValue: T,
equalityFn: (left: T, right: T) => boolean,
comparer: (left: T, right: T) => boolean,
): boolean {
return includesPredicate(collection, (collectionItem: T) => equalityFn(testValue, collectionItem));
return includesPredicate(collection, (collectionItem: T) => comparer(testValue, collectionItem));
}

export function indexOfPredicate<T>(collection: ReadonlyArray<T>, predicateFn: (element: T) => boolean): number {
export function indexOfPredicate<T>(collection: ReadonlyArray<T>, predicate: (element: T) => boolean): number {
const numElements: number = collection.length;

for (let index: number = 0; index < numElements; index += 1) {
if (predicateFn(collection[index])) {
if (predicate(collection[index])) {
return index;
}
}
Expand All @@ -137,7 +123,7 @@ export function indexOfPredicate<T>(collection: ReadonlyArray<T>, predicateFn: (
export function isSubset<T>(
largerCollection: ReadonlyArray<T>,
smallerCollection: ReadonlyArray<T>,
equalityFn: (left: T, right: T) => boolean = (left: T, right: T): boolean => left === right,
comparer: (left: T, right: T) => boolean = (left: T, right: T): boolean => left === right,
): boolean {
if (smallerCollection.length > largerCollection.length) {
return false;
Expand All @@ -147,7 +133,7 @@ export function isSubset<T>(
let foundMatch: boolean = false;

for (const largerCollectionValue of largerCollection) {
if (equalityFn(smallerCollectionValue, largerCollectionValue)) {
if (comparer(smallerCollectionValue, largerCollectionValue)) {
foundMatch = true;
break;
}
Expand Down Expand Up @@ -194,13 +180,13 @@ export function removeAtIndex<T>(collection: ReadonlyArray<T>, index: number): T

export function split<T>(
collection: ReadonlyArray<T>,
splitFn: (value: T) => boolean,
splitter: (value: T) => boolean,
): [ReadonlyArray<T>, ReadonlyArray<T>] {
const left: T[] = [];
const right: T[] = [];

for (const value of collection) {
if (splitFn(value) === true) {
if (splitter(value) === true) {
left.push(value);
} else {
right.push(value);
Expand Down
42 changes: 16 additions & 26 deletions src/powerquery-parser/common/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import { ErrorResult, OkResult, Result, ResultUtils } from "./result";
import { CommonError } from ".";

export function asDefined<T>(maybeValue: T | undefined, maybeMessage?: string, maybeDetails?: object): NonNullable<T> {
isDefined(maybeValue, maybeMessage, maybeDetails);
export function asDefined<T>(value: T | undefined, message?: string, details?: object): NonNullable<T> {
isDefined(value, message, details);

return maybeValue;
return value;
}

export function asInstanceofError<T>(value: T): Error {
Expand All @@ -16,15 +16,15 @@ export function asInstanceofError<T>(value: T): Error {
return value;
}

export function isTrue(value: boolean, maybeMessage?: string, maybeDetails?: object): asserts value is true {
export function isTrue(value: boolean, message?: string, details?: object): asserts value is true {
if (value !== true) {
throw new CommonError.InvariantError(maybeMessage ?? `assert failed, expected value to be true`, maybeDetails);
throw new CommonError.InvariantError(message ?? `assert failed, expected value to be true`, details);
}
}

export function isFalse(value: boolean, maybeMessage?: string, maybeDetails?: object): asserts value is false {
export function isFalse(value: boolean, message?: string, details?: object): asserts value is false {
if (value !== false) {
throw new CommonError.InvariantError(maybeMessage ?? `assert failed, expected value to be false`, maybeDetails);
throw new CommonError.InvariantError(message ?? `assert failed, expected value to be false`, details);
}
}

Expand All @@ -42,28 +42,18 @@ export function isInstanceofError<T>(value: T | Error): asserts value is Error {
}

export function isDefined<T>(
maybeValue: T | undefined,
maybeMessage?: string,
maybeDetails?: object,
): asserts maybeValue is NonNullable<T> {
if (maybeValue === undefined) {
throw new CommonError.InvariantError(
maybeMessage ?? `assert failed, expected value to be defined`,
maybeDetails,
);
value: T | undefined,
message?: string,
details?: object,
): asserts value is NonNullable<T> {
if (value === undefined) {
throw new CommonError.InvariantError(message ?? `assert failed, expected value to be defined`, details);
}
}

export function isUndefined<T>(
maybeValue: T | undefined,
maybeMessage?: string,
maybeDetails?: object,
): asserts maybeValue is undefined {
if (maybeValue !== undefined) {
throw new CommonError.InvariantError(
maybeMessage ?? `assert failed, expected value to be undefined`,
maybeDetails,
);
export function isUndefined<T>(value: T | undefined, message?: string, details?: object): asserts value is undefined {
if (value !== undefined) {
throw new CommonError.InvariantError(message ?? `assert failed, expected value to be undefined`, details);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/powerquery-parser/common/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export class CancellationError extends Error {
}

export class InvariantError extends Error {
constructor(readonly invariantBroken: string, readonly maybeDetails?: object) {
super(Localization.error_common_invariantError(Templates.DefaultTemplates, invariantBroken, maybeDetails));
constructor(readonly invariantBroken: string, readonly details?: object) {
super(Localization.error_common_invariantError(Templates.DefaultTemplates, invariantBroken, details));
Object.setPrototypeOf(this, InvariantError.prototype);
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/powerquery-parser/common/immutableSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class ImmutableSet<T> {

public constructor(
iterable: Iterable<T> = [],
private readonly equalityFn: (left: T, right: T) => boolean = (left: T, right: T): boolean => left === right,
private readonly comparer: (left: T, right: T) => boolean = (left: T, right: T): boolean => left === right,
) {
this.internalCollection = [...iterable];
this.size = this.internalCollection.length;
Expand All @@ -20,7 +20,7 @@ export class ImmutableSet<T> {
if (this.has(value)) {
return this;
} else {
return new ImmutableSet(new Set([...this.values(), value]), this.equalityFn);
return new ImmutableSet(new Set([...this.values(), value]), this.comparer);
}
}

Expand All @@ -41,18 +41,18 @@ export class ImmutableSet<T> {

public delete(value: T): ImmutableSet<T> {
const values: ReadonlyArray<T> = [...this.internalCollection.values()].filter(
(item: T) => !this.equalityFn(item, value),
(item: T) => !this.comparer(item, value),
);

if (values.length === this.internalCollection.length) {
return this;
} else {
return new ImmutableSet(new Set(values), this.equalityFn);
return new ImmutableSet(new Set(values), this.comparer);
}
}

public has(value: T): boolean {
return ArrayUtils.includesUnique(this.internalCollection, value, this.equalityFn);
return ArrayUtils.includesUnique(this.internalCollection, value, this.comparer);
}

public values(): IterableIterator<T> {
Expand Down
34 changes: 14 additions & 20 deletions src/powerquery-parser/common/mapUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,31 @@

import { Assert } from ".";

export function assertDelete<K, V>(map: Map<K, V>, key: K, maybeMessage?: string, maybeDetails?: object): void {
Assert.isTrue(map.delete(key), maybeMessage ?? `failed to delete, key is absent`, maybeDetails ?? { key });
export function assertDelete<K, V>(map: Map<K, V>, key: K, message?: string, details?: object): void {
Assert.isTrue(map.delete(key), message ?? `failed to delete, key is absent`, details ?? { key });
}

export function assertGet<K, V>(map: Map<K, V>, key: K, maybeMessage?: string, maybeDetails?: object): V {
return Assert.asDefined(map.get(key), maybeMessage ?? `key not found in given map`, maybeDetails ?? { key });
export function assertGet<K, V>(map: Map<K, V>, key: K, message?: string, details?: object): V {
return Assert.asDefined(map.get(key), message ?? `key not found in given map`, details ?? { key });
}

export function assertIn<K, V>(map: Map<K, V>, key: K, maybeMessage?: string): void {
Assert.isTrue(map.has(key), maybeMessage ?? `key is absent`, { key });
export function assertIn<K, V>(map: Map<K, V>, key: K, message?: string): void {
Assert.isTrue(map.has(key), message ?? `key is absent`, { key });
}

export function assertNotIn<K, V>(map: Map<K, V>, key: K, maybeMessage?: string): void {
Assert.isFalse(map.has(key), maybeMessage ?? `key is present`, { key });
export function assertNotIn<K, V>(map: Map<K, V>, key: K, message?: string): void {
Assert.isFalse(map.has(key), message ?? `key is present`, { key });
}

export function isEqualMap<K, V>(
left: Map<K, V>,
right: Map<K, V>,
valueCmpFn: (left: V, right: V) => boolean,
): boolean {
export function isEqualMap<K, V>(left: Map<K, V>, right: Map<K, V>, comparer: (left: V, right: V) => boolean): boolean {
if (left.size !== right.size) {
return false;
}

for (const [leftKey, leftValue] of left.entries()) {
const maybeRightValue: V | undefined = right.get(leftKey);
const value: V | undefined = right.get(leftKey);

if (maybeRightValue === undefined) {
return false;
} else if (valueCmpFn(leftValue, maybeRightValue) === false) {
if (value === undefined || !comparer(leftValue, value)) {
return false;
}
}
Expand All @@ -44,16 +38,16 @@ export function isEqualMap<K, V>(
export function isSubsetMap<K, V>(
left: Map<K, V>,
right: Map<K, V>,
valueCmpFn: (left: V, right: V) => boolean,
comparer: (left: V, right: V) => boolean,
): boolean {
if (left.size > right.size) {
return false;
}

for (const [key, leftType] of left.entries()) {
const maybeRightType: V | undefined = right.get(key);
const rightType: V | undefined = right.get(key);

if (maybeRightType === undefined || !valueCmpFn(leftType, maybeRightType)) {
if (rightType === undefined || !comparer(leftType, rightType)) {
return false;
}
}
Expand Down
Loading

0 comments on commit 01701b2

Please sign in to comment.