[js/web] fix typescript type check (#18343)

### Description

This PR fixes the TypeScript type check.

Previously, when I use esbuild to replace webpack (#17745), typescript
typecheck was disabled. This causes a few TypeScript type error checked
in into the code base. This PR fixes the followings:

- Use "Node16" as default "module" value in tsconfig.json, because in
TypeScript v5, `(module == "ES2015" && moduleResolution == "Node16")` is
an invalid combination.
- Set `noUnusedParameters` to true as default. in web override it to
false because multiple code need to be updated ( a following-up PR will
do this )
- set correct project file for 'web/lib/**/*.ts' for ESLint (otherwise
WebGPU types are not populated correctly)
- fix type error in file js/web/lib/wasm/jsep/webgpu/program-manager.ts
- upgrade "@webgpu/types" to latest to fix type error in file
js/web/lib/wasm/jsep/backend-webgpu.ts
- add package script "prebuild" for web to run tsc type check
- add type check in CI yml file
This commit is contained in:
Yulong Wang 2023-11-10 16:03:38 -08:00 committed by GitHub
parent 8dba6efd61
commit 6b0c97b43f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 29 additions and 28 deletions

View file

@ -144,7 +144,9 @@ module.exports = {
'no-unused-expressions': 'off', 'no-unused-expressions': 'off',
} }
}, { }, {
files: ['web/lib/**/*.ts'], rules: { files: ['web/lib/**/*.ts'],
excludedFiles: 'web/lib/wasm/proxy-worker/**/*',
parserOptions: { 'project': 'web/tsconfig.json' },rules: {
'no-underscore-dangle': 'off', 'no-underscore-dangle': 'off',
} }
}, { }, {

View file

@ -4,8 +4,7 @@
"outDir": "./dist/esm", "outDir": "./dist/esm",
"declaration": true, "declaration": true,
"declarationMap": true, "declarationMap": true,
"esModuleInterop": false, "esModuleInterop": false
"noUnusedParameters": true
}, },
"include": ["lib"] "include": ["lib"]
} }

View file

