Skip to content

Commit

Permalink
chore: set typescript to strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesfer committed Jun 25, 2019
1 parent 7f6360c commit 0bc8018
Show file tree
Hide file tree
Showing 27 changed files with 139 additions and 122 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
"scripts": {
"commit": "git-cz",
"build": "yarn build:declaration && yarn build:rollup",
"build:declaration": "tsc --project tsconfig.json --outDir dist/typings --declaration --emitDeclarationOnly",
"build:declaration": "tsc --project tsconfig.declaration.json --outDir dist/typings --declaration --emitDeclarationOnly",
"build:rollup": "rollup -c",
"lint": "tslint --project ./tsconfig.lint.json",
"lint": "tslint --project ./tsconfig.json",
"test": "docker-compose -f docker/docker-compose.test.yml -p cypher-query-builder up --exit-code-from tests",
"test:unit": "nyc --reporter=html --reporter=text-summary mocha src/*.spec.ts src/**/*.spec.ts",
"validate": "yarn --silent lint && yarn --silent build && yarn --silent test",
Expand Down
6 changes: 3 additions & 3 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { RemoveProperties } from './clauses/remove';
* @internal
*/
export interface WrapperClause {
new (clause: Clause): Clause;
new (clause: Set): Clause;
}

/**
Expand Down Expand Up @@ -155,7 +155,7 @@ export class SetBlock<Q> {
return this.chain(this.wrap(new Set({ variables }, { override })));
}

private wrap(clause: Clause): Clause {
private wrap(clause: Set): Clause {
return this.wrapper ? new this.wrapper(clause) : clause;
}
}
Expand Down Expand Up @@ -253,7 +253,7 @@ export abstract class Builder<Q> extends SetBlock<Q> {
* @returns {Q}
*/
createNode(
name?: Many<string> | Dictionary<any>,
name: Many<string> | Dictionary<any>,
labels?: Many<string> | Dictionary<any>,
conditions?: Dictionary<any>,
) {
Expand Down
2 changes: 1 addition & 1 deletion src/clause-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ClauseCollection extends Clause {
* Adds a clause to the child list.
* @param {Clause} clause
*/
addClause(clause) {
addClause(clause: Clause) {
clause.useParameterBag(this.parameterBag);
this.clauses.push(clause);
}
Expand Down
2 changes: 1 addition & 1 deletion src/clauses/node-pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Pattern } from './pattern';

export class NodePattern extends Pattern {
constructor(
name: Many<string> | Dictionary<any>,
name?: Many<string> | Dictionary<any>,
labels?: Many<string> | Dictionary<any>,
conditions?: Dictionary<any>,
) {
Expand Down
4 changes: 2 additions & 2 deletions src/clauses/pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { Parameter } from '../parameter-bag';
import { stringifyLabels } from '../utils';

export abstract class Pattern extends Clause {
protected useExpandedConditions: boolean;
protected conditionParams = {};
protected useExpandedConditions: boolean | undefined;
protected conditionParams: Dictionary<Parameter> | Parameter = {};
protected name: string;
protected labels: string[];
protected conditions: Dictionary<any>;
Expand Down
2 changes: 1 addition & 1 deletion src/clauses/relation-pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Dictionary, trim, Many, isNil, isNumber, isArray, every } from 'lodash'
import { Pattern } from './pattern';
import { PathLength, stringifyPathLength } from '../utils';

const isPathLengthArray = value => (
const isPathLengthArray = (value: any) => (
isArray(value) && every(value, item => isNumber(item) || isNil(item)) && value.length > 0
);
const isPathLength = (value: any): value is PathLength => (
Expand Down
5 changes: 3 additions & 2 deletions src/clauses/return.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { TermListClause } from './term-list-clause';
import { Many } from 'lodash';
import { Term, TermListClause } from './term-list-clause';

export class Return extends TermListClause {
/**
* Creates a return clause
* @param {string|object|array<string|object>|} terms [description]
*/
constructor(terms) {
constructor(terms: Many<Term>) {
super(terms);
}

Expand Down
18 changes: 9 additions & 9 deletions src/clauses/set.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
concat, map, mapValues, castArray, Dictionary,
Many, isObject,
Many, isObject, isString,
} from 'lodash';
import { Clause } from '../clause';
import { stringifyLabels } from '../utils';
Expand All @@ -13,7 +13,7 @@ export type SetProperties = {
};

export interface SetOptions {
override: boolean;
override?: boolean;
}

export class Set extends Clause {
Expand All @@ -34,11 +34,11 @@ export class Set extends Clause {

protected makeVariableStatement = (value: string | Dictionary<string>, key: string): string => {
const op = this.override ? ' = ' : ' += ';
if (isObject(value)) {
const operationStrings = map(value, (value, prop) => `${key}.${prop}${op}${value}`);
return operationStrings.join(', ');
if (isString(value)) {
return key + op + value;
}
return key + op + value;
const operationStrings = map(value, (value, prop) => `${key}.${prop}${op}${value}`);
return operationStrings.join(', ');
}

constructor(
Expand All @@ -48,7 +48,7 @@ export class Set extends Clause {
super();

let options: SetOptions | undefined = inOptions;
if (inOptions === undefined) {
if (options === undefined) {
// tslint:disable-next-line:max-line-length
console.warn('Warning: In the future, override will default to false in a Set clause when no options are provided. To retain the old behaviour, pass { override: false } as options to the Set constructor.');
options = { override: true };
Expand All @@ -58,8 +58,8 @@ export class Set extends Clause {
this.values = mapValues(values, (value, name) => {
return this.parameterBag.addParam(value, name);
});
this.variables = variables;
this.override = options.override;
this.variables = variables || {};
this.override = options.override === undefined ? false : options.override;
}

build() {
Expand Down
3 changes: 2 additions & 1 deletion src/clauses/skip.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Clause } from '../clause';
import { Parameter } from '../parameter-bag';

export class Skip extends Clause {
protected amountParam;
protected amountParam: Parameter;

constructor(public amount: number | string) {
super();
Expand Down
27 changes: 16 additions & 11 deletions src/clauses/term-list-clause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export class TermListClause extends Clause {
if (isPlainObject(term)) {
return this.stringifyDictionary(term);
}

return '';
}

private stringifyProperty(prop: string, alias?: string, node?: string): string {
Expand All @@ -78,17 +80,20 @@ export class TermListClause extends Clause {
}

private stringifyDictionary(node: Dictionary<string | Properties>): string[] {
const convertToString = (list, prop, key) => {
if (isString(prop)) {
// Alias
list.push(this.stringifyProperty(prop, key));
} else {
// Node with properties
list.push(...this.stringifyProperties(prop, null, key));
}
return list;
};
return reduce(node, convertToString, []);
return reduce(
node,
(list, prop, key) => {
if (isString(prop)) {
// Alias
list.push(this.stringifyProperty(prop, key));
} else {
// Node with properties
list.push(...this.stringifyProperties(prop, undefined, key));
}
return list;
},
[] as string[],
);
}

build() {
Expand Down
9 changes: 6 additions & 3 deletions src/clauses/where-comparators.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Dictionary } from 'lodash';
import { expect } from 'chai';
import {
between,
between, Comparator,
contains, endsWith, equals, exists, greaterEqualTo, greaterThan, hasLabel,
inArray,
isNull, lessEqualTo, lessThan, regexp, startsWith,
Expand All @@ -9,11 +10,12 @@ import { ParameterBag } from '../parameter-bag';

describe('Where Comparators', () => {
let bag: ParameterBag;

beforeEach(() => {
bag = new ParameterBag();
});

const simpleOperators = {
const simpleOperators: Dictionary<(value: any, variable?: boolean) => Comparator> = {
equals,
greaterThan,
greaterEqualTo,
Expand All @@ -24,7 +26,8 @@ describe('Where Comparators', () => {
contains,
inArray,
};
const opSymbols = {

const opSymbols: Dictionary<string> = {
equals: '=',
greaterThan: '>',
greaterEqualTo: '>=',
Expand Down
9 changes: 5 additions & 4 deletions src/clauses/where-operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Precedence,
WhereOp,
} from './where-utils';
import { ParameterBag } from '../parameter-bag';

export const operators = { and, or, xor, not };

Expand Down Expand Up @@ -38,7 +39,7 @@ export class WhereAnd extends WhereOp {
super();
}

evaluate(params, precedence = Precedence.None, name = '') {
evaluate(params: ParameterBag, precedence = Precedence.None, name = '') {
return combineAnd(params, this.conditions, precedence, name);
}
}
Expand Down Expand Up @@ -69,7 +70,7 @@ export class WhereOr extends WhereOp {
super();
}

evaluate(params, precedence = Precedence.None, name = '') {
evaluate(params: ParameterBag, precedence = Precedence.None, name = '') {
return combineOr(params, this.conditions, precedence, name);
}
}
Expand Down Expand Up @@ -98,7 +99,7 @@ export class WhereXor extends WhereOp {
super();
}

evaluate(params, precedence = Precedence.None, name = '') {
evaluate(params: ParameterBag, precedence = Precedence.None, name = '') {
return combineXor(params, this.conditions, precedence, name);
}
}
Expand Down Expand Up @@ -127,7 +128,7 @@ export class WhereNot extends WhereOp {
super();
}

evaluate(params, precedence = Precedence.None, name = '') {
evaluate(params: ParameterBag, precedence = Precedence.None, name = '') {
return combineNot(params, this.conditions, precedence, name);
}
}
5 changes: 3 additions & 2 deletions src/clauses/with.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { TermListClause } from './term-list-clause';
import { Many } from 'lodash';
import { Term, TermListClause } from './term-list-clause';

export class With extends TermListClause {
/**
* Creates a with clause
* @param {string|object|array<string|object>} terms
*/
constructor(terms) {
constructor(terms: Many<Term>) {
super(terms);
}

Expand Down
40 changes: 21 additions & 19 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ function isCredentials(credentials: any): credentials is Credentials {
return 'username' in credentials && 'password' in credentials;
}

// We have to correct the type of lodash's isFunction method because it doesn't correctly narrow
// union types such as the options parameter passed to the connection constructor.
const isTrueFunction: (value: any) => value is Function = isFunction;

/**
* A connection lets you access the Neo4j server and run queries against it.
*
Expand Down Expand Up @@ -108,15 +112,13 @@ export class Connection extends Builder<Query> {
? neo4j.auth.basic(auth.username, auth.password)
: auth;

this.options = {
driverConstructor: isFunction(options) ? options
: options.driverConstructor ? options.driverConstructor : neo4j.driver,
driverConfig: isFunction(options) || !options.driverConfig ? {} : options.driverConfig,
};

this.driver = this.options.driverConstructor(this.url, this.auth, this.options.driverConfig);
const driverConstructor = isTrueFunction(options) ? options
: options.driverConstructor ? options.driverConstructor : neo4j.driver;
const driverConfig = isTrueFunction(options) || !options.driverConfig
? {} : options.driverConfig;
this.options = { driverConstructor, driverConfig };
this.driver = driverConstructor(this.url, this.auth, this.options.driverConfig);
this.open = true;

connections.push(this);
}

Expand Down Expand Up @@ -207,19 +209,20 @@ export class Connection extends Builder<Query> {
* @returns {Promise<Dictionary<R>[]>}
*/
run<R = any>(query: Query): Promise<Dictionary<R>[]> {
if (!this.open) {
throw Error('Cannot run query; connection is not open.');
}

if (query.getClauses().length === 0) {
throw Error('Cannot run query: no clauses attached to the query.');
}

const queryObj = query.buildQueryObject();
const session = this.session();
if (!session) {
throw Error('Cannot run query: connection is not open.');
}

const queryObj = query.buildQueryObject();
const result = session.run(queryObj.query, queryObj.params);

// Need to wrap promise in an any-promise
return AnyPromise.resolve(session.run(queryObj.query, queryObj.params))
return AnyPromise.resolve(result)
.then((result) => {
session.close();
return this.transformer.transformRecords<R>(result.records);
Expand Down Expand Up @@ -298,18 +301,17 @@ export class Connection extends Builder<Query> {
* In practice this should never happen unless you're doing some strange things.
*/
stream<R = any>(query: Query): RxObservable<Dictionary<R>> {
if (!this.open) {
throw Error('Cannot run query; connection is not open.');
}

if (query.getClauses().length === 0) {
throw Error('Cannot run query: no clauses attached to the query.');
}

const queryObj = query.buildQueryObject();
const session = this.session();
if (!session) {
throw Error('Cannot run query: connection is not open.');
}

// Run the query
const queryObj = query.buildQueryObject();
const result = session.run(queryObj.query, queryObj.params);

// Subscribe to the result and clean up the session
Expand Down
2 changes: 1 addition & 1 deletion src/parameter-bag.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ParameterBag } from './parameter-bag';
import { expect } from '../test-setup';

describe('ParameterBag', () => {
let parameterBag;
let parameterBag: ParameterBag;

beforeEach(() => {
parameterBag = new ParameterBag();
Expand Down
2 changes: 1 addition & 1 deletion src/parameter-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Dictionary } from 'lodash';
export class ParameterContainer {
protected parameterBag = new ParameterBag();

useParameterBag(newBag) {
useParameterBag(newBag: ParameterBag) {
newBag.importParams(this.parameterBag);
this.parameterBag = newBag;
}
Expand Down
Loading

0 comments on commit 0bc8018

Please sign in to comment.