Move to TypeScript (#3830)
This moves our JS file to use TS instead, which allows us to use a proper linter to check the code. All related files where moved out from the root in a dedicated folder to avoid polluting the Rust environment.
This commit is contained in:
parent
7b0104b54c
commit
7892e86731
13 changed files with 120 additions and 13 deletions
11
.github/dependabot.yaml
vendored
Normal file
11
.github/dependabot.yaml
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
version: 2
|
||||
|
||||
updates:
|
||||
- package-ecosystem: npm
|
||||
directory: src/platform_impl/web/script
|
||||
schedule:
|
||||
interval: daily
|
||||
groups:
|
||||
github-actions:
|
||||
patterns:
|
||||
- '*'
|
||||
21
.github/workflows/ci.yml
vendored
21
.github/workflows/ci.yml
vendored
|
|
@ -243,9 +243,28 @@ jobs:
|
|||
log-level: error
|
||||
arguments: --all-features --target ${{ matrix.platform.target }}
|
||||
|
||||
eslint:
|
||||
name: ESLint
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
defaults:
|
||||
run:
|
||||
working-directory: ./src/platform_impl/web/script
|
||||
|
||||
steps:
|
||||
- uses: taiki-e/checkout-action@v1
|
||||
- name: Setup NPM
|
||||
run: npm install
|
||||
- name: Run ESLint
|
||||
run: npx eslint
|
||||
|
||||
swc:
|
||||
name: Minimize JavaScript
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
defaults:
|
||||
run:
|
||||
working-directory: ./src/platform_impl/web/script
|
||||
|
||||
steps:
|
||||
- uses: taiki-e/checkout-action@v1
|
||||
|
|
@ -253,7 +272,7 @@ jobs:
|
|||
run: sudo npm i -g @swc/cli
|
||||
- name: Run SWC
|
||||
run: |
|
||||
swc src/platform_impl/web/web_sys/worker.js -o src/platform_impl/web/web_sys/worker.min.js
|
||||
swc . --ignore node_modules,**/*.d.ts --only **/*.ts -d . --out-file-extension min.js
|
||||
- name: Check for diff
|
||||
run: |
|
||||
[[ -z $(git status -s) ]]
|
||||
|
|
|
|||
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -5,3 +5,6 @@ rls/
|
|||
*~
|
||||
#*#
|
||||
.DS_Store
|
||||
# NPM package used to run ESLint.
|
||||
/src/platform_impl/web/script/node_modules
|
||||
/src/platform_impl/web/script/package-lock.json
|
||||
|
|
|
|||
|
|
@ -1,6 +1,13 @@
|
|||
{
|
||||
"module": {
|
||||
"type": "es6"
|
||||
},
|
||||
"isModule": true,
|
||||
"minify": true,
|
||||
"jsc": {
|
||||
"parser": {
|
||||
"syntax": "typescript"
|
||||
},
|
||||
"target": "es2022",
|
||||
"minify": {
|
||||
"compress": {
|
||||
32
src/platform_impl/web/script/eslint.config.mjs
Normal file
32
src/platform_impl/web/script/eslint.config.mjs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import eslint from '@eslint/js'
|
||||
import tseslint from 'typescript-eslint'
|
||||
import globals from 'globals'
|
||||
|
||||
export default tseslint.config(
|
||||
{
|
||||
ignores: ['**/*.min.js', 'eslint.config.mjs'],
|
||||
},
|
||||
eslint.configs.recommended,
|
||||
...tseslint.configs.strictTypeChecked,
|
||||
...tseslint.configs.stylisticTypeChecked,
|
||||
{
|
||||
languageOptions: {
|
||||
parserOptions: {
|
||||
ecmaVersion: 'latest',
|
||||
project: ['tsconfig.json'],
|
||||
sourceType: 'module',
|
||||
},
|
||||
globals: {
|
||||
...globals.browser,
|
||||
},
|
||||
},
|
||||
rules: {
|
||||
'@typescript-eslint/no-confusing-void-expression': [
|
||||
'error',
|
||||
{
|
||||
ignoreArrowShorthand: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
}
|
||||
)
|
||||
9
src/platform_impl/web/script/package.json
Normal file
9
src/platform_impl/web/script/package.json
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9",
|
||||
"@types/eslint__js": "^8",
|
||||
"eslint": "^8",
|
||||
"typescript": "^5",
|
||||
"typescript-eslint": "^7"
|
||||
}
|
||||
}
|
||||
12
src/platform_impl/web/script/scheduler.d.ts
vendored
Normal file
12
src/platform_impl/web/script/scheduler.d.ts
vendored
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
declare global {
|
||||
// eslint-disable-next-line no-var
|
||||
var scheduler: Scheduler
|
||||
}
|
||||
|
||||
export interface Scheduler {
|
||||
postTask<T>(callback: () => T | PromiseLike<T>, options?: SchedulerPostTaskOptions): Promise<T>
|
||||
}
|
||||
|
||||
export interface SchedulerPostTaskOptions {
|
||||
delay?: number
|
||||
}
|
||||
14
src/platform_impl/web/script/tsconfig.json
Normal file
14
src/platform_impl/web/script/tsconfig.json
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "ESNext",
|
||||
"allowUnreachableCode": false,
|
||||
"allowUnusedLabels": false,
|
||||
"exactOptionalPropertyTypes": true,
|
||||
"noImplicitOverride": true,
|
||||
"noPropertyAccessFromIndexSignature": true,
|
||||
"noUncheckedIndexedAccess": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"strict": true,
|
||||
}
|
||||
}
|
||||
1
src/platform_impl/web/script/worker.min.js
vendored
Normal file
1
src/platform_impl/web/script/worker.min.js
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
onmessage=e=>{let[s,a]=e.data,l=()=>s.postMessage(void 0);"scheduler"in globalThis?globalThis.scheduler.postTask(l,{delay:a}):setTimeout(l,a)};
|
||||
10
src/platform_impl/web/script/worker.ts
Normal file
10
src/platform_impl/web/script/worker.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
onmessage = (event) => {
|
||||
const [port, timeout] = event.data as [MessagePort, number]
|
||||
const f = () => port.postMessage(undefined)
|
||||
|
||||
if ('scheduler' in globalThis) {
|
||||
void globalThis.scheduler.postTask(f, { delay: timeout })
|
||||
} else {
|
||||
setTimeout(f, timeout)
|
||||
}
|
||||
}
|
||||
|
|
@ -175,7 +175,7 @@ impl Schedule {
|
|||
F: 'static + FnMut(),
|
||||
{
|
||||
thread_local! {
|
||||
static URL: ScriptUrl = ScriptUrl::new(include_str!("worker.min.js"));
|
||||
static URL: ScriptUrl = ScriptUrl::new(include_str!("../script/worker.min.js"));
|
||||
static WORKER: Worker = URL.with(|url| Worker::new(&url.0)).expect("`new Worker()` is not expected to fail with a local script");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +0,0 @@
|
|||
onmessage = event => {
|
||||
const [port, timeout] = event.data
|
||||
const f = () => port.postMessage(undefined)
|
||||
|
||||
if ('scheduler' in this) {
|
||||
scheduler.postTask(f, { delay: timeout })
|
||||
} else {
|
||||
setTimeout(f, timeout)
|
||||
}
|
||||
}
|
||||
1
src/platform_impl/web/web_sys/worker.min.js
vendored
1
src/platform_impl/web/web_sys/worker.min.js
vendored
|
|
@ -1 +0,0 @@
|
|||
onmessage=e=>{let[s,t]=e.data,a=()=>s.postMessage(void 0);"scheduler"in this?scheduler.postTask(a,{delay:t}):setTimeout(a,t)};
|
||||
Loading…
Add table
Add a link
Reference in a new issue