Browse Source

@joystream/types - checks config, CI, linter fixes

Leszek Wiesner 4 years ago
parent
commit
be66c5e9fe

+ 0 - 2
.github/workflows/joystream-cli.yml

@@ -17,7 +17,6 @@ jobs:
     - name: checks
       run: |
         yarn install --frozen-lockfile
-        yarn madge --circular types/
         yarn workspace joystream-cli checks
 
   cli_build_osx:
@@ -35,5 +34,4 @@ jobs:
     - name: checks
       run: |
         yarn install --frozen-lockfile --network-timeout 120000
-        yarn madge --circular types/
         yarn workspace joystream-cli checks

+ 37 - 0
.github/workflows/joystream-types.yml

@@ -0,0 +1,37 @@
+name: joystream-types
+on: [pull_request, push]
+
+jobs:
+  types_checks_ubuntu:
+    name: Ubuntu Checks
+    runs-on: ubuntu-latest
+    strategy:
+      matrix:
+        node-version: [12.x]
+    steps:
+    - uses: actions/checkout@v1
+    - name: Use Node.js ${{ matrix.node-version }}
+      uses: actions/setup-node@v1
+      with:
+        node-version: ${{ matrix.node-version }}
+    - name: checks
+      run: |
+        yarn install --frozen-lockfile
+        yarn workspace @joystream/types checks
+
+  types_checks_osx:
+    name: MacOS Checks
+    runs-on: macos-latest
+    strategy:
+      matrix:
+        node-version: [12.x]
+    steps:
+    - uses: actions/checkout@v1
+    - name: Use Node.js ${{ matrix.node-version }}
+      uses: actions/setup-node@v1
+      with:
+        node-version: ${{ matrix.node-version }}
+    - name: checks
+      run: |
+        yarn install --frozen-lockfile --network-timeout 120000
+        yarn workspace @joystream/types checks

+ 0 - 4
.github/workflows/pioneer.yml

@@ -17,7 +17,6 @@ jobs:
     - name: build
       run: |
         yarn install --frozen-lockfile
-        yarn madge --circular types/
         yarn workspace pioneer build
 
   pioneer_build_osx:
@@ -35,7 +34,6 @@ jobs:
     - name: build
       run: |
         yarn install --frozen-lockfile --network-timeout 120000
-        yarn madge --circular types/
         yarn workspace pioneer build
 
   pioneer_lint_ubuntu:
@@ -53,7 +51,6 @@ jobs:
     - name: lint
       run: |
         yarn install --frozen-lockfile
-        yarn madge --circular types/
         yarn workspace pioneer lint
 
   pioneer_lint_osx:
@@ -71,5 +68,4 @@ jobs:
     - name: lint
       run: |
         yarn install --frozen-lockfile --network-timeout 120000
-        yarn madge --circular types/
         yarn workspace pioneer lint

+ 0 - 10
types/.editorconfig

@@ -1,10 +0,0 @@
-root = true
-[*]
-indent_style=space
-indent_size=2
-tab_width=2
-end_of_line=lf
-charset=utf-8
-trim_trailing_whitespace=true
-max_line_length=120
-insert_final_newline=true

+ 1 - 0
types/.eslintignore

