Skip to content

Commit

Permalink
BREAKING CHANGES: Flow -> Typescript (#209)
Browse files Browse the repository at this point in the history
* chore: add typescript dependencies
* chore: patch transform-export-default-name to make tests run without noise
* chore: build js with babel and d.ts with typescript
* chore: tell nyc to cover ts files
* fix: remove unused dependencies
* feat: match types with @types/slonik
* style: don't lint output dir
* fix: bump babel plugin version to avoid patch
* chore: disable automatic release (temporary)
* fix: don't try to patch-package for end-users
* chore: remove types package
  • Loading branch information
gajus committed Oct 19, 2020
2 parents 343b1a8 + fa1a463 commit 169c7b9
Show file tree
Hide file tree
Showing 116 changed files with 802 additions and 640 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"plugins": [
"transform-export-default-name",
"macros",
"@babel/transform-flow-strip-types"
"@babel/plugin-transform-typescript"
],
"presets": [
[
Expand Down
29 changes: 27 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
{
"parser": "@typescript-eslint/parser",
"parserOptions": {"ecmaVersion": 2018, "sourceType": "module"},
"extends": [
"canonical",
"canonical/flowtype"
"canonical"
],
"plugins": [
"@typescript-eslint/eslint-plugin"
],
"ignorePatterns": ["dist", "node_modules", "coverage"],
"root": true,
"rules": {
"@typescript-eslint/no-explicit-any": 1,
"linebreak-style": 0,
// https://github.com/typescript-eslint/typescript-eslint/issues/1624#issuecomment-589731039
"import/no-unresolved": 0,
"no-unused-vars": 0,
"@typescript-eslint/no-unused-vars": 1,
"require-await": 0,
"object-curly-newline": [
2,
{
"ExportDeclaration": {
"minProperties": 1,
"multiline": true
},
"ImportDeclaration": {
"minProperties": 1,
"multiline": true
}
}
],
"import/no-cycle": 0,
"no-continue": 0,
"no-restricted-syntax": 0,
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ node_modules
!.npmignore
!.travis.yml
!.README
yarn.lock
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ after_success:
- NODE_ENV=test nyc npm run test
- nyc report --reporter=text-lcov | coveralls
- npm run build
- semantic-release
# - semantic-release
notifications:
email: false
sudo: false
34 changes: 22 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
},
"ava": {
"babel": {
"extensions": [
"js",
"ts"
],
"compileAsTests": [
"test/helpers/**/*"
]
Expand All @@ -14,7 +18,7 @@
"test/slonik/**/*"
],
"require": [
"@babel/register"
"./ts-register"
],
"timeout": "30s"
},
Expand Down Expand Up @@ -44,23 +48,26 @@
"@ava/babel": "^1.0.1",
"@babel/cli": "^7.11.6",
"@babel/core": "^7.11.6",
"@babel/plugin-transform-flow-strip-types": "^7.10.4",
"@babel/plugin-transform-typescript": "^7.12.1",
"@babel/preset-env": "^7.11.5",
"@babel/register": "^7.11.5",
"@typescript-eslint/eslint-plugin": "2.26.0",
"@typescript-eslint/parser": "2.26.0",
"ava": "^3.13.0",
"babel-plugin-istanbul": "^6.0.0",
"babel-plugin-macros": "^2.8.0",
"babel-plugin-transform-export-default-name": "^2.0.4",
"babel-plugin-transform-export-default-name": "^2.1.0",
"coveralls": "^3.1.0",
"eslint": "^7.11.0",
"eslint-config-canonical": "^24.1.1",
"flow-bin": "^0.135.0",
"flow-copy-source": "^2.0.9",
"expect-type": "^0.8.0",
"gitdown": "^3.1.3",
"husky": "^4.3.0",
"nyc": "^15.1.0",
"patch-package": "^6.2.2",
"semantic-release": "^17.2.1",
"sinon": "^9.2.0"
"sinon": "^9.2.0",
"typescript": "^4.0.3"
},
"engines": {
"node": ">=10.0"
Expand All @@ -78,23 +85,24 @@
],
"license": "BSD-3-Clause",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"name": "slonik",
"nyc": {
"all": true,
"exclude": [
"src/bin",
"src/queries/*.js"
"src/queries/*.ts"
],
"include": [
"src/**/*.js"
"src/**/*.ts"
],
"instrument": false,
"reporter": [
"html",
"text-summary"
],
"require": [
"@babel/register"
"./ts-register"
],
"silent": true,
"sourceMap": false
Expand All @@ -104,10 +112,12 @@
"url": "https://github.com/gajus/slonik"
},
"scripts": {
"build": "rm -fr ./dist && NODE_ENV=production babel ./src --out-dir ./dist --copy-files --source-maps && flow-copy-source src dist",
"build": "rm -fr ./dist && NODE_ENV=production babel ./src --out-dir ./dist --extensions '.ts' --copy-files --source-maps && tsc -p tsconfig.lib.json",
"create-readme": "gitdown ./.README/README.md --output-file ./README.md",
"lint": "eslint ./src ./test && flow",
"test": "NODE_ENV=test nyc ava --verbose"
"eslint": "eslint --ext '.ts' --max-warnings 0",
"lint": "npm run eslint ./src ./test && tsc -p .",
"test": "NODE_ENV=test nyc ava --verbose",
"prepare": "patch-package"
},
"version": "5.0.0"
}
13 changes: 13 additions & 0 deletions patches/inline-loops.macro+1.2.2.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/node_modules/inline-loops.macro/index.d.ts b/node_modules/inline-loops.macro/index.d.ts
index c3eeb39..19161b4 100644
--- a/node_modules/inline-loops.macro/index.d.ts
+++ b/node_modules/inline-loops.macro/index.d.ts
@@ -6,7 +6,7 @@ type Dictionary<Type> = {
/**
* iterables
*/
-type ArrayIterable = any[];
+type ArrayIterable = any[] | readonly any[];
type ObjectIterable = Dictionary<any>;

/**
File renamed without changes.
14 changes: 5 additions & 9 deletions src/QueryStream.js → src/QueryStream.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow

/* eslint-disable fp/no-class, fp/no-this, id-match, flowtype/no-weak-types, promise/prefer-await-to-callbacks */
/* eslint-disable fp/no-class, fp/no-this, id-match, @typescript-eslint/no-explicit-any, promise/prefer-await-to-callbacks */

import {
Readable,
Expand Down Expand Up @@ -28,8 +28,6 @@ export default class QueryStream extends Readable {

cursor: any;

cursor: any;

batchSize: number;

handleRowDescription: Function;
Expand All @@ -38,16 +36,14 @@ export default class QueryStream extends Readable {

handleDataRow: Function;

handlePortalSuspended: Function;

handleCommandComplete: Function;

handleReadyForQuery: Function;

handleError: Function;

// $FlowFixMe
constructor (text, values, options) {
// @ts-ignore
constructor (text, values, options?) {
super({
objectMode: true,
...options,
Expand Down Expand Up @@ -83,7 +79,7 @@ export default class QueryStream extends Readable {
this.cursor.close(callback || close);
}

// $FlowFixMe
// @ts-ignore
_read (size: number) {
if (this._reading || this._closed) {
return;
Expand Down Expand Up @@ -117,7 +113,7 @@ export default class QueryStream extends Readable {
this._reading = false;

for (const row of rows) {
// $FlowFixMe
// @ts-ignore
this.push({
fields: map(result.fields || [], (field) => {
return {
Expand Down
3 changes: 2 additions & 1 deletion src/assertions.js → src/assertions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow

export const assertSqlSqlToken = (subject: *) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const assertSqlSqlToken = (subject: any) => {
if (typeof subject !== 'object' || subject === null || subject.type !== 'SLONIK_TOKEN_SQL') {
throw new TypeError('Query must be constructed using `sql` tagged template literal.');
}
Expand Down
2 changes: 1 addition & 1 deletion src/binders/bindPool.js → src/binders/bindPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default (
pool: InternalDatabasePoolType,
clientConfiguration: ClientConfigurationType,
): DatabasePoolType => {
// eslint-disable-next-line flowtype/no-weak-types
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mapConnection = (targetMethodName: string): any => {
return (query: TaggedTemplateLiteralInvocationType) => {
if (typeof query === 'string') {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const copyFromBinary: InternalCopyFromBinaryFunctionType = async (
});

copyFromBinaryStream.on('end', () => {
// $FlowFixMe
// @ts-ignore
resolve({});
});
});
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import {
} from '../routines';
import type {
InternalQueryFunctionType,
QueryResultRowType,
} from '../types';

const query: InternalQueryFunctionType<*> = async (connectionLogger, connection, clientConfiguration, rawSql, values, inheritedQueryId) => {
// $FlowFixMe
const query: InternalQueryFunctionType<QueryResultRowType> = async (connectionLogger, connection, clientConfiguration, rawSql, values, inheritedQueryId) => {
// @ts-ignore
return executeQuery(
connectionLogger,
connection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ const stream: InternalStreamFunctionType = async (connectionLogger, connection,
}));

transformedStream.on('end', () => {
// $FlowFixMe
// @ts-ignore
resolve({});
});

// Invoked if stream is destroyed using transformedStream.destroy().
transformedStream.on('close', () => {
// $FlowFixMe
// @ts-ignore
resolve({});
});

Expand Down
File renamed without changes.
4 changes: 3 additions & 1 deletion src/errors.js → src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import ExtendableError from 'es6-error';

export class SlonikError extends ExtendableError {}
export class SlonikError extends ExtendableError {
originalError: Error
}

export class InvalidConfigurationError extends SlonikError {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
import createTypeParserPreset from './createTypeParserPreset';

export default (clientUserConfigurationInput?: ClientConfigurationInputType): ClientConfigurationType => {
const typeParsers: $ReadOnlyArray<TypeParserType> = [];
const typeParsers: readonly TypeParserType[] = [];

const configuration = {
captureStackTrace: true,
Expand All @@ -20,7 +20,7 @@ export default (clientUserConfigurationInput?: ClientConfigurationInputType): Cl
idleInTransactionSessionTimeout: 60000,
idleTimeout: 5000,

// $FlowFixMe
// @ts-ignore
interceptors: [],

maximumPoolSize: 10,
Expand All @@ -30,7 +30,7 @@ export default (clientUserConfigurationInput?: ClientConfigurationInputType): Cl
transactionRetryLimit: 5,
typeParsers,

// $FlowFixMe
// @ts-ignore
...clientUserConfigurationInput,
};

Expand All @@ -39,7 +39,7 @@ export default (clientUserConfigurationInput?: ClientConfigurationInputType): Cl
}

if (!configuration.typeParsers || configuration.typeParsers === typeParsers) {
// $FlowFixMe
// @ts-ignore
configuration.typeParsers = createTypeParserPreset();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ type ConnectionHandlerType = (
connection: InternalDatabaseConnectionType,
boundConnection: DatabasePoolConnectionType,
clientConfiguration: ClientConfigurationType
) => MaybePromiseType<*>;
) => MaybePromiseType<unknown>;

type PoolHandlerType = (pool: DatabasePoolType) => Promise<*>;
type PoolHandlerType = (pool: DatabasePoolType) => Promise<unknown>;

const terminatePoolConnection = (pool, connection, error) => {
if (!connection.connection.slonik.terminated) {
Expand All @@ -55,7 +55,7 @@ const createConnection = async (
connectionType: ConnectionTypeType,
connectionHandler: ConnectionHandlerType,
poolHandler: PoolHandlerType,
query?: TaggedTemplateLiteralInvocationType | null = null,
query: TaggedTemplateLiteralInvocationType | null = null,
) => {
if (pool.slonik.ended) {
throw new UnexpectedStateError('Connection pool shutdown has been already initiated. Cannot create a new connection.');
Expand All @@ -79,6 +79,7 @@ const createConnection = async (

let remainingConnectionRetryLimit = clientConfiguration.connectionRetryLimit;

// eslint-disable-next-line no-constant-condition
while (true) {
remainingConnectionRetryLimit--;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import {
} from '../utilities';
import createClientConfiguration from './createClientConfiguration';

type OverridesType = {|
+query: (sql: string, values: $ReadOnlyArray<PrimitiveValueExpressionType>,) => Promise<QueryResultType<QueryResultRowType>>,
|};
type OverridesType = {
readonly query: (sql: string, values: ReadonlyArray<PrimitiveValueExpressionType>) => Promise<QueryResultType<QueryResultRowType>>;
};

export default (
overrides: OverridesType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
QueryResultType,
} from '../types';

export default (rows: $ReadOnlyArray<QueryResultRowType>): QueryResultType<QueryResultRowType> => {
export default (rows: ReadonlyArray<QueryResultRowType>): QueryResultType<QueryResultRowType> => {
return {
command: 'SELECT',
fields: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default (

try {
/* eslint-disable global-require, import/no-unassigned-import, import/no-extraneous-dependencies */
// $FlowFixMe
// @ts-ignore
require('pg-native');
/* eslint-enable */

Expand Down
Loading

0 comments on commit 169c7b9

Please sign in to comment.