Skip to content
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

feat: add additional string prototype methods #4299

Merged
merged 7 commits into from Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/ast/values.ts
Expand Up @@ -163,6 +163,7 @@ const literalNumberMembers: MemberDescriptions = assembleMemberDescriptions(

const literalStringMembers: MemberDescriptions = assembleMemberDescriptions(
{
at: returnsString,
charAt: returnsString,
charCodeAt: returnsNumber,
codePointAt: returnsNumber,
Expand All @@ -173,6 +174,7 @@ const literalStringMembers: MemberDescriptions = assembleMemberDescriptions(
lastIndexOf: returnsNumber,
localeCompare: returnsNumber,
match: returnsBoolean,
matchAll: returnsBoolean,
normalize: returnsString,
padEnd: returnsString,
padStart: returnsString,
Expand All @@ -183,17 +185,27 @@ const literalStringMembers: MemberDescriptions = assembleMemberDescriptions(
returns: UNKNOWN_LITERAL_STRING
}
},
replaceAll: {
value: {
callsArgs: [1],
returns: UNKNOWN_LITERAL_STRING
}
},
search: returnsNumber,
slice: returnsString,
split: returnsUnknown,
startsWith: returnsBoolean,
substr: returnsString,
substr: returnsString, // deprecated
substring: returnsString,
toLocaleLowerCase: returnsString,
toLocaleUpperCase: returnsString,
toLowerCase: returnsString,
toUpperCase: returnsString,
trim: returnsString,
trimEnd: returnsString,
trimLeft: returnsString, // deprecated, alias for trimStart
trimRight: returnsString, // deprecated, alias for trimEnd
trimStart: returnsString,
valueOf: returnsString
},
objectMembers
Expand Down
1 change: 1 addition & 0 deletions test/form/samples/builtin-prototypes/literal/_expected.js
Expand Up @@ -9,6 +9,7 @@ true.valueOf()();
'ab'.charAt(1)();
null.unknown;
'ab'.replace( 'a', () => console.log( 1 ) || 'b' );
'ab'.replaceAll( 'a', () => console.log( 1 ) || 'b' );

// deep property access is forbidden
true.x.y;
Expand Down
8 changes: 8 additions & 0 deletions test/form/samples/builtin-prototypes/literal/main.js
Expand Up @@ -29,6 +29,7 @@ const _numberToLocaleString = (1).toLocaleString().trim();
const _numberToString = (1).toString().trim();

// string prototype
const _at = 'ab'.at( 1 ).trim();
const _charAt = 'ab'.charAt( 1 ).trim();
const _charCodeAt = 'ab'.charCodeAt( 1 ).toExponential( 2 );
const _codePointAt = 'ab'.codePointAt( 1 ).toExponential( 2 );
Expand All @@ -39,12 +40,15 @@ const _indexOf = 'ab'.indexOf( 'a' ).toExponential( 2 );
const _lastIndexOf = 'ab'.lastIndexOf( 'a' ).toExponential( 2 );
const _localeCompare = 'ab'.localeCompare( 'a' ).toExponential( 2 );
const _match = 'ab'.match( /a/ ).valueOf();
const _matchAll = 'ab'.matchAll( /a/ ).valueOf();
const _normalize = 'ab'.normalize().trim();
const _padEnd = 'ab'.padEnd( 4, 'a' ).trim();
const _padStart = 'ab'.padStart( 4, 'a' ).trim();
const _repeat = 'ab'.repeat( 2 ).trim();
const _replace = 'ab'.replace( 'a', () => 'b' ).trim();
const _replaceEffect = 'ab'.replace( 'a', () => console.log( 1 ) || 'b' );
const _replaceAll = 'ab'.replaceAll( 'a', () => 'b' ).trim();
const _replaceAllEffect = 'ab'.replaceAll( 'a', () => console.log( 1 ) || 'b' );
const _search = 'ab'.search( /a/ ).toExponential( 2 );
const _slice = 'ab'.slice( 0, 1 ).trim();
const _split = 'ab'.split( 'a' );
Expand All @@ -56,6 +60,10 @@ const _toLocaleUpperCase = 'ab'.toLocaleUpperCase().trim();
const _toLowerCase = 'ab'.toLowerCase().trim();
const _toUpperCase = 'ab'.toUpperCase().trim();
const _trim = 'ab'.trim().trim();
const _trimEnd = 'ab'.trimEnd().trim();
const _trimLeft = 'ab'.trimLeft().trim();
const _trimRight = 'ab'.trimRight().trim();
const _trimStart = 'ab'.trimStart().trim();
const _stringValueOf = 'ab'.valueOf().trim();
// inherited
const _stringHasOwnProperty = 'ab'.hasOwnProperty( 'toString' ).valueOf();
Expand Down