Skip to content

Commit 36464ca

Browse files
authored
Fix minor TypeScript issues and add ESLint TypeScript support (#418)
* Fix "Cannot find module" errors in TypeScript test files * Add "../../types/" as type package to **/test/typescript/tsconfig.json * Add ESLint TypeScript support * Remove directory tsconfig.json files * Add project root tsconfig.json files * Add eslint dev dependencies to fabric-shim-api project * Modify lint script in package.json files to check TypeScript files * Fix fabric-content-api project code format * Set max-len rule to warning for index.d.ts files * Fix fabric-shim-api project code format * Suppress some TypeScript ESLint rules globally * Fix fabric-shim project code format Signed-off-by: Jerrylum <[email protected]>
1 parent 2d94982 commit 36464ca

File tree

20 files changed

+230
-273
lines changed

20 files changed

+230
-273
lines changed

.eslintrc

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
{
22
"env": {
3-
43
"es6": true,
54
"node": true,
65
"mocha": true
76
},
8-
"extends": "eslint:recommended",
7+
"extends": [ "eslint:recommended", "@rushstack/eslint-config"],
8+
"overrides": [
9+
{
10+
"files": ["**/test/typescript/*.ts"],
11+
"rules": {
12+
"no-unused-vars": "off",
13+
"@typescript-eslint/no-unused-vars": "off",
14+
"@typescript-eslint/no-floating-promises": "off"
15+
}
16+
},
17+
{
18+
"files": ["**/index.d.ts"],
19+
"rules": {
20+
"max-len": "warn"
21+
}
22+
}
23+
],
924
"parserOptions": {
1025
"ecmaVersion": 9,
1126
"sourceType": "module"
@@ -25,7 +40,7 @@
2540
"SwitchCase": 1
2641
}
2742
],
28-
"keyword-spacing":"error",
43+
"keyword-spacing": "error",
2944
"linebreak-style": ["error", "unix"],
3045
"max-len": [
3146
"error",
@@ -67,6 +82,12 @@
6782
],
6883
"space-infix-ops": ["error"],
6984
"space-in-parens": ["error", "never"],
70-
"yoda": "error"
85+
"yoda": "error",
86+
"@typescript-eslint/no-explicit-any": "warn",
87+
"@typescript-eslint/explicit-member-accessibility": "off",
88+
"@typescript-eslint/explicit-function-return-type": "off",
89+
"@typescript-eslint/interface-name-prefix": "off",
90+
"@typescript-eslint/no-namespace": "off",
91+
"@typescript-eslint/typedef": "off"
7192
}
7293
}

apis/fabric-contract-api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"buildt": "tsc --project test/typescript",
1313
"test": "nyc mocha --recursive 'test/unit/**/*.js'",
1414
"build": "npm run lint && npm run test:unit && npm run test:schema",
15-
"lint": "eslint ./lib/",
15+
"lint": "eslint ./lib ./types ./test/typescript/*.ts --ext .js --ext .ts",
1616
"test:unit": "npm run test",
1717
"test:schema": "ajv compile -s ./schema/contract-schema.json && ajv validate -s ./schema/contract-schema.json -d ./schema/example-full.json"
1818
},

apis/fabric-contract-api/test/typescript/smartcontract.ts

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,72 +5,72 @@
55
66
*/
77

8-
import { Contract, Context } from 'fabric-contract-api';
9-
import { ChaincodeStub, ClientIdentity } from 'fabric-shim-api';
8+
import {Contract, Context} from 'fabric-contract-api';
9+
import {ChaincodeStub, ClientIdentity} from 'fabric-shim-api';
1010

