fix(web): correct type errors, improve build

(cherry picked from commit 4db3edadf4)
This commit is contained in:
vemoo 2025-08-30 22:51:41 +02:00 committed by ObserverOfTime
parent 59f3cb91c2
commit bb82b94ded
10 changed files with 1132 additions and 428 deletions

View file

@ -50,7 +50,7 @@ declare namespace RuntimeExports {
function setValue(ptr: number, value: number, type?: string): void;
let currentParseCallback: ((index: number, position: {row: number, column: number}) => string | undefined) | null;
let currentLogCallback: ((message: string, isLex: boolean) => void) | null;
let currentProgressCallback: ((state: {currentOffset: number}) => void) | null;
let currentProgressCallback: ((state: {currentOffset: number, hasError: boolean}) => void) | null;
let currentQueryProgressCallback: ((state: {currentOffset: number}) => void) | null;
let HEAPF32: Float32Array;
let HEAPF64: Float64Array;

File diff suppressed because it is too large Load diff

View file

@ -61,10 +61,9 @@
"lib/*.h"
],
"devDependencies": {
"@eslint/js": "^9.19.0",
"@types/emscripten": "^1.40.0",
"@types/node": "^22.12.0",
"@vitest/coverage-v8": "^3.0.4",
"@eslint/js": "^9.20.0",
"@types/node": "^22.13.1",
"@vitest/coverage-v8": "^3.0.5",
"dts-buddy": "^0.5.4",
"esbuild": "^0.24.2",
"eslint": "^9.19.0",
@ -74,8 +73,16 @@
"typescript-eslint": "^8.22.0",
"vitest": "^3.0.4"
},
"peerDependencies": {
"@types/emscripten": "^1.40.0"
},
"peerDependenciesMeta": {
"@types/emscripten": {
"optional": true
}
},
"scripts": {
"build:ts": "node script/build.js",
"build:ts": "tsc -b . && node script/build.js",
"build:wasm": "cd ../../ && cargo xtask build-wasm",
"build:wasm:debug": "cd ../../ && cargo xtask build-wasm --debug",
"build": "npm run build:wasm && npm run build:ts",

View file

@ -1,4 +1,4 @@
export {
export type {
Point,
Range,
Edit,
@ -7,8 +7,8 @@ export {
LogCallback,
} from './constants';
export {
ParseOptions,
ParseState,
type ParseOptions,
type ParseState,
LANGUAGE_VERSION,
MIN_COMPATIBLE_VERSION,
Parser,
@ -18,14 +18,14 @@ export { Tree } from './tree';
export { Node } from './node';
export { TreeCursor } from './tree_cursor';
export {
QueryOptions,
QueryState,
QueryProperties,
QueryPredicate,
QueryCapture,
QueryMatch,
type QueryOptions,
type QueryState,
type QueryProperties,
type QueryPredicate,
type QueryCapture,
type QueryMatch,
CaptureQuantifier,
PredicateStep,
type PredicateStep,
Query,
} from './query';
} from './query';
export { LookaheadIterator } from './lookahead_iterator';

View file

@ -6,7 +6,7 @@ import { Query } from './query';
const LANGUAGE_FUNCTION_REGEX = /^tree_sitter_\w+$/;
export class LanguageMetadata {
export interface LanguageMetadata {
readonly major_version: number;
readonly minor_version: number;
readonly patch_version: number;

View file

@ -168,10 +168,9 @@ export function marshalEdit(edit: Edit, address = TRANSFER_BUFFER) {
*
* Unmarshals a {@link LanguageMetadata} from the transfer buffer.
*/
export function unmarshalLanguageMetadata(address: number): LanguageMetadata {
const result = {} as LanguageMetadata;
result.major_version = C.getValue(address, 'i32'); address += SIZE_OF_INT;
result.minor_version = C.getValue(address, 'i32'); address += SIZE_OF_INT;
result.field_count = C.getValue(address, 'i32');
return result;
export function unmarshalLanguageMetadata(address: number): LanguageMetadata {
const major_version = C.getValue(address, 'i32');
const minor_version = C.getValue(address += SIZE_OF_INT, 'i32');
const patch_version = C.getValue(address += SIZE_OF_INT, 'i32');
return { major_version, minor_version, patch_version };
}

View file

@ -9,7 +9,8 @@ import { TRANSFER_BUFFER } from './parser';
/** A single node within a syntax {@link Tree}. */
export class Node {
/** @internal */
private [0] = 0; // Internal handle for WASM
// @ts-expect-error: never read
private [0] = 0; // Internal handle for Wasm
/** @internal */
private _children?: (Node | null)[];
@ -635,7 +636,7 @@ export class Node {
}
/** Get the S-expression representation of this node. */
toString() {
toString(): string {
marshalNode(this);
const address = C._ts_node_to_string_wasm(this.tree[0]);
const result = C.AsciiToString(address);

View file

@ -7,16 +7,20 @@ import { getText, Tree } from './tree';
/** A stateful object for walking a syntax {@link Tree} efficiently. */
export class TreeCursor {
/** @internal */
private [0] = 0; // Internal handle for WASM
// @ts-expect-error: never read
private [0] = 0; // Internal handle for Wasm
/** @internal */
private [1] = 0; // Internal handle for WASM
// @ts-expect-error: never read
private [1] = 0; // Internal handle for Wasm
/** @internal */
private [2] = 0; // Internal handle for WASM
// @ts-expect-error: never read
private [2] = 0; // Internal handle for Wasm
/** @internal */
private [3] = 0; // Internal handle for WASM
// @ts-expect-error: never read
private [3] = 0; // Internal handle for Wasm
/** @internal */
private tree: Tree;

View file

@ -193,14 +193,14 @@ describe('Node', () => {
it('correctly retrieves immediate children', () => {
const sourceCode = 'let x = 1; console.log(x);';
tree = parser.parse(sourceCode)!;
const root = tree.rootNode
const child = root.children[0].children[0]
const a = root.childWithDescendant(child)
expect(a!.startIndex).toBe(0)
const b = a!.childWithDescendant(child)
expect(b).toEqual(child)
const c = b!.childWithDescendant(child)
expect(c).toBeNull()
const root = tree.rootNode;
const child = root.children[0]!.children[0]!;
const a = root.childWithDescendant(child);
expect(a!.startIndex).toBe(0);
const b = a!.childWithDescendant(child);
expect(b).toEqual(child);
const c = b!.childWithDescendant(child);
expect(c).toBeNull();
});
});

View file

@ -25,11 +25,14 @@
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,
"composite": true,
"isolatedModules": true,
},
"include": [
"src/**/*",
"script/**/*",
"test/**/*",
"src/*.ts",
"script/*",
"test/*",
"lib/*.ts"
],
"exclude": [
"node_modules",