Skip to content

Commit 7892e86

Browse files
authored
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.
1 parent 7b0104b commit 7892e86

13 files changed

+120
-13
lines changed

.github/dependabot.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: 2
2+
3+
updates:
4+
- package-ecosystem: npm
5+
directory: src/platform_impl/web/script
6+
schedule:
7+
interval: daily
8+
groups:
9+
github-actions:
10+
patterns:
11+
- '*'

.github/workflows/ci.yml

+20-1
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,36 @@ jobs:
243243
log-level: error
244244
arguments: --all-features --target ${{ matrix.platform.target }}
245245

246+
eslint:
247+
name: ESLint
248+
249+
runs-on: ubuntu-latest
250+
defaults:
251+
run:
252+
working-directory: ./src/platform_impl/web/script
253+
254+
steps:
255+
- uses: taiki-e/checkout-action@v1
256+
- name: Setup NPM
257+
run: npm install
258+
- name: Run ESLint
259+
run: npx eslint
260+
246261
swc:
247262
name: Minimize JavaScript
263+
248264
runs-on: ubuntu-latest
265+
defaults:
266+
run:
267+
working-directory: ./src/platform_impl/web/script
249268

250269
steps:
251270
- uses: taiki-e/checkout-action@v1
252271
- name: Install SWC
253272
run: sudo npm i -g @swc/cli
254273
- name: Run SWC
255274
run: |
256-
swc src/platform_impl/web/web_sys/worker.js -o src/platform_impl/web/web_sys/worker.min.js
275+
swc . --ignore node_modules,**/*.d.ts --only **/*.ts -d . --out-file-extension min.js
257276
- name: Check for diff
258277
run: |
259278
[[ -z $(git status -s) ]]

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ rls/
55
*~
66
#*#
77
.DS_Store
8+
# NPM package used to run ESLint.
9+
/src/platform_impl/web/script/node_modules
10+
/src/platform_impl/web/script/package-lock.json

.swcrc src/platform_impl/web/script/.swcrc

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
{
2+
"module": {
3+
"type": "es6"
4+
},
5+
"isModule": true,
26
"minify": true,
37
"jsc": {
8+
"parser": {
9+
"syntax": "typescript"
10+
},
411
"target": "es2022",
512
"minify": {
613
"compress": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import eslint from '@eslint/js'
2+
import tseslint from 'typescript-eslint'
3+
import globals from 'globals'
4+
5+
export default tseslint.config(
6+
{
7+
ignores: ['**/*.min.js', 'eslint.config.mjs'],
8+
},
9+
eslint.configs.recommended,
10+
...tseslint.configs.strictTypeChecked,
11+
...tseslint.configs.stylisticTypeChecked,
12+
{
13+
languageOptions: {
14+
parserOptions: {
15+
ecmaVersion: 'latest',
16+
project: ['tsconfig.json'],
17+
sourceType: 'module',
18+
},
19+
globals: {
20+
...globals.browser,
21+
},
22+
},
23+
rules: {
24+
'@typescript-eslint/no-confusing-void-expression': [
25+
'error',
26+
{
27+
ignoreArrowShorthand: true,
28+
},
29+
],
30+
},
31+
}
32+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"devDependencies": {
3+
"@eslint/js": "^9",
4+
"@types/eslint__js": "^8",
5+
"eslint": "^8",
6+
"typescript": "^5",
7+
"typescript-eslint": "^7"
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
declare global {
2+
// eslint-disable-next-line no-var
3+
var scheduler: Scheduler
4+
}
5+
6+
export interface Scheduler {
7+
postTask<T>(callback: () => T | PromiseLike<T>, options?: SchedulerPostTaskOptions): Promise<T>
8+
}
9+
10+
export interface SchedulerPostTaskOptions {
11+
delay?: number
12+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"allowUnreachableCode": false,
5+
"allowUnusedLabels": false,
6+
"exactOptionalPropertyTypes": true,
7+
"noImplicitOverride": true,
8+
"noPropertyAccessFromIndexSignature": true,
9+
"noUncheckedIndexedAccess": true,
10+
"noUnusedLocals": true,
11+
"noUnusedParameters": true,
12+
"strict": true,
13+
}
14+
}

src/platform_impl/web/script/worker.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
onmessage = (event) => {
2+
const [port, timeout] = event.data as [MessagePort, number]
3+
const f = () => port.postMessage(undefined)
4+
5+
if ('scheduler' in globalThis) {
6+
void globalThis.scheduler.postTask(f, { delay: timeout })
7+
} else {
8+
setTimeout(f, timeout)
9+
}
10+
}

src/platform_impl/web/web_sys/schedule.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ impl Schedule {
175175
F: 'static + FnMut(),
176176
{
177177
thread_local! {
178-
static URL: ScriptUrl = ScriptUrl::new(include_str!("worker.min.js"));
178+
static URL: ScriptUrl = ScriptUrl::new(include_str!("../script/worker.min.js"));
179179
static WORKER: Worker = URL.with(|url| Worker::new(&url.0)).expect("`new Worker()` is not expected to fail with a local script");
180180
}
181181

src/platform_impl/web/web_sys/worker.js

-10
This file was deleted.

src/platform_impl/web/web_sys/worker.min.js

-1
This file was deleted.

0 commit comments

Comments
 (0)