Skip to content

Commit

Permalink
fix: check whether RegExp have the global or sticky flags set
Browse files Browse the repository at this point in the history
  • Loading branch information
TrickyPi committed Dec 10, 2022
1 parent 2c23141 commit 10f49e8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/ast/values.ts
Expand Up @@ -200,7 +200,8 @@ const literalNumberMembers: MemberDescriptions = assembleMemberDescriptions(
objectMembers
);

const literalRegExpMembers: MemberDescriptions = assembleMemberDescriptions(
// RegExp are stateful when they have the global or sticky flags set.
const literalParticularRegExpMembers: MemberDescriptions = assembleMemberDescriptions(
{
exec: returnsUnknown,
test: returnsBoolean
Expand Down Expand Up @@ -266,8 +267,8 @@ export const literalStringMembers: MemberDescriptions = assembleMemberDescriptio
export function getLiteralMembersForValue<T extends LiteralValue = LiteralValue>(
value: T
): MemberDescriptions {
if (value instanceof RegExp) {
return literalRegExpMembers;
if (value instanceof RegExp && !value.global && !value.sticky) {
return literalParticularRegExpMembers;
}
switch (typeof value) {
case 'boolean': {
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/builtin-prototypes/literal/_expected.js
Expand Up @@ -17,6 +17,9 @@ true.valueOf().unknown.unknown().unknown;
'ab'.charAt(1).unknown.unknown();
'ab'.charAt(1).unknown.unknown().unknown;

/1/g.exec(1);
/1/y.exec(1);

null.unknown;
'ab'.replace('a', () => console.log(1) || 'b');
'ab'.replaceAll('a', () => console.log(1) || 'b');
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/builtin-prototypes/literal/main.js
Expand Up @@ -17,6 +17,9 @@ true.valueOf().unknown.unknown().unknown;
'ab'.charAt(1).unknown.unknown();
'ab'.charAt(1).unknown.unknown().unknown;

/1/g.exec(1);
/1/y.exec(1);

null.unknown;

// boolean prototype
Expand Down

0 comments on commit 10f49e8

Please sign in to comment.