-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Declare variables before use (#6806)
While working on #6805, I noticed that many variables were being used before they were declared. Technically this worked fine in practice because we were using the `transform-es2015-block-scoping` Babel plugin, which transforms `let` and `const` to `var`, which is hoisted. However, after removing that Babel transformation, many things broke. All instances of variables or classes being used before declared have been fixed. The `no-use-before-define` eslint rule has been added to catch these cases going forward. The rule is disabled for function declarations for the moment, because those are always hoisted. We could disable that too if we want to, but it's purely stylistic and would require a lot more changes.
- Loading branch information
Showing
8 changed files
with
99 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,36 @@ | ||
/*global Web3*/ | ||
|
||
|
||
// need to make sure we aren't affected by overlapping namespaces | ||
// and that we dont affect the app with our namespace | ||
// mostly a fix for web3's BigNumber if AMD's "define" is defined... | ||
let __define | ||
|
||
/** | ||
* Caches reference to global define object and deletes it to | ||
* avoid conflicts with other global define objects, such as | ||
* AMD's define function | ||
*/ | ||
const cleanContextForImports = () => { | ||
__define = global.define | ||
try { | ||
global.define = undefined | ||
} catch (_) { | ||
console.warn('MetaMask - global.define could not be deleted.') | ||
} | ||
} | ||
|
||
/** | ||
* Restores global define object from cached reference | ||
*/ | ||
const restoreContextAfterImports = () => { | ||
try { | ||
global.define = __define | ||
} catch (_) { | ||
console.warn('MetaMask - global.define could not be overwritten.') | ||
} | ||
} | ||
|
||
cleanContextForImports() | ||
require('web3/dist/web3.min.js') | ||
const log = require('loglevel') | ||
|
@@ -46,6 +78,20 @@ inpageProvider.enable = function ({ force } = {}) { | |
// this will be default true so it does not break any old apps. | ||
inpageProvider.autoRefreshOnNetworkChange = true | ||
|
||
|
||
// publicConfig isn't populated until we get a message from background. | ||
// Using this getter will ensure the state is available | ||
const getPublicConfigWhenReady = async () => { | ||
const store = inpageProvider.publicConfigStore | ||
let state = store.getState() | ||
// if state is missing, wait for first update | ||
if (!state.networkVersion) { | ||
state = await new Promise(resolve => store.once('update', resolve)) | ||
console.log('new state', state) | ||
} | ||
return state | ||
} | ||
|
||
// add metamask-specific convenience methods | ||
inpageProvider._metamask = new Proxy({ | ||
/** | ||
|
@@ -87,19 +133,6 @@ inpageProvider._metamask = new Proxy({ | |
}, | ||
}) | ||
|
||
// publicConfig isn't populated until we get a message from background. | ||
// Using this getter will ensure the state is available | ||
async function getPublicConfigWhenReady () { | ||
const store = inpageProvider.publicConfigStore | ||
let state = store.getState() | ||
// if state is missing, wait for first update | ||
if (!state.networkVersion) { | ||
state = await new Promise(resolve => store.once('update', resolve)) | ||
console.log('new state', state) | ||
} | ||
return state | ||
} | ||
|
||
// Work around for [email protected] deleting the bound `sendAsync` but not the unbound | ||
// `sendAsync` method on the prototype, causing `this` reference issues with drizzle | ||
const proxiedInpageProvider = new Proxy(inpageProvider, { | ||
|
@@ -161,33 +194,3 @@ inpageProvider.publicConfigStore.subscribe(function (state) { | |
window.postMessage('onboardingcomplete', '*') | ||
} | ||
}) | ||
|
||
// need to make sure we aren't affected by overlapping namespaces | ||
// and that we dont affect the app with our namespace | ||
// mostly a fix for web3's BigNumber if AMD's "define" is defined... | ||
let __define | ||
|
||
/** | ||
* Caches reference to global define object and deletes it to | ||
* avoid conflicts with other global define objects, such as | ||
* AMD's define function | ||
*/ | ||
function cleanContextForImports () { | ||
__define = global.define | ||
try { | ||
global.define = undefined | ||
} catch (_) { | ||
console.warn('MetaMask - global.define could not be deleted.') | ||
} | ||
} | ||
|
||
/** | ||
* Restores global define object from cached reference | ||
*/ | ||
function restoreContextAfterImports () { | ||
try { | ||
global.define = __define | ||
} catch (_) { | ||
console.warn('MetaMask - global.define could not be overwritten.') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters