Skip to content

Commit

Permalink
Deoptimize typeof for regular expression literals to better support e…
Browse files Browse the repository at this point in the history
…s6-shim (#2916)
  • Loading branch information
lukastaegert committed Jun 10, 2019
1 parent 3ff3db8 commit 1718faa
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/ast/nodes/Literal.ts
Expand Up @@ -26,9 +26,11 @@ export default class Literal<T = LiteralValue> extends NodeBase {
getLiteralValueAtPath(path: ObjectPath): LiteralValueOrUnknown {
if (
path.length > 0 ||
// unknown literals such as bigints can also be null but do not start with an "n"
// unknown literals can also be null but do not start with an "n"
(this.value === null && this.context.code.charCodeAt(this.start) !== 110) ||
typeof this.value === 'bigint'
typeof this.value === 'bigint' ||
// to support shims for regular expressions
this.context.code.charCodeAt(this.start) === 47
) {
return UNKNOWN_VALUE;
}
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/deopt-regexp-type/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'deoptimizes typeof for regular expressions to better support es6-sim'
};
10 changes: 10 additions & 0 deletions test/form/samples/deopt-regexp-type/_expected.js
@@ -0,0 +1,10 @@
const isCallable =
typeof /abc/ === 'function'
? function IsCallableSlow(x) {
return typeof x === 'function' && _toString(x) === '[object Function]';
}
: function IsCallableFast(x) {
return typeof x === 'function';
};

console.log(isCallable(/x/));
10 changes: 10 additions & 0 deletions test/form/samples/deopt-regexp-type/main.js
@@ -0,0 +1,10 @@
const isCallable =
typeof /abc/ === 'function'
? function IsCallableSlow(x) {
return typeof x === 'function' && _toString(x) === '[object Function]';
}
: function IsCallableFast(x) {
return typeof x === 'function';
};

console.log(isCallable(/x/));

0 comments on commit 1718faa

Please sign in to comment.