Skip to content

Commit 0130321

Browse files
committed
refactor(tauri): improve script handling in execute function for Tauri
- Updated the `execute` function to simplify string handling by passing strings as-is, allowing Rust to manage proper escaping. - Adjusted related test cases to reflect this change, ensuring accurate expectations for string arguments passed to the plugin.
1 parent 529b134 commit 0130321

2 files changed

Lines changed: 6 additions & 5 deletions

File tree

packages/tauri-service/src/commands/execute.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ export async function execute<ReturnValue, InnerArguments extends unknown[]>(
6565
log.debug('Plugin availability cached, skipping check');
6666
}
6767

68-
// Convert function to string - keep parameters intact, plugin will inject tauri as first arg
69-
// For strings, use JSON.stringify to safely escape special characters
70-
const scriptString = typeof script === 'function' ? script.toString() : JSON.stringify(script);
68+
// Convert function to string - keep parameters intact, plugin will handle escaping
69+
// For functions: use .toString() (produces valid JS function source)
70+
// For strings: send as-is (Rust handles proper escaping when args present)
71+
const scriptString = typeof script === 'function' ? script.toString() : script;
7172

7273
// Execute via plugin's execute command with better error handling
7374
// The plugin will inject the Tauri APIs object as the first argument

packages/tauri-service/test/commands/execute.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ describe('execute', () => {
189189
expect(secondCall[3]).toBe(2);
190190
});
191191

192-
it('should pass strings properly stringified', async () => {
192+
it('should pass strings as-is to the plugin', async () => {
193193
const mockExecute = vi.fn();
194194
mockExecute.mockResolvedValueOnce(true);
195195
mockExecute.mockResolvedValueOnce(JSON.stringify({ __wdio_value__: 'hello' }));
@@ -199,7 +199,7 @@ describe('execute', () => {
199199
await execute<string, []>(browser, 'return "hello"');
200200

201201
const secondCall = mockExecute.mock.calls[1];
202-
expect(secondCall[1]).toBe(JSON.stringify('return "hello"'));
202+
expect(secondCall[1]).toBe('return "hello"');
203203
});
204204
});
205205

0 commit comments

Comments
 (0)