Skip to content

Commit

Permalink
Fix minor TypeScript issues and add ESLint TypeScript support (#418)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
Jerrylum authored Mar 14, 2024
1 parent 2d94982 commit 36464ca
Show file tree
Hide file tree
Showing 20 changed files with 230 additions and 273 deletions.
29 changes: 25 additions & 4 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
{
"env": {

"es6": true,
"node": true,
"mocha": true
},
"extends": "eslint:recommended",
"extends": [ "eslint:recommended", "@rushstack/eslint-config"],
"overrides": [
{
"files": ["**/test/typescript/*.ts"],
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-floating-promises": "off"
}
},
{
"files": ["**/index.d.ts"],
"rules": {
"max-len": "warn"
}
}
],
"parserOptions": {
"ecmaVersion": 9,
"sourceType": "module"
Expand All @@ -25,7 +40,7 @@
"SwitchCase": 1
}
],
"keyword-spacing":"error",
"keyword-spacing": "error",
"linebreak-style": ["error", "unix"],
"max-len": [
"error",
Expand Down Expand Up @@ -67,6 +82,12 @@
],
"space-infix-ops": ["error"],
"space-in-parens": ["error", "never"],
"yoda": "error"
"yoda": "error",
"@typescript-eslint/no-explicit-any": "warn",
"@typescript-eslint/explicit-member-accessibility": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/interface-name-prefix": "off",
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/typedef": "off"
}
}
2 changes: 1 addition & 1 deletion apis/fabric-contract-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"buildt": "tsc --project test/typescript",
"test": "nyc mocha --recursive 'test/unit/**/*.js'",
"build": "npm run lint && npm run test:unit && npm run test:schema",
"lint": "eslint ./lib/",
"lint": "eslint ./lib ./types ./test/typescript/*.ts --ext .js --ext .ts",
"test:unit": "npm run test",
"test:schema": "ajv compile -s ./schema/contract-schema.json && ajv validate -s ./schema/contract-schema.json -d ./schema/example-full.json"
},
Expand Down
112 changes: 56 additions & 56 deletions apis/fabric-contract-api/test/typescript/smartcontract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,72 +5,72 @@
*/

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

