From 600231c52fc6fa43e605ef19ffb20b20b56fa752 Mon Sep 17 00:00:00 2001 From: "Narcisse D. ASSOGBA" Date: Thu, 29 Mar 2018 19:03:17 +0100 Subject: [PATCH] Add "noImplicitAny" constraint and other tslint constraints --- types/sql-bricks/index.d.ts | 121 ++++++++++++++------------- types/sql-bricks/sql-bricks-tests.ts | 64 ++++++++------ types/sql-bricks/tsconfig.json | 2 +- types/sql-bricks/tslint.json | 3 - 4 files changed, 103 insertions(+), 87 deletions(-) diff --git a/types/sql-bricks/index.d.ts b/types/sql-bricks/index.d.ts index d09bfee715..ccc7e523a7 100644 --- a/types/sql-bricks/index.d.ts +++ b/types/sql-bricks/index.d.ts @@ -78,16 +78,17 @@ declare namespace SqlBricks { * Appends additional columns to an existing query. * @param columns can be passed as multiple arguments, a comma-delimited string or an array. */ - select(...columns: string[]): SelectStatement + select(...columns: Array): SelectStatement /** * Appends additional columns to an existing query. * @param columns can be passed as multiple arguments, a comma-delimited string or an array. */ - select(columns: string[]): SelectStatement - select(...params): SelectStatement + select(columns: string[] | SelectStatement[]): SelectStatement + as(alias: string): SelectStatement - distinct(...columns): SelectStatement + distinct(...columns: Array): SelectStatement + distinct(columns: string[] | SelectStatement[]): SelectStatement /** * Makes the query a SELECT ... INTO query (which creates a new table with the results of the query). @@ -102,8 +103,8 @@ declare namespace SqlBricks { */ intoTable(tbl: TableName): SelectStatement - intoTemp(tbl: TableName): SelectStatement, - intoTempTable(tbl: TableName): SelectStatement, + intoTemp(tbl: TableName): SelectStatement + intoTempTable(tbl: TableName): SelectStatement /** * Table names can be passed in as multiple string arguments, a comma-delimited string or an array. @@ -151,8 +152,8 @@ declare namespace SqlBricks { fullOuterJoin(tbl: string, onCol1: string, onCol2: string): SelectStatement fullOuterJoin(firstTbl: string, ...otherTbls: string[]): SelectStatement - on(onCriteria: OnCriteria | WhereExpression): SelectStatement, - on(onCol1: string, onCol2: string): SelectStatement, + on(onCriteria: OnCriteria | WhereExpression): SelectStatement + on(onCol1: string, onCol2: string): SelectStatement /** * Joins using USING instead of ON. @@ -178,11 +179,10 @@ declare namespace SqlBricks { naturalRightOuterJoin(tbl: string): SelectStatement naturalFullOuterJoin(tbl: string): SelectStatement - where(column: string, value?: string): SelectStatement - where(whereExpr: WhereExpression): SelectStatement - where(...options): SelectStatement + where(column?: string | null, value?: any): SelectStatement + where(...whereExpr: WhereExpression[]): SelectStatement - and(...options): SelectStatement + and(...options: any[]): SelectStatement /** * Sets or extends the GROUP BY columns. @@ -219,28 +219,34 @@ declare namespace SqlBricks { interface InsertStatement extends Statement { into(tbl: TableName, ...columns: any[]): InsertStatement intoTable(tbl: TableName, ...columns: any[]): InsertStatement - select(...params): SelectStatement, - values(...values): InsertStatement + select(...columns: Array): SelectStatement + select(columns: string[] | SelectStatement[]): SelectStatement + values(...values: any[]): InsertStatement } /** * An UPDATE statement */ interface UpdateStatement extends Statement { - values(...values): UpdateStatement - set(...values): UpdateStatement, - where(...options): UpdateStatement, - and(...options): UpdateStatement, + values(...values: any[]): UpdateStatement + set(...values: any[]): UpdateStatement + where(column?: string | null, value?: any): SelectStatement + where(...whereExpr: WhereExpression[]): SelectStatement + and(column?: string | null, value?: any): SelectStatement + and(...whereExpr: WhereExpression[]): SelectStatement } /** * A DELETE statement */ interface DeleteStatement extends Statement { - from(...tbls: string[]): DeleteStatement, - using(...columnList): DeleteStatement, - where(...options): DeleteStatement, - and(...options): DeleteStatement, + from(...tbls: string[]): DeleteStatement + using(...columnList: string[]): SelectStatement + using(columnList: string[]): SelectStatement + where(column?: string | null, value?: any): SelectStatement + where(...whereExpr: WhereExpression[]): SelectStatement + and(column?: string | null, value?: any): SelectStatement + and(...whereExpr: WhereExpression[]): SelectStatement } } @@ -251,7 +257,7 @@ interface SqlBricksFn { * anywhere that a column is expected (the left-hand side of WHERE criteria and many other SQL Bricks APIs) * @param value value to be wraped */ - val(value): any + val(value: any): any /** * Returns a new INSERT statement. It can be used with or without the new operator. @@ -263,7 +269,7 @@ interface SqlBricksFn { * insert('person', {'first_name': 'Fred', 'last_name': 'Flintstone'}); * // INSERT INTO person (first_name, last_name) VALUES ('Fred', 'Flintstone') */ - insert(tbl?: string, ...values): SqlBricks.InsertStatement + insert(tbl?: string, ...values: any[]): SqlBricks.InsertStatement /** * Returns a new INSERT statement. It can be used with or without the new operator. @@ -275,23 +281,22 @@ interface SqlBricksFn { * insert('person', {'first_name': 'Fred', 'last_name': 'Flintstone'}); * // INSERT INTO person (first_name, last_name) VALUES ('Fred', 'Flintstone') */ - insertInto(tbl?: string, ...values): SqlBricks.InsertStatement + insertInto(tbl?: string, ...values: any[]): SqlBricks.InsertStatement /** * Returns a new select statement, seeded with a set of columns. It can be used with or without the new keyword. * @param columns it can be passed in here (or appended later via sel.select() or sel.distinct()) via multiple arguments * or a comma-delimited string or an array. If no columns are specified, toString() will default to SELECT *. */ - select(...columns: string[]): SqlBricks.SelectStatement + select(...columns: Array): SqlBricks.SelectStatement select(columns: string[] | SqlBricks.SelectStatement[]): SqlBricks.SelectStatement - select(...params): SqlBricks.SelectStatement /** * Returns a new UPDATE statement. It can be used with or without the new operator. * @param tbl table name * @param values */ - update(tbl: string, ...values): SqlBricks.UpdateStatement + update(tbl: string, ...values: any[]): SqlBricks.UpdateStatement /** * Returns a new DELETE statement. It can be used with or without the new operator. @@ -322,7 +327,7 @@ interface SqlBricksFn { * Sets a user-supplied function to automatically generate the .on() criteria for joins whenever it is not supplied explicitly. * @param func */ - joinCriteria(func?: (...args) => SqlBricks.OnCriteria): any + joinCriteria(func?: (...args: any[]) => SqlBricks.OnCriteria): any _extension(): any prop: number @@ -357,47 +362,47 @@ interface SqlBricksFn { * @param value1 * @param value2 */ - between(column: string, value1, value2): SqlBricks.WhereExpression + between(column: string, value1: any, value2: any): SqlBricks.WhereExpression isNull(column: string): SqlBricks.WhereExpression isNotNull(column: string): SqlBricks.WhereExpression - like(column: string, value, escapeStr?: string): SqlBricks.WhereExpression - exists(stmt): SqlBricks.WhereExpression + like(column: string, value: any, escapeStr?: string): SqlBricks.WhereExpression + exists(stmt: any): SqlBricks.WhereExpression in(column: string, stmt: SqlBricks.SelectStatement): SqlBricks.WhereExpression - in(column: string, ...values): SqlBricks.WhereExpression + in(column: string, ...values: any[]): SqlBricks.WhereExpression /** * Generates the appropriate relational operator (=, <>, <, <=, > or >=). * @param column column name or query result * @param value column value */ - eq(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary - equal(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary - notEq(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary - lt(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary - lte(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary - gt(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary - gte(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary + eq(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary + equal(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary + notEq(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary + lt(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary + lte(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary + gt(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary + gte(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary - eqAll(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary - notEqAll(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary - ltAll(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary - lteAll(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary - gtAll(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary - gteAll(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary + eqAll(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary + notEqAll(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary + ltAll(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary + lteAll(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary + gtAll(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary + gteAll(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary - eqAny(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary - notEqAny(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary - ltAny(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary - lteAny(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary - gtAny(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary - gteAny(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary + eqAny(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary + notEqAny(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary + ltAny(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary + lteAny(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary + gtAny(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary + gteAny(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary - eqSome(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary - notEqSome(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary - ltSome(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary - lteSome(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary - gtSome(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary - gteSome(column: string | SqlBricks.SelectStatement, value?): SqlBricks.WhereBinary + eqSome(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary + notEqSome(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary + ltSome(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary + lteSome(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary + gtSome(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary + gteSome(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary } declare const SqlBricks: SqlBricksFn diff --git a/types/sql-bricks/sql-bricks-tests.ts b/types/sql-bricks/sql-bricks-tests.ts index 94014d913d..8a5c7de5d7 100644 --- a/types/sql-bricks/sql-bricks-tests.ts +++ b/types/sql-bricks/sql-bricks-tests.ts @@ -6,22 +6,38 @@ import * as sql from "sql-bricks"; // tslint:disable-next-line:prefer-const -let assert; +let assert: any; const select = sql.select; -const insertInto = sql.insertInto, insert = sql.insert, - update = sql.update, del = sql.delete; -const and = sql.and, or = sql.or, like = sql.like, not = sql.not, $in = sql.in, - isNull = sql.isNull, isNotNull = sql.isNotNull, equal = sql.equal, eq = sql.eq, - notEq = sql.notEq, lt = sql.lt, lte = sql.lte, gt = sql.gt, gte = sql.gte, - between = sql.between, exists = sql.exists, eqAny = sql.eqAny, notEqAny = sql.notEqAny; +const insertInto = sql.insertInto; +const insert = sql.insert; +const update = sql.update; +const del = sql.delete; +const and = sql.and; +const or = sql.or; +const like = sql.like; +const not = sql.not; +const $in = sql.in; +const isNull = sql.isNull; +const isNotNull = sql.isNotNull; +const equal = sql.equal; +const eq = sql.eq; +const notEq = sql.notEq; +const lt = sql.lt; +const lte = sql.lte; +const gt = sql.gt; +const gte = sql.gte; +const between = sql.between; +const exists = sql.exists; +const eqAny = sql.eqAny; +const notEqAny = sql.notEqAny; const alias_expansions = { usr: 'user', psn: 'person', addr: 'address' }; -const table_to_alias = alias_expansions; +const table_to_alias: sql.OnCriteria = alias_expansions; sql.aliasExpansions(alias_expansions); -sql.joinCriteria(function(left_tbl, left_alias, right_tbl, right_alias) { - const criteria = {}; - criteria[left_alias + '.' + table_to_alias[right_tbl] + '_fk'] = right_alias + '.pk'; +sql.joinCriteria(function(left_tbl: string, left_alias: string, right_tbl: string, right_alias: string) { + const criteria: sql.OnCriteria = {}; + criteria[`${left_alias}.${table_to_alias[right_tbl]}_fk`] = right_alias + '.pk'; return criteria; }); @@ -160,7 +176,7 @@ describe('SQL Bricks', function() { }); it('should support user-supplied conversions', function() { const orig_bool = sql.conversions.Boolean; - sql.conversions.Boolean = function(bool) { return bool ? '1' : '0'; }; + sql.conversions.Boolean = function(bool: boolean) { return bool ? '1' : '0'; }; const str = del('user').where('active', false).toString(); sql.conversions.Boolean = orig_bool; assert.equal(str, 'DELETE FROM "user" WHERE active = 0'); @@ -184,7 +200,7 @@ describe('SQL Bricks', function() { select().from('test').join('psn').join('usr').toString(); sql.joinCriteria(orig); - assert(left_tbls.indexOf('person') > -1 && left_tbls.indexOf('psn') === -1, 'left_tbl is not expanded: [' + left_tbls.join(',') + ']'); + assert(left_tbls.indexOf('person') > -1 && left_tbls.indexOf('psn') === -1, `left_tbl is not expanded: [ ${left_tbls.join(',')} ]`); }); it('should support aliases', function() { @@ -727,9 +743,9 @@ describe('SQL Bricks', function() { it('should support changing the default array handling', function() { const proto = sql.select.prototype; const orig = proto.where; - proto.where = function(criteria) { + proto.where = function(criteria: sql.OnCriteria) { // return orig.call(this, arraysToOrs(criteria)); - return orig.call() + return orig.call(); }; check(select().from('table').where({ this: 'test', that: ['test1', 'test2'] }), @@ -738,15 +754,13 @@ describe('SQL Bricks', function() { proto.where = orig; }); - function arraysToOrs(criteria) { + function arraysToOrs(criteria: sql.WhereObject) { const where = and(); for (const col in criteria) { const val = criteria[col]; - let expr; - if (Array.isArray(val)) - expr = or(val.map(function(val) { return eq(col, val); })); - else - expr = eq(col, val); + let expr: sql.WhereExpression; + expr = Array.isArray(val) ? or(val.map(function(val) { return eq(col, val); })) + : expr = eq(col, val); where.expressions.push(expr); } return where; @@ -929,7 +943,7 @@ describe('SQL Bricks', function() { }); }); -function describe(...param) { } -function it(...param) { } -function check(...param) { } -function checkParams(...param) { } +function describe(...param: any[]) { } +function it(...param: any[]) { } +function check(...param: any[]) { } +function checkParams(...param: any[]) { } diff --git a/types/sql-bricks/tsconfig.json b/types/sql-bricks/tsconfig.json index 3c5db90a4f..4b321fc911 100644 --- a/types/sql-bricks/tsconfig.json +++ b/types/sql-bricks/tsconfig.json @@ -4,7 +4,7 @@ "lib": [ "es6" ], - "noImplicitAny": false, + "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": true, "strictFunctionTypes": true, diff --git a/types/sql-bricks/tslint.json b/types/sql-bricks/tslint.json index 1e7260a20f..4ddaadda1f 100644 --- a/types/sql-bricks/tslint.json +++ b/types/sql-bricks/tslint.json @@ -2,9 +2,6 @@ "extends": "dtslint/dt.json", "rules": { "only-arrow-functions": false, - "prefer-template": false, - "prefer-conditional-expression": false, - "one-variable-per-declaration": false, "semicolon":false } } \ No newline at end of file