From e05bae167a7249cc1c2c3faa6e5a242d0cad7c5d Mon Sep 17 00:00:00 2001 From: cha0s Date: Mon, 4 Apr 2022 03:08:36 -0500 Subject: [PATCH] fix: global/externals --- packages/code/src/function.js | 7 +++++++ packages/code/test/function.js | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/packages/code/src/function.js b/packages/code/src/function.js index 7ce6881..2606337 100644 --- a/packages/code/src/function.js +++ b/packages/code/src/function.js @@ -72,6 +72,13 @@ function flattenParams(ast) { export function hasExternalDependency(ast) { const locals = flattenParams(ast); + locals.push( + 'global', + 'super', + 'window', + 'Math', + // ... @todo + ); const idWalker = walk({ Identifier: ({name}) => { locals.push(name); diff --git a/packages/code/test/function.js b/packages/code/test/function.js index a4491ce..630dc46 100644 --- a/packages/code/test/function.js +++ b/packages/code/test/function.js @@ -43,11 +43,31 @@ it('can identify external dependencies', () => { )) .to.be.true; + expect(hasExternalDependency( + extractFunction(parse('function test(a, b) { c.a.b; }')), + )) + .to.be.true; + + expect(hasExternalDependency( + extractFunction(parse('function test(a, b) { c.d; }')), + )) + .to.be.true; + + expect(hasExternalDependency( + extractFunction(parse('function test(a, b) { Math.min(a, b); }')), + )) + .to.be.false; + expect(hasExternalDependency( extractFunction(parse('function test(a, b) { this.whatever; }')), )) .to.be.false; + expect(hasExternalDependency( + extractFunction(parse('function test(a, b) { this.foo.whatever }')), + )) + .to.be.false; + expect(hasExternalDependency( extractFunction(parse('function test(a, b) { console.log("HI"); }')), ))