From c6bca4e1d48cdabe1906ba57e0c073653ba93246 Mon Sep 17 00:00:00 2001 From: Nick Olszanski Date: Tue, 3 Oct 2023 15:03:02 +0200 Subject: [PATCH] chore: clenaup code and rever tests --- src/lib/web-worker/worker-exec.ts | 39 ++++++++++++++----------------- tests/unit/worker-exec.spec.ts | 21 ++++++++--------- 2 files changed, 27 insertions(+), 33 deletions(-) diff --git a/src/lib/web-worker/worker-exec.ts b/src/lib/web-worker/worker-exec.ts index 685ab84d..ee4640d6 100644 --- a/src/lib/web-worker/worker-exec.ts +++ b/src/lib/web-worker/worker-exec.ts @@ -118,35 +118,30 @@ export const runScriptContent = ( /** * Replace some `this` symbols with a new value. - * Still not perfect, but might be better than a more naive regex. + * Still not perfect, but might be better than a less advanced regex * Check out the tests for examples: tests/unit/worker-exec.spec.ts + * + * This still fails with simple strings like: + * 'sadly we fail at this simple string' + * + * One way to do that would be to remove all comments from code and do single / double quote counting + * per symbol. But this will still fail with evals. */ export const replaceThisInSource = (scriptContent: string, newThis: string): string => { - // Best for now but not perfect - // Use a more complex regex to match potential preceding character and adjust replacement accordingly - const r0 = /(? { - // console.log('\n'); - // console.log('input: ' + scriptContent); - // console.log('match: ', match); - // console.log('p1: ', p1); - // console.log('p2: ', p2); - // console.log('\n'); + /** + * Best for now but not perfect + * We don't use Regex lookbehind, because of Safari + */ + const FIND_THIS = /([a-zA-Z0-9_$\.\'\"\`])?(\.\.\.)?this(?![a-zA-Z0-9_$:])/g; + + return scriptContent.replace(FIND_THIS, (match, p1, p2) => { + const prefix = (p1 || '') + (p2 || ''); if (p1 != null) { - return (p1 || '') + (p2 || '') + 'this'; + return prefix + 'this'; } // If there was a preceding character, include it unchanged - // console.log('===', scriptContent, '----', p1, p2); - return (p1 || '') + (p2 || '') + newThis; + return prefix + newThis; }); - - // 3.5 - // const r = /(^|[^a-zA-Z0-9_$.\'\"`])\.\.\.?this(?![a-zA-Z0-9_$:])/g; - // return scriptContent.replace(r, '$1' + newThis); - // const r = /(? { diff --git a/tests/unit/worker-exec.spec.ts b/tests/unit/worker-exec.spec.ts index f508f9f6..2249f964 100644 --- a/tests/unit/worker-exec.spec.ts +++ b/tests/unit/worker-exec.spec.ts @@ -99,7 +99,6 @@ test('We should replace `this` keyword more or less sane', ({ env, win }) => { }; // Should replace: - // assert.is(r('`sadly we fail at this simple string`'), '`sadly we fail at this simple string`'); assert.is(r('{this:123}'), '{this:123}'); assert.is(r('a.this'), 'a.this'); assert.is(r('[`kathis`]'), '[`kathis`]'); @@ -124,7 +123,7 @@ test('We should replace `this` keyword more or less sane', ({ env, win }) => { a.b.this let _this, This, $this `; - // const rez = replaceThisInSource(input, `__this`); + const rez = replaceThisInSource(input, `__this`); const out = ` // Should replace: @@ -145,16 +144,16 @@ test('We should replace `this` keyword more or less sane', ({ env, win }) => { let _this, This, $this `; - // assert.is(rez, out); + assert.is(rez, out); }); -// test('properly replaces this is js window context', ({ env, win }) => { -// const s = ` -// let a = { this: 123 }; -// window.result = a.this; -// `; -// run(env, s); -// assert.is(win.result, 123); -// }); +test('properly replaces this is js window context', ({ env, win }) => { + const s = ` + let a = { this: 123 }; + window.result = a.this; + `; + run(env, s); + assert.is(win.result, 123); +}); test.run();