Skip to content

[js] add js.lib.NativeString #12127

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

Draft
wants to merge 3 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions std/StringTools.hx
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ class StringTools {
#elseif java
return (index < s.length) ? cast(_charAt(s, index), Int) : -1;
#elseif js
return (cast s).charCodeAt(index);
return (cast s : js.lib.NativeString).charCodeAt(index);
#elseif python
return if (index >= s.length) -1 else python.internal.UBuiltins.ord(python.Syntax.arrayAccess(s, index));
#elseif hl
Expand Down Expand Up @@ -501,7 +501,7 @@ class StringTools {
#elseif java
return cast(_charAt(s, index), Int);
#elseif js
return (cast s).charCodeAt(index);
return (cast s : js.lib.NativeString).charCodeAt(index);
#elseif python
return python.internal.UBuiltins.ord(python.Syntax.arrayAccess(s, index));
#elseif hl
Expand Down
6 changes: 4 additions & 2 deletions std/js/_std/EReg.hx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
import js.lib.NativeString;

@:coreApi class EReg {
var r:HaxeRegExp;

Expand Down Expand Up @@ -84,7 +86,7 @@
}

public inline function replace(s:String, by:String):String {
return (cast s).replace(r, by);
return (cast s : NativeString).replace(r, by);
}

public function map(s:String, f:EReg->String):String {
Expand Down Expand Up @@ -112,7 +114,7 @@
}

public static inline function escape(s:String):String {
return (cast s).replace(escapeRe, "\\$&");
return (cast s : NativeString).replace(escapeRe, "\\$&");
}

static var escapeRe = new js.lib.RegExp("[.*+?^${}()|[\\]\\\\]", "g");
Expand Down
2 changes: 1 addition & 1 deletion std/js/_std/HxOverrides.hx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class HxOverrides {

@:pure
static function cca(s:String, index:Int):Null<Int> {
var x = (cast s).charCodeAt(index);
var x = (cast s : js.lib.NativeString).charCodeAt(index);
if (x != x) // fast isNaN
return js.Lib.undefined; // isNaN will still return true
return x;
Expand Down
34 changes: 34 additions & 0 deletions std/js/lib/NativeString.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package js.lib;

/**
Native javascript string.
**/
@:native("String")
extern class NativeString {
/**
The `charCodeAt()` method of String values returns an integer between 0
and 65535 representing the UTF-16 code unit at the given index.

If `index` is out of range of `0` – `str.length - 1`, returns `NaN`.

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt
**/
@:pure function charCodeAt(index:Int):Int;

/**
The `replace()` method of String values returns a new string with one,
some, or all matches of a pattern replaced by a replacement.

The pattern can be a `String` or a `js.lib.RegExp`, and the replacement
can be a string or a function called for each match.

If pattern is a string, only the first occurrence will be replaced. The
original string is left unchanged.

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
**/
@:overload(function(pattern:RegExp, replacement:String):String {})
@:overload(function(pattern:RegExp, replacement:String->String):String {})
@:overload(function(pattern:String, replacement:String->String):String {})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to avoid this syntax for new additions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to have "real overloads" share documentation?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I guess that's what @see is for, though I'm not sure how you'd point to the right thing here.

@:pure function replace(pattern:String, replacement:String):String;
}
30 changes: 30 additions & 0 deletions tests/server/src/cases/issues/Issue11904.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cases.issues;

import haxe.display.Diagnostic;

class Issue11904 extends TestCase {
function test(_) {
vfs.putContent("Issue11904.hx", getTemplate("issues/Issue11904.hx"));
var args = ["-main", "Issue11904", "--js", "no.js", "--no-output"];
runHaxe(args);
runHaxeJson([], ServerMethods.Invalidate, {file: new FsPath("Issue11904.hx")});
runHaxeJsonCb(args, DisplayMethods.Diagnostics, {file: new FsPath("Issue11904.hx")}, res -> {
Assert.equals(1, res.length);
Assert.equals(2, res[0].diagnostics.length);

function check<T>(d:Diagnostic<T>) {
switch (d.kind) {
case ReplaceableCode:
Assert.equals("Unused variable", d.args.description);

case _:
// trace(d);
Assert.fail("Unexpected diagnostics kind: " + d.kind);
}
}

var diag = res[0].diagnostics;
for (d in diag) check(d);
});
}
}
9 changes: 9 additions & 0 deletions tests/server/test/templates/issues/Issue11904.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using StringTools;

@:nullSafety
class Issue11904 {
static function main() {}
static function extractReturnType(hint:String):Void {
for (i => code in hint) {}
}
}
Loading