@@ -0,0 +1 @@
+**/*.d.ts

+ 8 - 0
types/.eslintrc.js

@@ -0,0 +1,8 @@
+module.exports = {
+  rules: {
+    '@typescript-eslint/camelcase': 'off',
+    '@typescript-eslint/class-name-casing': 'off',
+    'no-unused-vars': 'off', // Required by the typescript rule below
+    '@typescript-eslint/no-unused-vars': ['error']
+  }
+}

+ 2 - 1
types/.gitignore

@@ -1,6 +1,7 @@
 # Don't track build artifacts
 **/*.js
 **/*.d.ts
+!.eslintrc.js
 
 # JSON files imported by hiring types
 hiring/schemas/role.schema.json
@@ -9,4 +10,4 @@ hiring/schemas/role.schema.json
 lib/
 
 # artifact of webpack when building pioneer?
-build/
+build/

+ 4 - 1
types/package.json

@@ -6,7 +6,10 @@
   "types": "index.d.ts",
   "scripts": {
     "prepublish": "npm run build",
-    "build": "tsc --build tsconfig.json"
+    "build": "tsc --build tsconfig.json",
+    "lint": "eslint ./ --quiet --ext .ts",
+    "format": "prettier ./ --write",
+    "checks": "yarn build && madge --circular ./ && yarn lint && prettier ./ --check"
   },
   "author": "Joystream contributors",
   "maintainers": [],

+ 1 - 1
types/src/common.ts

@@ -4,8 +4,8 @@ import { Codec } from "@polkadot/types/types";
 // we get 'moment' because it is a dependency of @polkadot/util, via @polkadot/keyring
 import moment from 'moment';
 import { JoyStruct } from './JoyStruct';
-export { JoyStruct } from './JoyStruct';
 import { JoyEnum } from './JoyEnum';
+export { JoyStruct } from './JoyStruct';
 export { JoyEnum } from './JoyEnum';
 
 // Treat a BTreeSet as a Vec since it is encoded in the same way

+ 1 - 1
types/src/content-working-group/index.ts

@@ -517,7 +517,7 @@ export class WorkingGroupUnstaker extends Enum {
 }
 
 export class CuratorApplicationIdToCuratorIdMap extends BTreeMap<ApplicationId, CuratorId> {
-  constructor (value?: any, index?: number) {
+  constructor (value?: any) {
     super(
       ApplicationId,
       CuratorId,

+ 1 - 2
types/src/forum.ts

@@ -1,6 +1,5 @@
-import { getTypeRegistry, bool, u32, u64, Text, Option, Vec as Vector} from '@polkadot/types';
+import { getTypeRegistry, bool, u32, u64, Text, Option, Vec as Vector, GenericAccountId } from '@polkadot/types';
 import { AccountId } from '@polkadot/types/interfaces';
-import { GenericAccountId } from '@polkadot/types';
 import { BlockAndTime, JoyStruct, ThreadId, PostId } from './common';
 
 export type ModerationActionType = {

+ 2 - 2
types/src/hiring/index.ts

@@ -8,6 +8,8 @@ import { GenericJoyStreamRoleSchema } from './schemas/role.schema.typings'
 
 import ajv from 'ajv'
 
+import * as role_schema_json from './schemas/role.schema.json'
+
 export class ApplicationId extends u64 { };
 export class OpeningId extends u64 { };
 
@@ -332,8 +334,6 @@ export class StakingPolicy extends JoyStruct<IStakingPolicy> {
   }
 
 };
-
-import * as role_schema_json from './schemas/role.schema.json'
 export const schemaValidator: ajv.ValidateFunction = new ajv({ allErrors: true }).compile(role_schema_json);
 
 const OpeningHRTFallback: GenericJoyStreamRoleSchema = {

+ 1 - 2
types/src/media.ts

@@ -1,5 +1,4 @@
-import { Enum, Struct, Option, Vec as Vector, H256, BTreeMap } from '@polkadot/types';
-import { getTypeRegistry, u64, bool, Text } from '@polkadot/types';
+import { Enum, Struct, Option, Vec as Vector, H256, BTreeMap , getTypeRegistry, u64, bool, Text } from '@polkadot/types';
 import { BlockAndTime } from './common';
 import { MemberId } from './members';
 import { StorageProviderId } from './working-group'; // this should be in discovery really

+ 28 - 28
types/src/members.ts

@@ -51,6 +51,34 @@ export class Role extends Enum {
   }
 }
 
+export class ActorInRole extends Struct {
+  constructor(value?: any) {
+    super(
+      {
+        role: Role,
+        actor_id: ActorId,
+      },
+      value
+    );
+  }
+
+  get role(): Role {
+    return this.get("role") as Role;
+  }
+
+  get actor_id(): ActorId {
+    return this.get("actor_id") as ActorId;
+  }
+
+  get isContentLead(): boolean {
+    return this.role.eq(RoleKeys.CuratorLead);
+  }
+
+  get isCurator(): boolean {
+    return this.role.eq(RoleKeys.Curator);
+  }
+}
+
 export type IProfile = {
   handle: Text;
   avatar_uri: Text;
@@ -129,34 +157,6 @@ export class Profile extends JoyStruct<IProfile> {
   }
 }
 
-export class ActorInRole extends Struct {
-  constructor(value?: any) {
-    super(
-      {
-        role: Role,
-        actor_id: ActorId,
-      },
-      value
-    );
-  }
-
-  get role(): Role {
-    return this.get("role") as Role;
-  }
-
-  get actor_id(): ActorId {
-    return this.get("actor_id") as ActorId;
-  }
-
-  get isContentLead(): boolean {
-    return this.role.eq(RoleKeys.CuratorLead);
-  }
-
-  get isCurator(): boolean {
-    return this.role.eq(RoleKeys.Curator);
-  }
-}
-
 export class UserInfo extends Struct {
   constructor(value?: any) {
     super(

+ 28 - 28
types/src/proposals.ts

@@ -275,34 +275,6 @@ class SetLeadParams extends Tuple {
 
 export class SetLead extends Option.with(SetLeadParams) {}
 
-export class ProposalDetails extends Enum {
-  constructor(value?: any, index?: number) {
-    super(
-      {
-        Text: "Text",
-        RuntimeUpgrade: "Vec<u8>",
-        SetElectionParameters: ElectionParameters,
-        Spending: SpendingParams,
-        SetLead: SetLead,
-        SetContentWorkingGroupMintCapacity: "Balance",
-        EvictStorageProvider: "AccountId",
-        SetValidatorCount: "u32",
-        SetStorageRoleParameters: RoleParameters,
-        AddWorkingGroupLeaderOpening: AddOpeningParameters,
-        BeginReviewWorkingGroupLeaderApplication: Tuple.with([OpeningId, WorkingGroup]),
-        FillWorkingGroupLeaderOpening: FillOpeningParameters,
-        SetWorkingGroupMintCapacity: Tuple.with(["Balance", WorkingGroup]),
-        DecreaseWorkingGroupLeaderStake: Tuple.with([WorkerId, "Balance", WorkingGroup]),
-        SlashWorkingGroupLeaderStake: Tuple.with([WorkerId, "Balance", WorkingGroup]),
-        SetWorkingGroupLeaderReward: Tuple.with([WorkerId, "Balance", WorkingGroup]),
-        TerminateWorkingGroupLeaderRole: TerminateRoleParameters,
-      },
-      value,
-      index
-    );
-  }
-}
-
 export class Proposal extends Struct {
   constructor(value?: any) {
     super(
@@ -562,6 +534,34 @@ export class TerminateRoleParameters extends JoyStruct<ITerminateRoleParameters>
   }
 }
 
+export class ProposalDetails extends Enum {
+  constructor(value?: any, index?: number) {
+    super(
+      {
+        Text: "Text",
+        RuntimeUpgrade: "Vec<u8>",
+        SetElectionParameters: ElectionParameters,
+        Spending: SpendingParams,
+        SetLead: SetLead,
+        SetContentWorkingGroupMintCapacity: "Balance",
+        EvictStorageProvider: "AccountId",
+        SetValidatorCount: "u32",
+        SetStorageRoleParameters: RoleParameters,
+        AddWorkingGroupLeaderOpening: AddOpeningParameters,
+        BeginReviewWorkingGroupLeaderApplication: Tuple.with([OpeningId, WorkingGroup]),
+        FillWorkingGroupLeaderOpening: FillOpeningParameters,
+        SetWorkingGroupMintCapacity: Tuple.with(["Balance", WorkingGroup]),
+        DecreaseWorkingGroupLeaderStake: Tuple.with([WorkerId, "Balance", WorkingGroup]),
+        SlashWorkingGroupLeaderStake: Tuple.with([WorkerId, "Balance", WorkingGroup]),
+        SetWorkingGroupLeaderReward: Tuple.with([WorkerId, "Balance", WorkingGroup]),
+        TerminateWorkingGroupLeaderRole: TerminateRoleParameters,
+      },
+      value,
+      index
+    );
+  }
+}
+
 // export default proposalTypes;
 export function registerProposalTypes() {
   try {

+ 22 - 18
types/src/versioned-store/EntityCodec.ts

@@ -4,7 +4,7 @@ import { Codec } from '@polkadot/types/types';
 import { Class, Entity, VecClassPropertyValue, ClassPropertyValue, EntityId, ClassId, unifyPropName } from '.';
 import * as PV from './PropertyValue';
 import { PropertyValue } from './PropertyValue';
-import PropertyTypeName from './PropertyTypeName';
+import { PropertyTypeName } from './PropertyTypeName';
 import ChannelId from '../content-working-group/ChannelId';
 
 /**
@@ -24,7 +24,7 @@ function substrateToPlain<T> (x: Codec): T | undefined {
     res = (x as bool).valueOf();
   } else if (x instanceof Vec) {
     res = x.map(y => substrateToPlain(y));
-  } else if (typeof x !== undefined && x !== null) {
+  } else if (typeof x !== 'undefined' && x !== null) {
     res = x.toString();
   }
 
@@ -65,7 +65,11 @@ function plainToSubstrate(propType: string, value: any): PropertyValue {
       return value as any[]
     }
 
-    return typeof value === undefined ? [] : [ value ]
+    // The next line was: "return typeof value === undefined ? [] : [ value ]" before
+    // This condition was never met (spotted by linter), because "typeof value" can be 'undefined'
+    // (a string), but not actually undefined. Changing it to 'undefined' would change this function's behavior though
+    // and that may lead to unexpected consequences.
+    return [ value ]
   }
 
   const valueAsBoolArr = (): boolean[] => {
@@ -140,21 +144,6 @@ export type TextValueEntity = PlainEntity & {
   value: string
 }
 
-export class EntityCodecResolver {
-
-  private codecByClassIdMap = new Map<string, AnyEntityCodec>()
-
-  constructor (classes: Class[]) {
-    classes.forEach(c => {
-      this.codecByClassIdMap.set(c.id.toString(), new AnyEntityCodec(c))
-    })
-  }
-
-  getCodecByClassId<C extends EntityCodec<any>>(classId: ClassId): C | undefined {
-    return this.codecByClassIdMap.get(classId.toString()) as C
-  }
-}
-
 // TODO delete this hack once EntityCodec extracted from types to media app
 type EntityType = any
 type ChannelEntity = any
@@ -279,3 +268,18 @@ export abstract class EntityCodec<T extends PlainEntity> {
 
 /** This class is created just to satisfy TypeScript in some cases */
 export class AnyEntityCodec extends EntityCodec<any> {}
