-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Sticky RegExp 'y' and RegExp#test delegation #732
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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,5 @@ | ||
require('../../modules/es.regexp.sticky'); | ||
|
||
module.exports = function (it) { | ||
return it.sticky; | ||
}; | ||
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
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,23 @@ | ||
'use strict'; | ||
|
||
var fails = require('./fails'); | ||
|
||
// babel-minify transpiles RegExp('a', 'y') -> /a/y and it causes SyntaxError, | ||
// so we use an intermediate function. | ||
function RE(s, f) { | ||
return RegExp(s, f); | ||
} | ||
|
||
exports.UNSUPPORTED_Y = fails(function () { | ||
// babel-minify transpiles RegExp('a', 'y') -> /a/y and it causes SyntaxError | ||
var re = RE('a', 'y'); | ||
re.lastIndex = 2; | ||
return re.exec('abcd') != null; | ||
}); | ||
|
||
exports.BROKEN_CARET = fails(function () { | ||
// https://bugzilla.mozilla.org/show_bug.cgi?id=773687 | ||
var re = RE('^r', 'gy'); | ||
re.lastIndex = 2; | ||
return re.exec('str') != null; | ||
}); |
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 |
---|---|---|
|
@@ -6,8 +6,10 @@ var defineProperty = require('../internals/object-define-property').f; | |
var getOwnPropertyNames = require('../internals/object-get-own-property-names').f; | ||
var isRegExp = require('../internals/is-regexp'); | ||
var getFlags = require('../internals/regexp-flags'); | ||
var stickyHelpers = require('../internals/regexp-sticky-helpers'); | ||
var redefine = require('../internals/redefine'); | ||
var fails = require('../internals/fails'); | ||
var setInternalState = require('../internals/internal-state').set; | ||
var setSpecies = require('../internals/set-species'); | ||
var wellKnownSymbol = require('../internals/well-known-symbol'); | ||
|
||
|
@@ -20,7 +22,9 @@ var re2 = /a/g; | |
// "new" should create a new object, old webkit bug | ||
var CORRECT_NEW = new NativeRegExp(re1) !== re1; | ||
|
||
var FORCED = DESCRIPTORS && isForced('RegExp', (!CORRECT_NEW || fails(function () { | ||
var UNSUPPORTED_Y = stickyHelpers.UNSUPPORTED_Y; | ||
|
||
var FORCED = DESCRIPTORS && isForced('RegExp', (!CORRECT_NEW || UNSUPPORTED_Y || fails(function () { | ||
re2[MATCH] = false; | ||
// RegExp constructor can alter flags and IsRegExp works correct with @@match | ||
return NativeRegExp(re1) != re1 || NativeRegExp(re2) == re2 || NativeRegExp(re1, 'i') != '/a/i'; | ||
|
@@ -33,13 +37,32 @@ if (FORCED) { | |
var thisIsRegExp = this instanceof RegExpWrapper; | ||
var patternIsRegExp = isRegExp(pattern); | ||
var flagsAreUndefined = flags === undefined; | ||
return !thisIsRegExp && patternIsRegExp && pattern.constructor === RegExpWrapper && flagsAreUndefined ? pattern | ||
: inheritIfRequired(CORRECT_NEW | ||
? new NativeRegExp(patternIsRegExp && !flagsAreUndefined ? pattern.source : pattern, flags) | ||
: NativeRegExp((patternIsRegExp = pattern instanceof RegExpWrapper) | ||
? pattern.source | ||
: pattern, patternIsRegExp && flagsAreUndefined ? getFlags.call(pattern) : flags) | ||
, thisIsRegExp ? this : RegExpPrototype, RegExpWrapper); | ||
|
||
if (!thisIsRegExp && patternIsRegExp && pattern.constructor === RegExpWrapper && flagsAreUndefined) { | ||
return pattern; | ||
} | ||
|
||
if (CORRECT_NEW) { | ||
if (patternIsRegExp && !flagsAreUndefined) pattern = pattern.source; | ||
} else if (pattern instanceof RegExpWrapper) { | ||
if (flagsAreUndefined) flags = getFlags.call(pattern); | ||
pattern = pattern.source; | ||
} | ||
|
||
if (UNSUPPORTED_Y) { | ||
var sticky = !!flags && flags.indexOf('y') > -1; | ||
if (sticky) flags = flags.replace(/y/g, ''); | ||
} | ||
|
||
var result = inheritIfRequired( | ||
CORRECT_NEW ? new NativeRegExp(pattern, flags) : NativeRegExp(pattern, flags), | ||
thisIsRegExp ? this : RegExpPrototype, | ||
RegExpWrapper | ||
); | ||
|
||
if (UNSUPPORTED_Y) setInternalState(result, { sticky: sticky }); | ||
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. Maybe it would be better to set it only when |
||
|
||
return result; | ||
}; | ||
var proxy = function (key) { | ||
key in RegExpWrapper || defineProperty(RegExpWrapper, key, { | ||
|
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,21 @@ | ||
var DESCRIPTORS = require('../internals/descriptors'); | ||
var UNSUPPORTED_Y = require('../internals/regexp-sticky-helpers').UNSUPPORTED_Y; | ||
var defineProperty = require('../internals/object-define-property').f; | ||
var getInternalState = require('../internals/internal-state').get; | ||
var RegExpPrototype = RegExp.prototype; | ||
|
||
// `RegExp.prototype.sticky` getter | ||
if (DESCRIPTORS && UNSUPPORTED_Y) { | ||
defineProperty(RegExp.prototype, 'sticky', { | ||
configurable: true, | ||
get: function () { | ||
if (this === RegExpPrototype) return undefined; | ||
// We can't use InternalStateModule.getterFor because | ||
// we don't add metadata for regexps created by a literal. | ||
if (this instanceof RegExp) { | ||
return !!getInternalState(this).sticky; | ||
} | ||
throw TypeError('Incompatible receiver, RegExp required'); | ||
} | ||
}); | ||
} |
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 @@ | ||
'use strict'; | ||
var $ = require('../internals/export'); | ||
var isObject = require('../internals/is-object'); | ||
|
||
var DELEGATES_TO_EXEC = function () { | ||
var execCalled = false; | ||
var re = /[ac]/; | ||
re.exec = function () { | ||
execCalled = true; | ||
return /./.exec.apply(this, arguments); | ||
}; | ||
return re.test('abc') === true && execCalled; | ||
}(); | ||
|
||
var nativeTest = /./.test; | ||
|
||
$({ target: 'RegExp', proto: true, forced: !DELEGATES_TO_EXEC }, { | ||
test: function (str) { | ||
if (typeof this.exec !== 'function') { | ||
return nativeTest.call(this, str); | ||
} | ||
var result = this.exec(str); | ||
if (result !== null && !isObject(result)) { | ||
throw new Error('RegExp exec method returned something other than an Object or null'); | ||
} | ||
return !!result; | ||
} | ||
}); |
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
Oops, something went wrong.
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.
Also required
stable/regexp/sticky
,features/regexp/sticky
, the same forRegExp#test
and adding them totests/commonjs
.