Skip to content
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

Add warning for ES6 instance functions in development environment #6117

Merged
merged 1 commit into from
Aug 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions client/boot/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Initialize localStorage polyfill before any dependencies are loaded
require( 'lib/local-storage' )();
if ( process.env.NODE_ENV === 'development' ) {
require( 'lib/wrap-es6-functions' )();
}

/**
* External dependencies
Expand Down
28 changes: 28 additions & 0 deletions client/lib/wrap-es6-functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import partial from 'lodash/partial';
import isFunction from 'lodash/isFunction';

function wrapFnWithWarning( fn, name ) {
const consoleFn = ( console.error || console.log ).bind( console );
return function() {
const err = new Error( `${ name } is not supported on all browsers. You must use a replacement method from lodash.` );
consoleFn( err );
return fn.apply( this, arguments );
}
}

function wrapObjectFn( obj, objectName, key ) {
if ( isFunction( obj[ key ] ) ) {
Object.defineProperty( obj, key, { value: wrapFnWithWarning( obj[ key ], `${ objectName }${ key}` ) } );
}
}

export default function() {
[ 'keys', 'entries', 'values', 'findIndex', 'fill', 'find' ]
.map( partial( wrapObjectFn, Array.prototype, 'Array#' ) );

[ 'codePointAt', 'normalize', 'repeat', 'startsWith', 'endsWith', 'includes' ]
.map( partial( wrapObjectFn, String.prototype, 'String#' ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quickly looked at one of the traces, and it seems like one of our vendor libs is callingcodePointAt with unexpected results.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it was the tests that were borked, I was stubbing the methods instead spying. The tests are OK now.


[ 'flags' ].map( partial( wrapObjectFn, RegExp.prototype, 'RegExp#' ) );

}
70 changes: 70 additions & 0 deletions client/lib/wrap-es6-functions/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* External Dependencies
*/
import partial from 'lodash/partial';
import assert from 'assert';
import isFunction from 'lodash/isFunction';

/**
* Internal Dependencies
*/
import { useSandbox } from 'test/helpers/use-sinon';


describe( 'wrapping', () => {
let sandbox,
arrayProps = [ 'keys', 'entries', 'values', 'findIndex', 'fill', 'find' ].filter( key => isFunction( Array.prototype[ key ] ) ),
stringProps = [ 'codePointAt', 'normalize', 'repeat', 'startsWith', 'endsWith', 'includes' ].filter( key => isFunction( String.prototype[ key ] ) ),
consoleSpy,
regExpProps = [ 'flags' ].filter( key => isFunction( RegExp.prototype[ key ] ) );

function installSpies( props, obj ) {
props.forEach( key => {
sandbox.spy( obj, key );
} );
}

function assertCall( obj, args, key ) {
it( key, () => {
if ( isFunction( obj[ key ] ) ) {
obj[ key ].apply( obj, args );
assert( consoleSpy.calledOnce );
}
} )
}

useSandbox( newSandbox => sandbox = newSandbox );
before( () => {
consoleSpy = sandbox.stub( console, 'error' );
installSpies( arrayProps, Array.prototype );
installSpies( stringProps, String.prototype );
installSpies( regExpProps, RegExp.prototype );

require( '../' )();
} );

after( () => {
sandbox.restore();
} );

beforeEach( () => {
consoleSpy.reset();
} );

describe( 'Array', () => {
[ 'keys', 'entries', 'values' ].forEach( partial( assertCall, Array.prototype, [] ) );
[ 'findIndex', 'find' ].forEach( partial( assertCall, Array.prototype, [ () => true ] ) );
[ 'fill' ].forEach( partial( assertCall, Array.prototype, [ 1 ] ) );
} );

describe( 'String', () => {
[ 'codePointAt', 'repeat' ].forEach( partial( assertCall, 'hello', [ 1 ] ) );
[ 'startsWith', 'endsWith', 'includes' ].forEach( partial( assertCall, 'hello', [ 'a' ] ) );
[ 'normalize' ].forEach( partial( assertCall, 'hello', [] ) );
} );

describe( 'RegExp', () => {
[ 'flags' ].forEach( partial( assertCall, /a/, 'flags', [ 'g' ] ) );
} );

} );