Skip to content

Commit

Permalink
Bug fix for HashMap. It used get() to determine if a key exists (whi…
Browse files Browse the repository at this point in the history
…ch returns the value instead).

Plus some clean up.
  • Loading branch information
mike-lischke committed Nov 29, 2024
1 parent d918d90 commit 6b14554
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"license": "MIT",
"scripts": {
"prepublishOnly": "npm run build-minified && npm run test",
"build": "npm run generate-parsers && tsc && npm run build-mjs && npm run build-cjs",
"build-minified": "npm run generate-parsers && tsc && npm run build-mjs-minified && npm run build-cjs-minified",
"build-bundle": "esbuild ./src/index.js --main-fields=module,main --bundle --sourcemap",
"build-mjs": "npm run build-bundle -- --outfile=dist/index.mjs --format=esm --platform=node",
Expand Down
3 changes: 1 addition & 2 deletions src/AutoIndentWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ export class AutoIndentWriter implements STWriter {
let n = 0;
// if want wrap and not already at start of line (last char was \n)
// and we have hit or exceeded the threshold
if (this.lineWidth !== STWriter.NO_WRAP && wrap !== null && !this.atStartOfLine &&
this.charPosition >= this.lineWidth) {
if (this.lineWidth !== STWriter.NO_WRAP && !this.atStartOfLine && this.charPosition >= this.lineWidth) {
// ok to wrap
// Walk wrap string and look for A\nB. Spit out A\n
// then spit indent or anchor, whichever is larger
Expand Down
4 changes: 2 additions & 2 deletions src/Interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ export class Interpreter {
protected storeArgs(scope: IInstanceScope, argCount: number, st?: IST): void;
protected storeArgs(...args: unknown[]): void {
if (typeof args[1] !== "number") {
const [scope, attrs, st] = args as [IInstanceScope, Map<string, unknown>, IST | undefined];
const [scope, attrs, st] = args as [IInstanceScope, HashMap<string, unknown>, IST | undefined];

let noSuchAttributeReported = false;
if (attrs && st) {
Expand Down Expand Up @@ -1174,7 +1174,7 @@ export class Interpreter {
continue;
}

if (!attrs || !attrs.has(argumentKey)) {
if (!attrs || !attrs.containsKey(argumentKey)) {
argumentCountMismatch = true;
break;
}
Expand Down
8 changes: 7 additions & 1 deletion src/support/HashMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ export class HashMap<Key, Value> implements Map<Key, Value> {
}

public has(key: Key): boolean {
return this.get(key) !== undefined;
return this.containsKey(key);
}

public containsKey(key: Key): boolean {
const hashCode = MurmurHash.hashCode(key);

return this.#data[hashCode] !== undefined;
}

public set(key: Key, value: Value): this {
Expand Down
9 changes: 5 additions & 4 deletions tests/BaseTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ export abstract class BaseTest {

public static User = class User {
public id: number;
public name: string;
#name: string;
public constructor(id: number, name: string) {
this.id = id; this.name = name;
this.id = id;
this.#name = name;
}
public isManager(): boolean { return true; }
public hasParkingSpot(): boolean { return true; }
public getName(): string { return this.name; }
public getName(): string { return this.#name; }
};

public static HashableUser = class HashableUser extends BaseTest.User {
Expand All @@ -39,7 +40,7 @@ export abstract class BaseTest {
if (o instanceof HashableUser) {
const hu = o;

return this.id === hu.id && this.name === hu.name;
return this.id === hu.id && this.getName() === hu.getName();
}

return false;
Expand Down
10 changes: 10 additions & 0 deletions tests/TestCoreBasics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ export class TestCoreBasics extends BaseTest {
assertEquals(expected, result);
};

@Test
public testBooleanGetName(): void {
const template = "<t.name>"; // call getName
const st = new ST(template);
st?.add("t", new BaseTest.User(32, "Ter"));
const expected = "Ter";
const result = st?.render();
assertEquals(expected, result);
};

@Test
public testNullAttrProp(): void {
const template = "<u.id>: <u.name>";
Expand Down
2 changes: 1 addition & 1 deletion tests/TestLineWrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import path from "path";
import { BaseTest } from "./BaseTest.js";
import { assertEquals } from "./junit.js";

import { Test } from "./decorators.js";
import { AutoIndentWriter, Misc, ST, STGroupFile, StringWriter } from "../src/index.js";
import { Test } from "./decorators.js";

export class TestLineWrap extends BaseTest {
@Test
Expand Down
2 changes: 1 addition & 1 deletion tests/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export function Test<T extends ITestParameters, This, Args extends unknown[], Re
// provider name on the prototype.
Object.getOwnPropertyNames(Object.getPrototypeOf(this)).forEach((name) => {
if (typeof this[name] === "function" && name === param.dataProvider) {
provider = this[name] as Function;
provider = this[name];
}
});
}
Expand Down

0 comments on commit 6b14554

Please sign in to comment.