-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FED-466: Fix console logs that require formatting #52
Merged
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
aa96e2f
fix console logs that require formatting
kealjones-wk c6fde52
add 1 test for formatter :P
kealjones-wk 8f7b9d4
fix json stringify
kealjones-wk 19b2de9
add more situations to the formatter test
kealjones-wk 5eeb3a0
address gregs feedback
kealjones-wk 6790c0d
add exact number of args test
kealjones-wk 800eeb3
address feedback
kealjones-wk 2e3d986
add doc comment, and add all options from spec
kealjones-wk cfc5f88
change console getter to final in function
kealjones-wk 2b39903
fix typing
kealjones-wk c7beb86
fix javascript function call for dart2js
kealjones-wk 80acd97
add // @dart = 2.7 to head of formatter file
kealjones-wk 3990a38
Merge branch 'master' into spike-fed-466
kealjones-wk b814c35
use the this argument for jsfunctionapply
kealjones-wk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -14,9 +14,15 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
@JS() | ||
library console_log_utils; | ||
|
||
import 'dart:async'; | ||
import 'dart:html'; | ||
import 'dart:js'; | ||
import 'dart:js_util'; | ||
|
||
import 'package:js/js.dart'; | ||
import 'package:meta/meta.dart'; | ||
import 'package:react/react_client/react_interop.dart'; | ||
|
||
|
@@ -74,6 +80,8 @@ T spyOnConsoleLogs<T>( | |
} | ||
} | ||
|
||
get _console => getProperty(window, 'console'); | ||
|
||
/// Starts spying on console logs, calling [onLog] for each log that occurs until the | ||
/// returned function (`stopSpying`) is called. | ||
/// | ||
|
@@ -91,26 +99,37 @@ void Function() startSpyingOnConsoleLogs({ | |
@required void Function(String) onLog, | ||
}) { | ||
final logTypeToCapture = configuration.logType == 'all' ? ConsoleConfig.types : [configuration.logType]; | ||
final consoleRefs = <String, JsFunction>{}; | ||
final consoleRefs = <String, dynamic>{}; | ||
final consolePropertyDescriptors = <String, dynamic>{}; | ||
|
||
_resetPropTypeWarningCache(); | ||
|
||
// Bind to the current zone so the callback isn't called in the top-level zone. | ||
final boundOnLog = Zone.current.bindUnaryCallback(onLog); | ||
|
||
for (final config in logTypeToCapture) { | ||
consoleRefs[config] = context['console'][config] as JsFunction; | ||
context['console'][config] = JsFunction.withThis((self, [message, arg1, arg2, arg3, arg4, arg5]) { | ||
// NOTE: Using console.log or print within this function will cause an infinite | ||
// loop when the logType is set to `log`. | ||
boundOnLog(format(message?.toString(), [arg1, arg2, arg3, arg4, arg5])); | ||
consoleRefs[config].apply([message, arg1, arg2, arg3, arg4, arg5], thisArg: self); | ||
}); | ||
consolePropertyDescriptors[config] = _getOwnPropertyDescriptor(_console, config); | ||
consoleRefs[config] = context['console'][config]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we restore the cast that was previously here along with the typing on |
||
final newDescriptor = _assign( | ||
newObject(), | ||
consolePropertyDescriptors[config], | ||
jsify({ | ||
'value': allowInteropCaptureThis((self, | ||
[message, arg1 = _undefined, arg2 = _undefined, arg3 = _undefined, arg4 = _undefined, arg5 = _undefined]) { | ||
// NOTE: Using console.log or print within this function will cause an infinite | ||
// loop when the logType is set to `log`. | ||
final args = [arg1, arg2, arg3, arg4, arg5]..removeWhere((arg) => arg == _undefined); | ||
boundOnLog(format(message?.toString(), args)); | ||
consoleRefs[config].apply([message, ...args], thisArg: self); | ||
}) | ||
}), | ||
); | ||
_defineProperty(_console, config, newDescriptor); | ||
} | ||
|
||
void stopSpying() { | ||
for (final config in logTypeToCapture) { | ||
context['console'][config] = consoleRefs[config]; | ||
_defineProperty(_console, config, consolePropertyDescriptors[config]); | ||
} | ||
} | ||
|
||
|
@@ -154,3 +173,25 @@ class ConsoleConfig { | |
/// Captures calls to `console.log`, `console.warn`, and `console.error`. | ||
static const ConsoleConfig all = ConsoleConfig._('all'); | ||
} | ||
|
||
const _undefined = Undefined(); | ||
|
||
class Undefined { | ||
const Undefined(); | ||
} | ||
|
||
@JS('Object.assign') | ||
external dynamic _assign(dynamic object, dynamic otherObject, [dynamic anotherObject]); | ||
|
||
@JS('Object.getOwnPropertyDescriptor') | ||
external _PropertyDescriptor _getOwnPropertyDescriptor(dynamic object, String propertyName); | ||
|
||
@JS('Object.defineProperty') | ||
external void _defineProperty(dynamic object, String propertyName, dynamic descriptor); | ||
|
||
@JS('Object.prototype.hasOwnProperty') | ||
external bool _hasOwnProperty(dynamic object, String name); | ||
|
||
@JS() | ||
@anonymous | ||
class _PropertyDescriptor {} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this and the import were committed by mistake.