Skip to content

Commit

Permalink
Merge pull request #6117 from Automattic/fix/es6-ie11
Browse files Browse the repository at this point in the history
Add warning for ES6 instance functions in development environment
  • Loading branch information
klimeryk authored Aug 17, 2016
2 parents 4bd537c + b3b1fe1 commit 486cc16
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
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#' ) );

[ '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' ] ) );
} );

} );

0 comments on commit 486cc16

Please sign in to comment.