Skip to content

Commit

Permalink
Move to TypeScript (#3830)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
daxpedda authored Jul 27, 2024
1 parent 7b0104b commit 7892e86
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 13 deletions.
11 changes: 11 additions & 0 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 2

updates:
- package-ecosystem: npm
directory: src/platform_impl/web/script
schedule:
interval: daily
groups:
github-actions:
patterns:
- '*'
21 changes: 20 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,36 @@ 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
- name: Install SWC
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 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions .swcrc → src/platform_impl/web/script/.swcrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
{
"module": {
"type": "es6"
},
"isModule": true,
"minify": true,
"jsc": {
"parser": {
"syntax": "typescript"
},
"target": "es2022",
"minify": {
"compress": {
Expand Down
32 changes: 32 additions & 0 deletions src/platform_impl/web/script/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions src/platform_impl/web/script/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"devDependencies": {
"@eslint/js": "^9",
"@types/eslint__js": "^8",
"eslint": "^8",
"typescript": "^5",
"typescript-eslint": "^7"
}
}
12 changes: 12 additions & 0 deletions src/platform_impl/web/script/scheduler.d.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 0 deletions src/platform_impl/web/script/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions src/platform_impl/web/script/worker.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/platform_impl/web/script/worker.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletion src/platform_impl/web/web_sys/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
10 changes: 0 additions & 10 deletions src/platform_impl/web/web_sys/worker.js

This file was deleted.

1 change: 0 additions & 1 deletion src/platform_impl/web/web_sys/worker.min.js

This file was deleted.

0 comments on commit 7892e86

Please sign in to comment.