-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Incorporate code review feedback from buschtoens.
* Extract shared utility for a `this` context that errors when used. * Assert when more positional arguments are provided. * Avoid wrapping callback needlessly (makes debugging a bit easier, since the event handler function is actually the user-land function in most cases. * Incorporates a system to error if any `this` properties are accessed from within the callback in debug builds. * Fixup comments for addEventListener / removeEventListener logic. * Fix test assertion message. * Add test ensuring the modifier is destroyed properly.
- Loading branch information
Showing
4 changed files
with
125 additions
and
51 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
39 changes: 39 additions & 0 deletions
39
packages/@ember/-internals/glimmer/lib/utils/untouchable-this.ts
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { HAS_NATIVE_PROXY } from '@ember/-internals/utils'; | ||
import { assert } from '@ember/debug'; | ||
import { DEBUG } from '@glimmer/env'; | ||
|
||
export default function buildUntouchableThis(source: string): null | object { | ||
let context: null | object = null; | ||
if (DEBUG && HAS_NATIVE_PROXY) { | ||
let assertOnProperty = (property: string | number | symbol) => { | ||
assert( | ||
`You accessed \`this.${String( | ||
property | ||
)}\` from a function passed to the ${source}, but the function itself was not bound to a valid \`this\` context. Consider updating to usage of \`@action\`.` | ||
); | ||
}; | ||
|
||
context = new Proxy( | ||
{}, | ||
{ | ||
get(_target: {}, property: string | symbol) { | ||
assertOnProperty(property); | ||
}, | ||
|
||
set(_target: {}, property: string | symbol) { | ||
assertOnProperty(property); | ||
|
||
return false; | ||
}, | ||
|
||
has(_target: {}, property: string | symbol) { | ||
assertOnProperty(property); | ||
|
||
return false; | ||
}, | ||
} | ||
); | ||
} | ||
|
||
return context; | ||
} |
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