Skip to content

Commit

Permalink
fix: stricter type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
chanind committed Jan 1, 2024
1 parent 70eab9e commit ca276d8
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/lib/_lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { debug } from './logger';
// These are the regex patterns for parsing. They must not have any
// capturing groups. They are used during lexing and will be
// checked by name during parsing.
const PATTERNS = {
const PATTERNS: { [key: string]: string } = {
COMMENT: '#.*$',
STRING: '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"',
ALIGNMENT: '~(?:[a-z].?)?[0-9]+(?:,[0-9]+)*',
Expand Down
12 changes: 6 additions & 6 deletions src/lib/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ export class Model {
const deifs: Map<Constant, Array<_Dereified>> = new Map();
if (reifications) {
for (const [role, concept, source, target] of reifications) {
if (reifs[role] === undefined) {
reifs[role] = [];
if (!reifs.has(role)) {
reifs.set(role, []);
}
reifs[role].push([concept, source, target]);
reifs.get(role)!.push([concept, source, target]);
if (!deifs.has(concept)) {
deifs.set(concept, []);
}
Expand Down Expand Up @@ -258,7 +258,7 @@ export class Model {
* Return `true` if `role` can be reified.
*/
isRoleReifiable(role: Role): boolean {
return role in this.reifications;
return this.reifications.has(role);
}

/**
Expand All @@ -285,10 +285,10 @@ export class Model {
reify(triple: Triple, options: ModelReifyOptions = {}): _Reification {
const { variables } = options;
const [source, role, target] = triple;
if (!(role in this.reifications)) {
if (!this.reifications.has(role)) {
throw new ModelError(`'${role}' cannot be reified`);
}
const [concept, sourceRole, targetRole] = this.reifications[role][0];
const [concept, sourceRole, targetRole] = this.reifications.get(role)![0];

let variable = '_';
if (variables) {
Expand Down
12 changes: 6 additions & 6 deletions src/lib/transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ test('canonicalize_roles_amr_codec', (t) => {
test('reify_edges_default_codec', (t) => {
const decode = defCodec.decode.bind(defCodec);
const norm = makeNorm(reifyEdges, defModel);
const form = makeForm((g: Graph, { indent }: { indent?: number }) =>
const form = makeForm((g: Graph, { indent }: { indent?: number | null }) =>
defCodec.encode(g, { indent }),
);

Expand All @@ -75,7 +75,7 @@ test('reify_edges_default_codec', (t) => {
test('reify_edges_amr_codec', (t) => {
const decode = amrCodec.decode.bind(amrCodec);
const norm = makeNorm(reifyEdges, amrModel);
const form = makeForm((g: Graph, { indent }: { indent?: number }) =>
const form = makeForm((g: Graph, { indent }: { indent?: number | null }) =>
amrCodec.encode(g, { indent }),
);

Expand All @@ -101,7 +101,7 @@ test('reify_edges_amr_codec', (t) => {
test('dereify_edges_default_codec', (t) => {
const decode = defCodec.decode.bind(defCodec);
const norm = makeNorm(dereifyEdges, defModel);
const form = makeForm((g: Graph, { indent }: { indent?: number }) =>
const form = makeForm((g: Graph, { indent }: { indent?: number | null }) =>
defCodec.encode(g, { indent }),
);

Expand All @@ -125,7 +125,7 @@ test('dereify_edges_default_codec', (t) => {
test('dereify_edges_amr_codec', (t) => {
const decode = amrCodec.decode.bind(amrCodec);
const norm = makeNorm(dereifyEdges, amrModel);
const form = makeForm((g: Graph, { indent }: { indent?: number }) =>
const form = makeForm((g: Graph, { indent }: { indent?: number | null }) =>
amrCodec.encode(g, { indent }),
);

Expand All @@ -148,7 +148,7 @@ test('dereify_edges_amr_codec', (t) => {
test('reify_attributes', (t) => {
const decode = defCodec.decode.bind(defCodec);
const norm = reifyAttributes;
const form = makeForm((g: Graph, { indent }: { indent?: number }) =>
const form = makeForm((g: Graph, { indent }: { indent?: number | null }) =>
defCodec.encode(g, { indent }),
);

Expand All @@ -165,7 +165,7 @@ test('indicate_branches', (t) => {
(x, { model }: { model: Model }) => indicateBranches(x, model),
defModel,
);
const form = makeForm((g: Graph, { indent }: { indent?: number }) =>
const form = makeForm((g: Graph, { indent }: { indent?: number | null }) =>
defCodec.encode(g, { indent }),
);

Expand Down
2 changes: 1 addition & 1 deletion src/lib/tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ test('walk', (t) => {
// assert _vars(t) == ['a', 'b']

test('reset_variables', (t) => {
const _vars = (t) => t.nodes().map(([v]) => v);
const _vars = (t: Tree) => t.nodes().map(([v]) => v);

const t1 = new Tree(one_arg_node());
t.deepEqual(_vars(t1), ['a', 'b']);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ const _nodes = (node: Node): Node[] => {
return ns;
};

function* _walk(node: Node, path: number[]) {
function* _walk(node: Node, path: number[]): Generator<_Step> {
const branches = node[1] ?? [];
for (const [i, branch] of branches.entries()) {
const curpath = path.concat([i]);
Expand Down
11 changes: 1 addition & 10 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,7 @@
"inlineSourceMap": true,
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
"resolveJsonModule": true /* Include modules imported with .json extension. */,

// "strict": true /* Enable all strict type-checking options. */,

/* Strict Type-Checking Options */
// "noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
"strictNullChecks": true /* Enable strict null checks. */,
// "strictFunctionTypes": true /* Enable strict checking of function types. */,
// "strictPropertyInitialization": true /* Enable strict checking of property initialization in classes. */,
// "noImplicitThis": true /* Raise error on 'this' expressions with an implied 'any' type. */,
// "alwaysStrict": true /* Parse in strict mode and emit "use strict" for each source file. */,
"strict": true /* Enable all strict type-checking options. */,

/* Additional Checks */
"noUnusedLocals": true /* Report errors on unused locals. */,
Expand Down

0 comments on commit ca276d8

Please sign in to comment.