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:
daxpedda 2024-07-27 18:26:52 +02:00 committed by GitHub
parent 7b0104b54c
commit 7892e86731
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 120 additions and 13 deletions

11
.github/dependabot.yaml vendored Normal file
View file

@ -0,0 +1,11 @@
version: 2
updates:
- package-ecosystem: npm
directory: src/platform_impl/web/script
schedule:
interval: daily
groups:
github-actions:
patterns:
- '*'

View file

@ -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
View file

@ -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

View file

@ -1,6 +1,13 @@
{
"module": {
"type": "es6"
},
"isModule": true,
"minify": true,
"jsc": {
"parser": {
"syntax": "typescript"
},
"target": "es2022",
"minify": {
"compress": {

View 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,
},
],
},
}
)

View file

@ -0,0 +1,9 @@
{
"devDependencies": {
"@eslint/js": "^9",
"@types/eslint__js": "^8",
"eslint": "^8",
"typescript": "^5",
"typescript-eslint": "^7"
}
}

View 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
}

View 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,
}
}

View 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)};

View 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)
}
}

View file

@ -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");
}

View file

@ -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)
}
}

View file

@ -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)};