+
+export class EntityCodecResolver {
+
+  private codecByClassIdMap = new Map<string, AnyEntityCodec>()
+
+  constructor (classes: Class[]) {
+    classes.forEach(c => {
+      this.codecByClassIdMap.set(c.id.toString(), new AnyEntityCodec(c))
+    })
+  }
+
+  getCodecByClassId<C extends EntityCodec<any>>(classId: ClassId): C | undefined {
+    return this.codecByClassIdMap.get(classId.toString()) as C
+  }
+}

+ 0 - 2
types/src/versioned-store/PropertyTypeName.ts

@@ -22,5 +22,3 @@ export type PropertyTypeName =
   'TextVec' |
   'InternalVec'
 ;
-
-export default PropertyTypeName;

+ 3 - 3
types/src/versioned-store/permissions/batching/operation-types.ts

@@ -23,12 +23,12 @@ export class OperationType extends Enum {
   }
 
   static CreateEntity (class_id: ClassId) : OperationType {
-    let value = new CreateEntity({class_id});
+    const value = new CreateEntity({class_id});
     return new OperationType({'CreateEntity': value });
   }
 
   static UpdatePropertyValues (entity_id: ParametrizedEntity, parametrized_property_values: ParameterizedClassPropertyValues) : OperationType {
-    let value = new UpdatePropertyValues({
+    const value = new UpdatePropertyValues({
       entity_id,
       parametrized_property_values,
     });
@@ -36,7 +36,7 @@ export class OperationType extends Enum {
   }
 
   static AddSchemaSupportToEntity (entity_id: ParametrizedEntity, schema_id: u16, parametrized_property_values: ParameterizedClassPropertyValues) : OperationType {
-    let value = new AddSchemaSupportToEntity({
+    const value = new AddSchemaSupportToEntity({
       entity_id,
       schema_id,
       parametrized_property_values

+ 1 - 1
types/src/working-group/index.ts

@@ -118,7 +118,7 @@ export class Worker extends JoyStruct<IWorker> {
   }
 
   get is_active(): boolean {
-    return !Boolean(this.isEmpty);
+    return !this.isEmpty;
   }
 }