Skip to content

Commit 3d305a6

Browse files
fix: fix napi error
1 parent a25bef1 commit 3d305a6

File tree

5 files changed

+19
-13
lines changed

5 files changed

+19
-13
lines changed

Cargo.lock

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/napi/__test__/index.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ test('test file count', async t => {
8787
test('show good error message for invalid arg', async t => {
8888
const sg = parse('console.log(123)')
8989
t.throws(() => sg.root().find({rule: {regex: '('}}), {
90-
message: 'Rule contains invalid regex matcher.'
90+
message: /Rule contains invalid regex matcher/
9191
})
9292
t.throws(() => sg.root().find({
9393
rule: { all: [{any: [{ kind: '33'}]}]}
9494
}), {
95-
message: 'Rule contains invalid kind matcher.'
95+
message: /Rule contains invalid kind matcher/
9696
})
9797
})
9898

crates/napi/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface NapiConfig {
2626
rule: any
2727
constraints?: any
2828
language?: FrontEndLanguage
29+
utils?: any
2930
}
3031
export function parseFiles(paths: string[], callback: (err: null | Error, result: SgRoot) => void): Promise<number>
3132
export interface FindConfig {
@@ -35,6 +36,7 @@ export interface FindConfig {
3536
export class SgNode {
3637
range(): Range
3738
isLeaf(): boolean
39+
isNamedLeaf(): boolean
3840
kind(): string
3941
text(): string
4042
matches(m: string): boolean

crates/napi/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@
6363
}
6464
},
6565
"packageManager": "[email protected]"
66-
}
66+
}

crates/napi/src/lib.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use ast_grep_core::pinned::{NodeData, PinnedNodeData};
66
use ast_grep_core::{matcher::KindMatcher, AstGrep, NodeMatch, Pattern};
77
use ignore::types::TypesBuilder;
88
use ignore::{WalkBuilder, WalkState};
9-
use napi::anyhow::{anyhow, Context, Result as Ret};
9+
use napi::anyhow::{anyhow, Context, Error, Result as Ret};
1010
use napi::bindgen_prelude::*;
1111
use napi::threadsafe_function::{ErrorStrategy, ThreadsafeFunction, ThreadsafeFunctionCallMode};
1212
use napi::{JsNumber, Task};
@@ -196,9 +196,13 @@ fn parse_config(
196196
constraints: config.constraints.map(serde_json::from_value).transpose()?,
197197
utils: config.utils.map(serde_json::from_value).transpose()?,
198198
};
199-
rule
200-
.get_matcher(&Default::default())
201-
.map_err(|e| napi::Error::new(napi::Status::InvalidArg, e.to_string()))
199+
rule.get_matcher(&Default::default()).map_err(|e| {
200+
let error = Error::from(e)
201+
.chain()
202+
.map(ToString::to_string)
203+
.collect::<Vec<_>>();
204+
napi::Error::new(napi::Status::InvalidArg, error.join("\n |->"))
205+
})
202206
}
203207

204208
/// tree traversal API

0 commit comments

Comments
 (0)