Skip to content

Commit 4ad5a14

Browse files
committed
feat: working wasm mock
1 parent 954c980 commit 4ad5a14

9 files changed

Lines changed: 56 additions & 19 deletions

File tree

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,23 @@ Names:
4848
✅ Bundle/minify/obfuscate
4949

5050
❌ Invent / Implement license mechanism
51-
- try webassembly
51+
- ✅ try webassembly
52+
- ❌ Cleanup rust code
53+
- ❌ cleanup rust deps
54+
- ❌ cleanup build chunks
55+
- ❌ add wasm files to package script
56+
- ❌ obfuscate wasm identifiers
57+
-
5258

53-
❌❌ Add support for proposal syntaxes
59+
❌ PoC / Implement vscode extension - mostly to understand how to license
60+
61+
✅ Add support for proposal syntaxes
5462

5563
✅ Add support for multiple wildcards
5664
- `($$, $$) => {}` is invalid while parsing function
5765
- `$_refN` - currently without ref analysis
5866
- `$$_refN` - currently without ref analysis
5967

60-
❌ PoC / Implement vscode extension - mostly to understand how to license
6168

6269
✅ Implement tests
6370

__tests__/search/codePatterns.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { search } from '/search'
22
import { compareCode } from '/astUtils';
33
import path from 'path'
44
import { getFilesList } from '/getFilesList'
5-
5+
import { init } from '/wasm'
66
const filesList = getFilesList(path.resolve(__dirname, '__fixtures__'))
77

88
describe('code patterns', () => {

jest.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ module.exports = {
88
moduleNameMapper: pathsToModuleNameMapper(tsConfig.compilerOptions.paths, { prefix: '<rootDir>' }),
99
"testPathIgnorePatterns": [
1010
"__fixtures__"
11-
]
11+
],
12+
setupFilesAfterEnv: ['./jest.setup.js']
1213
};

jest.setup.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const { init } = require('/wasm')
2+
3+
beforeAll(async () => {
4+
await init()
5+
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@
5454
"test": "yarn build && jest --passWithNoTests",
5555
"test:setup": "node ./dist/getFixtures.js"
5656
}
57-
}
57+
}

src/astUtils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { parse, ParserOptions } from '@babel/parser'
22
// import omit from 'object.omit';
3-
3+
import { wasmFns } from './wasm'
44
export type Position = {
55
line: number, column: number
66
}
@@ -55,8 +55,9 @@ export const getKeysToCompare = (node: PoorNodeType) => {
5555
}
5656

5757
export const sanitizeJSXText = (node: PoorNodeType) => {
58+
wasmFns.transform_value(node);
5859
//@ts-ignore
59-
node.value = node.value?.trim()
60+
// node.value = node.value?.trim()
6061
//@ts-ignore
6162
node.extra.raw = node.extra.raw?.trim()
6263
//@ts-ignore

src/dev.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@ import path from 'path';
22
import fs from 'fs';
33
import { search } from './search';
44
import { Mode, getMode, logMetrics, print } from './utils'
5+
import { init } from './wasm'
56

67

78
const query = fs.readFileSync(path.resolve('./devQuery')).toString()
89

9-
const mockFilePath = path.resolve('./devFile')
10+
const mockFilePath = path.resolve('./devFile');
1011

11-
const matches = search({
12-
queryCodes: [query],
13-
filePaths: [mockFilePath],
14-
mode: getMode(process.argv[2] as Mode),
15-
caseInsensitive: Boolean(process.argv[3]),
16-
debug: true,
17-
})
12+
(async () => {
13+
await init()
14+
const matches = search({
15+
queryCodes: [query],
16+
filePaths: [mockFilePath],
17+
mode: getMode(process.argv[2] as Mode),
18+
caseInsensitive: Boolean(process.argv[3]),
19+
debug: true,
20+
})
1821

19-
print(matches)
20-
logMetrics()
22+
print(matches)
23+
logMetrics()
24+
})()

src/searchWorker.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import {
22
Worker, isMainThread, parentPort, workerData
33
} from 'worker_threads'
4+
import { init } from './wasm'
45

56
import { search } from './search';
67

7-
parentPort?.postMessage(search(workerData));
8+
(async () => {
9+
await init()
10+
parentPort?.postMessage(search(workerData));
11+
})()

src/wasm.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
type WasmType = typeof import('../crate/pkg')
2+
3+
const notInitialized = () => {
4+
throw new Error('Wasm modules not initialized')
5+
}
6+
7+
export const wasmFns = {
8+
transform_value: notInitialized
9+
} as Pick<WasmType, 'transform_value'>
10+
11+
export const init = () => {
12+
return import("../crate/pkg").then(mod => {
13+
wasmFns.transform_value = mod.transform_value
14+
})
15+
}

0 commit comments

Comments
 (0)