-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6117 from Automattic/fix/es6-ie11
Add warning for ES6 instance functions in development environment
- Loading branch information
Showing
3 changed files
with
101 additions
and
0 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
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#' ) ); | ||
|
||
} |
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,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' ] ) ); | ||
} ); | ||
|
||
} ); |