@ -1,7 +1,6 @@
{ {
"extends": "../tsconfig.json", "extends": "../tsconfig.json",
"compilerOptions": { "compilerOptions": {
"module": "Node16",
"outDir": "dist" "outDir": "dist"
}, },
"include": ["lib"] "include": ["lib"]

View file

@ -1,6 +1,6 @@
{ {
"compilerOptions": { "compilerOptions": {
"module": "ES2015", "module": "Node16",
"moduleResolution": "Node16", "moduleResolution": "Node16",
"esModuleInterop": true, "esModuleInterop": true,
"target": "ES2020", "target": "ES2020",
@ -10,7 +10,7 @@
"noImplicitAny": true, "noImplicitAny": true,
"noImplicitReturns": true, "noImplicitReturns": true,
"noImplicitThis": true, "noImplicitThis": true,
"noUnusedParameters": false, "noUnusedParameters": true,
"alwaysStrict": true, "alwaysStrict": true,
"strictNullChecks": true, "strictNullChecks": true,
"pretty": true, "pretty": true,

View file

@ -1,7 +1,6 @@
{ {
"extends": "./tsconfig.json", "extends": "./tsconfig.json",
"compilerOptions": { "compilerOptions": {
"module": "Node16",
"declaration": false, "declaration": false,
"sourceMap": false "sourceMap": false
} }

View file

@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved. // Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. // Licensed under the MIT License.
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/naming-convention */ /* eslint-disable @typescript-eslint/naming-convention */
/** /**
* The interface BuildDefinitions contains a set of flags which are defined at build time. * The interface BuildDefinitions contains a set of flags which are defined at build time.

View file

@ -68,7 +68,7 @@ export class ProgramManager {
this.backend.querySetCount * 8, GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST); this.backend.querySetCount * 8, GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST);
this.backend.endComputePass(); this.backend.endComputePass();
this.backend.getCommandEncoder().resolveQuerySet(this.backend.querySet, 0, 2, this.backend.queryData.buffer, 0); this.backend.getCommandEncoder().resolveQuerySet(this.backend.querySet!, 0, 2, this.backend.queryData.buffer, 0);
this.backend.getCommandEncoder().copyBufferToBuffer( this.backend.getCommandEncoder().copyBufferToBuffer(
this.backend.queryData.buffer, 0, syncData.buffer, 0, this.backend.querySetCount * 8); this.backend.queryData.buffer, 0, syncData.buffer, 0, this.backend.querySetCount * 8);
this.backend.flush(); this.backend.flush();
@ -77,7 +77,7 @@ export class ProgramManager {
const kernelInfo = this.backend.kernels.get(kernelId)!; const kernelInfo = this.backend.kernels.get(kernelId)!;
const kernelName = `[${kernelInfo[0]}] ${kernelInfo[1]}`; const kernelName = `[${kernelInfo[0]}] ${kernelInfo[1]}`;
syncData.buffer.mapAsync(GPUMapMode.READ).then(() => { void syncData.buffer.mapAsync(GPUMapMode.READ).then(() => {
const mappedData = new BigUint64Array(syncData.buffer.getMappedRange()); const mappedData = new BigUint64Array(syncData.buffer.getMappedRange());
const startTimeU64 = mappedData[0]; const startTimeU64 = mappedData[0];
const endTimeU64 = mappedData[1]; const endTimeU64 = mappedData[1];

View file

@ -25,7 +25,7 @@
"@types/minimatch": "^5.1.2", "@types/minimatch": "^5.1.2",
"@types/minimist": "^1.2.2", "@types/minimist": "^1.2.2",
"@types/platform": "^1.3.4", "@types/platform": "^1.3.4",
"@webgpu/types": "^0.1.30", "@webgpu/types": "^0.1.38",
"base64-js": "^1.5.1", "base64-js": "^1.5.1",
"chai": "^4.3.7", "chai": "^4.3.7",
"electron": "^23.1.2", "electron": "^23.1.2",
@ -323,9 +323,9 @@
} }
}, },
"node_modules/@webgpu/types": { "node_modules/@webgpu/types": {
"version": "0.1.30", "version": "0.1.38",
"resolved": "https://registry.npmjs.org/@webgpu/types/-/types-0.1.30.tgz", "resolved": "https://registry.npmjs.org/@webgpu/types/-/types-0.1.38.tgz",
"integrity": "sha512-9AXJSmL3MzY8ZL//JjudA//q+2kBRGhLBFpkdGksWIuxrMy81nFrCzj2Am+mbh8WoU6rXmv7cY5E3rdlyru2Qg==", "integrity": "sha512-7LrhVKz2PRh+DD7+S+PVaFd5HxaWQvoMqBbsV9fNJO1pjUs1P8bM2vQVNfk+3URTqbuTI7gkXi0rfsN0IadoBA==",
"dev": true "dev": true
}, },
"node_modules/accepts": { "node_modules/accepts": {
@ -3767,9 +3767,9 @@
} }
}, },
"@webgpu/types": { "@webgpu/types": {
"version": "0.1.30", "version": "0.1.38",
"resolved": "https://registry.npmjs.org/@webgpu/types/-/types-0.1.30.tgz", "resolved": "https://registry.npmjs.org/@webgpu/types/-/types-0.1.38.tgz",
"integrity": "sha512-9AXJSmL3MzY8ZL//JjudA//q+2kBRGhLBFpkdGksWIuxrMy81nFrCzj2Am+mbh8WoU6rXmv7cY5E3rdlyru2Qg==", "integrity": "sha512-7LrhVKz2PRh+DD7+S+PVaFd5HxaWQvoMqBbsV9fNJO1pjUs1P8bM2vQVNfk+3URTqbuTI7gkXi0rfsN0IadoBA==",
"dev": true "dev": true
}, },
"accepts": { "accepts": {

View file

@ -24,6 +24,7 @@
"build:doc": "node ./script/generate-webgl-operator-md && node ./script/generate-webgpu-operator-md", "build:doc": "node ./script/generate-webgl-operator-md && node ./script/generate-webgpu-operator-md",
"pull:wasm": "node ./script/pull-prebuilt-wasm-artifacts", "pull:wasm": "node ./script/pull-prebuilt-wasm-artifacts",
"test:e2e": "node ./test/e2e/run", "test:e2e": "node ./test/e2e/run",
"prebuild": "tsc -p . --noEmit",
"build": "node ./script/build", "build": "node ./script/build",
"test": "tsc --build ../scripts && node ../scripts/prepare-onnx-node-tests && node ./script/test-runner-cli", "test": "tsc --build ../scripts && node ../scripts/prepare-onnx-node-tests && node ./script/test-runner-cli",
"prepack": "node ./script/build && node ./script/prepack" "prepack": "node ./script/build && node ./script/prepack"
@ -42,7 +43,7 @@
"@types/minimatch": "^5.1.2", "@types/minimatch": "^5.1.2",
"@types/minimist": "^1.2.2", "@types/minimist": "^1.2.2",
"@types/platform": "^1.3.4", "@types/platform": "^1.3.4",
"@webgpu/types": "^0.1.30", "@webgpu/types": "^0.1.38",
"base64-js": "^1.5.1", "base64-js": "^1.5.1",
"chai": "^4.3.7", "chai": "^4.3.7",
"electron": "^23.1.2", "electron": "^23.1.2",

View file

@ -1,6 +1,7 @@
{ {
"extends": "../../tsconfig.tools.json", "extends": "../../tsconfig.tools.json",
"compilerOptions": { "compilerOptions": {
"sourceMap": true "sourceMap": true,
"noUnusedParameters": false
} }
} }

View file

@ -1,9 +1,9 @@
{ {
"extends": "../tsconfig.json", "extends": "../tsconfig.json",
"compilerOptions": { "compilerOptions": {
"module": "Node16",
"downlevelIteration": true, "downlevelIteration": true,
"declaration": true, "declaration": true,
"noUnusedParameters": false,
"typeRoots": ["./node_modules/@webgpu/types", "./node_modules/@types", "../node_modules/@types"] "typeRoots": ["./node_modules/@webgpu/types", "./node_modules/@types", "../node_modules/@types"]
}, },
"include": ["lib", "test"], "include": ["lib", "test"],

View file

@ -3,10 +3,6 @@ steps:
npm ci npm ci
workingDirectory: '$(Build.SourcesDirectory)/js' workingDirectory: '$(Build.SourcesDirectory)/js'
displayName: 'npm ci /js/' displayName: 'npm ci /js/'
- script: |
npm run lint
workingDirectory: '$(Build.SourcesDirectory)/js'
displayName: 'run ESLint without TS type populated'
- script: | - script: |
npm ci npm ci
workingDirectory: '$(Build.SourcesDirectory)/js/common' workingDirectory: '$(Build.SourcesDirectory)/js/common'
@ -19,6 +15,10 @@ steps:
npm ci npm ci
workingDirectory: '$(Build.SourcesDirectory)/js/web' workingDirectory: '$(Build.SourcesDirectory)/js/web'
displayName: 'npm ci /js/web/' displayName: 'npm ci /js/web/'
- script: |
npm run prebuild
workingDirectory: '$(Build.SourcesDirectory)/js/web'
displayName: 'run TypeScript type check in /js/web/'
- script: | - script: |
npm run lint npm run lint
workingDirectory: '$(Build.SourcesDirectory)/js' workingDirectory: '$(Build.SourcesDirectory)/js'

View file

@ -99,10 +99,6 @@ jobs:
npm ci npm ci
workingDirectory: '$(Build.SourcesDirectory)\js' workingDirectory: '$(Build.SourcesDirectory)\js'
displayName: 'npm ci /js/' displayName: 'npm ci /js/'
- script: |
npm run lint
workingDirectory: '$(Build.SourcesDirectory)\js'
displayName: 'run ESLint without TS type populated'
- script: | - script: |
npm ci npm ci
workingDirectory: '$(Build.SourcesDirectory)\js\common' workingDirectory: '$(Build.SourcesDirectory)\js\common'
@ -115,6 +111,10 @@ jobs:
npm ci npm ci
workingDirectory: '$(Build.SourcesDirectory)\js\web' workingDirectory: '$(Build.SourcesDirectory)\js\web'
displayName: 'npm ci /js/web/' displayName: 'npm ci /js/web/'
- script: |
npm run prebuild
workingDirectory: '$(Build.SourcesDirectory)\js\web'
displayName: 'run TypeScript type check in /js/web/'
- script: | - script: |
npm run lint npm run lint
workingDirectory: '$(Build.SourcesDirectory)\js' workingDirectory: '$(Build.SourcesDirectory)\js'