-
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 7 commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// This code was adapted to Dart from | ||
// https://github.com/nodejs/node-v0.x-archive/blob/master/lib/util.js | ||
// | ||
// Copyright Joyent, Inc. and other Node contributors. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a | ||
// copy of this software and associated documentation files (the | ||
// "Software"), to deal in the Software without restriction, including | ||
// without limitation the rights to use, copy, modify, merge, publish, | ||
// distribute, sublicense, and/or sell copies of the Software, and to permit | ||
// persons to whom the Software is furnished to do so, subject to the | ||
// following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included | ||
// in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
// USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
@JS() | ||
library console_log_formatter; | ||
|
||
import 'dart:developer'; | ||
|
||
import 'package:js/js.dart'; | ||
|
||
@JS('JSON.stringify') | ||
external String _jsonStringify(dynamic object); | ||
|
||
final _formatRegExp = RegExp('%[sdj%]'); | ||
|
||
/// A doc comment | ||
String format(dynamic f, List<dynamic> arguments) { | ||
if (f is! String) { | ||
return [f, ...arguments].join(' '); | ||
} | ||
|
||
var str = ''; | ||
if (f is String) { | ||
var i = 0; | ||
final len = arguments.length; | ||
str += f.replaceAllMapped(_formatRegExp, (m) { | ||
final x = m[0]; | ||
if (x == '%%') return '%'; | ||
if (i >= len) return x; | ||
switch (x) { | ||
case '%s': | ||
return arguments[i++].toString(); | ||
case '%d': | ||
return num.tryParse(arguments[i++].toString()).toString(); | ||
case '%j': | ||
try { | ||
final argToStringify = arguments[i++]; | ||
debugger(); | ||
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. Looks like this and the import were committed by mistake. |
||
return _jsonStringify(argToStringify); | ||
} catch (_) { | ||
return x; | ||
} | ||
break; | ||
default: | ||
return x; | ||
} | ||
}); | ||
|
||
if (i < len) { | ||
str += ' ${arguments.skip(i).join(' ')}'; | ||
} | ||
} | ||
|
||
return str; | ||
} |
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,12 +14,20 @@ | |
// 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'; | ||
|
||
import 'console_log_formatter.dart'; | ||
|
||
/// Runs a provided [callback] and afterwards [print]s each log that occurs during the runtime | ||
/// of that function. | ||
/// | ||
|
@@ -72,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. | ||
/// | ||
|
@@ -89,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(message?.toString()); | ||
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]); | ||
} | ||
} | ||
|
||
|
@@ -152,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.
You want me to close this PR? 😛
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.
🖕 get out of here friday greg its wednesday.
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.
This is worse than no doc comment lol