export class ScenarioContext extends Context{
customFunction(): void {
export class ScenarioContext extends Context {
customFunction(): void {

}
}
}

export default class TestContractOne extends Contract {
constructor() {
super('org.papernet.commercialpaper');
}

beforeTransaction(ctx: ScenarioContext) {
// test that the context super class properties are available
const stubApi: ChaincodeStub = ctx.stub;
const clientIdentity: ClientIdentity = ctx.clientIdentity;

// tests that the functions in the subclasses context be called
ctx.customFunction();

// This proves that typescript is enforcing the
// return type of Promise<void>
return Promise.resolve();
}

afterTransaction(ctx: ScenarioContext,result: any) {
// This proves that typescript is enforcing the
// return type of Promise<void>
return Promise.resolve();
}

aroundTransaction(ctx: ScenarioContext, fn: Function, parameters: any) {
// This proves that typescript is enforcing the
// return type of Promise<void>
return super.aroundTransaction(ctx, fn, parameters);
}

unknownTransaction(ctx: ScenarioContext){
// This proves that typescript is enforcing the
// return type of Promise<void>
return Promise.resolve();
}

createContext(){
return new ScenarioContext();
}

async Transaction(ctx: ScenarioContext) {
// test that the context super class properties are available
const stubApi: ChaincodeStub = ctx.stub;
const clientIdentity: ClientIdentity = ctx.clientIdentity;

// test that the name returns a string
const ns: string = this.getName();
}
beforeTransaction(ctx: ScenarioContext) {
// test that the context super class properties are available
const stubApi: ChaincodeStub = ctx.stub;
const clientIdentity: ClientIdentity = ctx.clientIdentity;

// tests that the functions in the subclasses context be called
ctx.customFunction();

// This proves that typescript is enforcing the
// return type of Promise<void>
return Promise.resolve();
}

afterTransaction(ctx: ScenarioContext, result: any) {
// This proves that typescript is enforcing the
// return type of Promise<void>
return Promise.resolve();
}

aroundTransaction(ctx: ScenarioContext, fn: Function, parameters: any) {
// This proves that typescript is enforcing the
// return type of Promise<void>
return super.aroundTransaction(ctx, fn, parameters);
}

unknownTransaction(ctx: ScenarioContext) {
// This proves that typescript is enforcing the
// return type of Promise<void>
return Promise.resolve();
}

createContext() {
return new ScenarioContext();
}

async Transaction(ctx: ScenarioContext) {
// test that the context super class properties are available
const stubApi: ChaincodeStub = ctx.stub;
const clientIdentity: ClientIdentity = ctx.clientIdentity;

// test that the name returns a string
const ns: string = this.getName();
}
}

export class TestContractTwo extends Contract {
constructor() {
super();
}

async Transaction(ctx: Context) {
const stubApi: ChaincodeStub = ctx.stub;
const clientIdentity: ClientIdentity = ctx.clientIdentity;
}
constructor() {
super();
}

async Transaction(ctx: Context) {
const stubApi: ChaincodeStub = ctx.stub;
const clientIdentity: ClientIdentity = ctx.clientIdentity;
}
}
19 changes: 0 additions & 19 deletions apis/fabric-contract-api/test/typescript/tsconfig.json

This file was deleted.

26 changes: 26 additions & 0 deletions apis/fabric-contract-api/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2020 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
{
"compilerOptions": {
"types": ["./types/"],
"alwaysStrict": true,
"module": "commonjs",
"declaration": true,
"sourceMap": true,
"strict": true,
"target": "es2017",
"lib": [
"esnext",
]
},
"include": [
"lib/**/*",
"test/**/*"
],
"exclude": [
"node_modules/**/*"
]
}
8 changes: 4 additions & 4 deletions apis/fabric-contract-api/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

declare module 'fabric-contract-api' {
import { Logger } from 'winston';
import { ChaincodeStub, ClientIdentity } from 'fabric-shim-api';
import {Logger} from 'winston';
import {ChaincodeStub, ClientIdentity} from 'fabric-shim-api';

export class Context {
stub: ChaincodeStub;
Expand Down Expand Up @@ -36,8 +36,8 @@ declare module 'fabric-contract-api' {


export class JSONSerializer {
toBuffer(result: any,schema:any,loggerPrefix?:string): Buffer;
fromBuffer(data: Buffer,schema:any,loggerPrefix?:string): any;
toBuffer(result: any, schema:any, loggerPrefix?:string): Buffer;
fromBuffer(data: Buffer, schema:any, loggerPrefix?:string): any;
}

export function Transaction(commit?: boolean): (target: any, propertyKey: string | symbol) => void;
Expand Down
34 changes: 0 additions & 34 deletions apis/fabric-contract-api/types/tsconfig.json

This file was deleted.

4 changes: 3 additions & 1 deletion apis/fabric-shim-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"url": "https://github.com/hyperledger/fabric-chaincode-node"
},
"scripts": {
"lint": "eslint ./types/ --ext .ts",
"build": "echo No Build needed"
},
"keywords": [
Expand All @@ -23,6 +24,7 @@
"types": "./types/index.d.ts",
"license": "Apache-2.0",
"devDependencies": {
"@types/long": "^4.0.1"
"@types/long": "^4.0.1",
"eslint": "^6.6.0"
}
}
25 changes: 25 additions & 0 deletions apis/fabric-shim-api/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2020 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
{
"compilerOptions": {
"types": ["./types/"],
"alwaysStrict": true,
"module": "commonjs",
"declaration": true,
"sourceMap": true,
"strict": true,
"target": "es2017",
"lib": [
"esnext",
]
},
"files": [
"types/index.d.ts"
],
"exclude": [
"node_modules/**/*"
]
}
6 changes: 3 additions & 3 deletions apis/fabric-shim-api/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
declare module 'fabric-shim-api' {
import Long = require("long");
import Long = require('long');

interface Timestamp {
seconds: Long;
Expand All @@ -23,12 +23,12 @@ declare module 'fabric-shim-api' {
message?: string;
payload: Uint8Array;
}

interface ErrorResponse {
status: RESPONSE_CODE.ERROR;
message: string;
payload?: Uint8Array;
}
}

interface ClientIdentity {
assertAttributeValue(attrName: string, attrValue: string): boolean;
Expand Down
37 changes: 0 additions & 37 deletions apis/fabric-shim-api/types/tsconfig.json

This file was deleted.

Loading

0 comments on commit 36464ca

Please sign in to comment.