11-
export class ScenarioContext extends Context{
12-
customFunction(): void {
11+
export class ScenarioContext extends Context {
12+
customFunction(): void {
1313

14-
}
14+
}
1515
}
1616

1717
export default class TestContractOne extends Contract {
1818
constructor() {
1919
super('org.papernet.commercialpaper');
2020
}
2121

22-
beforeTransaction(ctx: ScenarioContext) {
23-
// test that the context super class properties are available
24-
const stubApi: ChaincodeStub = ctx.stub;
25-
const clientIdentity: ClientIdentity = ctx.clientIdentity;
26-
27-
// tests that the functions in the subclasses context be called
28-
ctx.customFunction();
29-
30-
// This proves that typescript is enforcing the
31-
// return type of Promise<void>
32-
return Promise.resolve();
33-
}
34-
35-
afterTransaction(ctx: ScenarioContext,result: any) {
36-
// This proves that typescript is enforcing the
37-
// return type of Promise<void>
38-
return Promise.resolve();
39-
}
40-
41-
aroundTransaction(ctx: ScenarioContext, fn: Function, parameters: any) {
42-
// This proves that typescript is enforcing the
43-
// return type of Promise<void>
44-
return super.aroundTransaction(ctx, fn, parameters);
45-
}
46-
47-
unknownTransaction(ctx: ScenarioContext){
48-
// This proves that typescript is enforcing the
49-
// return type of Promise<void>
50-
return Promise.resolve();
51-
}
52-
53-
createContext(){
54-
return new ScenarioContext();
55-
}
56-
57-
async Transaction(ctx: ScenarioContext) {
58-
// test that the context super class properties are available
59-
const stubApi: ChaincodeStub = ctx.stub;
60-
const clientIdentity: ClientIdentity = ctx.clientIdentity;
61-
62-
// test that the name returns a string
63-
const ns: string = this.getName();
64-
}
22+
beforeTransaction(ctx: ScenarioContext) {
23+
// test that the context super class properties are available
24+
const stubApi: ChaincodeStub = ctx.stub;
25+
const clientIdentity: ClientIdentity = ctx.clientIdentity;
26+
27+
// tests that the functions in the subclasses context be called
28+
ctx.customFunction();
29+
30+
// This proves that typescript is enforcing the
31+
// return type of Promise<void>
32+
return Promise.resolve();
33+
}
34+
35+
afterTransaction(ctx: ScenarioContext, result: any) {
36+
// This proves that typescript is enforcing the
37+
// return type of Promise<void>
38+
return Promise.resolve();
39+
}
40+
41+
aroundTransaction(ctx: ScenarioContext, fn: Function, parameters: any) {
42+
// This proves that typescript is enforcing the
43+
// return type of Promise<void>
44+
return super.aroundTransaction(ctx, fn, parameters);
45+
}
46+
47+
unknownTransaction(ctx: ScenarioContext) {
48+
// This proves that typescript is enforcing the
49+
// return type of Promise<void>
50+
return Promise.resolve();
51+
}
52+
53+
createContext() {
54+
return new ScenarioContext();
55+
}
56+
57+
async Transaction(ctx: ScenarioContext) {
58+
// test that the context super class properties are available
59+
const stubApi: ChaincodeStub = ctx.stub;
60+
const clientIdentity: ClientIdentity = ctx.clientIdentity;
61+
62+
// test that the name returns a string
63+
const ns: string = this.getName();
64+
}
6565
}
6666

6767
export class TestContractTwo extends Contract {
68-
constructor() {
69-
super();
70-
}
71-
72-
async Transaction(ctx: Context) {
73-
const stubApi: ChaincodeStub = ctx.stub;
74-
const clientIdentity: ClientIdentity = ctx.clientIdentity;
75-
}
68+
constructor() {
69+
super();
70+
}
71+
72+
async Transaction(ctx: Context) {
73+
const stubApi: ChaincodeStub = ctx.stub;
74+
const clientIdentity: ClientIdentity = ctx.clientIdentity;
75+
}
7676
}

apis/fabric-contract-api/test/typescript/tsconfig.json

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2020 IBM All Rights Reserved.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
{
7+
"compilerOptions": {
8+
"types": ["./types/"],
9+
"alwaysStrict": true,
10+
"module": "commonjs",
11+
"declaration": true,
12+
"sourceMap": true,
13+
"strict": true,
14+
"target": "es2017",
15+
"lib": [
16+
"esnext",
17+
]
18+
},
19+
"include": [
20+
"lib/**/*",
21+
"test/**/*"
22+
],
23+
"exclude": [
24+
"node_modules/**/*"
25+
]
26+
}

apis/fabric-contract-api/types/index.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
*/
77

88
declare module 'fabric-contract-api' {
9-
import { Logger } from 'winston';
10-
import { ChaincodeStub, ClientIdentity } from 'fabric-shim-api';
9+
import {Logger} from 'winston';
10+
import {ChaincodeStub, ClientIdentity} from 'fabric-shim-api';
1111

1212
export class Context {
1313
stub: ChaincodeStub;
@@ -36,8 +36,8 @@ declare module 'fabric-contract-api' {
3636

3737

3838
export class JSONSerializer {
39-
toBuffer(result: any,schema:any,loggerPrefix?:string): Buffer;
40-
fromBuffer(data: Buffer,schema:any,loggerPrefix?:string): any;
39+
toBuffer(result: any, schema:any, loggerPrefix?:string): Buffer;
40+
fromBuffer(data: Buffer, schema:any, loggerPrefix?:string): any;
4141
}
4242

4343
export function Transaction(commit?: boolean): (target: any, propertyKey: string | symbol) => void;

apis/fabric-contract-api/types/tsconfig.json

Lines changed: 0 additions & 34 deletions
This file was deleted.

apis/fabric-shim-api/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"url": "https://github.com/hyperledger/fabric-chaincode-node"
1010
},
1111
"scripts": {
12+
"lint": "eslint ./types/ --ext .ts",
1213
"build": "echo No Build needed"
1314
},
1415
"keywords": [
@@ -23,6 +24,7 @@
2324
"types": "./types/index.d.ts",
2425
"license": "Apache-2.0",
2526
"devDependencies": {
26-
"@types/long": "^4.0.1"
27+
"@types/long": "^4.0.1",
28+
"eslint": "^6.6.0"
2729
}
2830
}

apis/fabric-shim-api/tsconfig.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2020 IBM All Rights Reserved.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
{
7+
"compilerOptions": {
8+
"types": ["./types/"],
9+
"alwaysStrict": true,
10+
"module": "commonjs",
11+
"declaration": true,
12+
"sourceMap": true,
13+
"strict": true,
14+
"target": "es2017",
15+
"lib": [
16+
"esnext",
17+
]
18+
},
19+
"files": [
20+
"types/index.d.ts"
21+
],
22+
"exclude": [
23+
"node_modules/**/*"
24+
]
25+
}

apis/fabric-shim-api/types/index.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
*/
77
declare module 'fabric-shim-api' {
8-
import Long = require("long");
8+
import Long = require('long');
99

1010
interface Timestamp {
1111
seconds: Long;
@@ -23,12 +23,12 @@ declare module 'fabric-shim-api' {
2323
message?: string;
2424
payload: Uint8Array;
2525
}
26-
26+
2727
interface ErrorResponse {
2828
status: RESPONSE_CODE.ERROR;
2929
message: string;
3030
payload?: Uint8Array;
31-
}
31+
}
3232

3333
interface ClientIdentity {
3434
assertAttributeValue(attrName: string, attrValue: string): boolean;

0 commit comments

Comments
 (0)