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

Polyfill String.matchAll for compatibility #2443

Merged
merged 48 commits into from
Nov 21, 2024

Conversation

Sway007
Copy link
Member

@Sway007 Sway007 commented Nov 21, 2024

Please check if the PR fulfills these requirements

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

The builtin function string.matchAll has version compatibility issues.
image

Summary by CodeRabbit

  • New Features

    • Introduced a polyfill for String.prototype.matchAll, enhancing string matching capabilities in unsupported environments.
    • Updated shader chunk loading to improve identification of includes in shader code.
  • Bug Fixes

    • Ensured that the matchAll method throws a TypeError when the global flag is not used.
  • Tests

    • Added a test suite for the new polyfill to validate its functionality and ensure compatibility with existing behavior.

@Sway007 Sway007 added high priority High priority issue compatibility risk There may be a certain compatibility risk, which needs to be verified by real machine testing loader labels Nov 21, 2024
@Sway007 Sway007 self-assigned this Nov 21, 2024
Copy link

coderabbitai bot commented Nov 21, 2024

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

📥 Commits

Reviewing files that changed from the base of the PR and between 5d3132a and a36417b.

📒 Files selected for processing (3)
  • packages/core/src/Polyfill.ts (1 hunks)
  • packages/core/src/index.ts (1 hunks)
  • tests/src/core/Polyfill.test.ts (1 hunks)
 ________________________________________________
< Squeezing intelligence out of LLMs since 2023. >
 ------------------------------------------------
  \
   \   (\__/)
       (•ㅅ•)
       /   づ

Walkthrough

The changes involve updates to the ShaderChunkLoader class where the regular expression _shaderIncludeRegex was modified to improve matching of shader includes without requiring preceding whitespace. A new Polyfill class was introduced in Polyfill.ts, providing a static method registerPolyfill to implement a polyfill for the String.prototype.matchAll method. Additionally, the index.ts file was updated to import the Polyfill class and invoke its registration method. A test suite for the polyfill was also created to ensure its functionality.

Changes

File Change Summary
packages/loader/src/ShaderChunkLoader.ts Updated _shaderIncludeRegex to match without preceding whitespace; _loadChunksInCode uses the updated regex.
packages/loader/src/Polyfill.ts Introduced Polyfill class with registerPolyfill and _registerMatchAll methods for polyfilling String.prototype.matchAll.
packages/loader/src/index.ts Added import for Polyfill and called Polyfill.registerPolyfill() for initialization.
tests/src/loader/Polyfill.test.ts New test suite for String.prototype.matchAll polyfill, validating behavior and error handling.

Poem

In the land of shaders, where colors play,
A new match method brightens the day.
With regex refined, and logic so neat,
Our code hops along, light on its feet!
A dance of includes, in patterns so grand,
A joyful update, crafted by hand! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Nov 21, 2024

Codecov Report

Attention: Patch coverage is 16.66667% with 15 lines in your changes missing coverage. Please review.

Project coverage is 70.73%. Comparing base (f9fc2c6) to head (a36417b).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
packages/core/src/Polyfill.ts 6.25% 14 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2443      +/-   ##
==========================================
- Coverage   70.78%   70.73%   -0.06%     
==========================================
  Files         524      526       +2     
  Lines       27487    27504      +17     
  Branches     4111     4113       +2     
==========================================
- Hits        19457    19454       -3     
- Misses       6477     6495      +18     
- Partials     1553     1555       +2     
Flag Coverage Δ
unittests 70.73% <16.66%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (1)
packages/loader/src/ShaderChunkLoader.ts (1)

15-15: LGTM! Consider adding documentation.

The regex pattern correctly identifies shader includes. Consider adding a comment explaining the pattern components for better maintainability.

-  private static _shaderIncludeRegex = /\s#include\s+"([./][^\\"]+)"/;
+  /** Matches shader include statements in the format: #include "./path/to/shader" */
+  private static _shaderIncludeRegex = /\s#include\s+"([./][^\\"]+)"/;
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between f9fc2c6 and df2399c.

📒 Files selected for processing (1)
  • packages/loader/src/ShaderChunkLoader.ts (2 hunks)
🧰 Additional context used
🪛 Biome
packages/loader/src/ShaderChunkLoader.ts

[error] 20-20: void is confusing outside a return type or a type parameter.

Unsafe fix: Use undefined instead.

(lint/suspicious/noConfusingVoidType)

🪛 GitHub Check: codecov/patch
packages/loader/src/ShaderChunkLoader.ts

[warning] 22-22: packages/loader/src/ShaderChunkLoader.ts#L22
Added line #L22 was not covered by tests


[warning] 44-45: packages/loader/src/ShaderChunkLoader.ts#L44-L45
Added lines #L44 - L45 were not covered by tests


[warning] 47-47: packages/loader/src/ShaderChunkLoader.ts#L47
Added line #L47 was not covered by tests


[warning] 49-51: packages/loader/src/ShaderChunkLoader.ts#L49-L51
Added lines #L49 - L51 were not covered by tests


[warning] 53-53: packages/loader/src/ShaderChunkLoader.ts#L53
Added line #L53 was not covered by tests

🔇 Additional comments (1)
packages/loader/src/ShaderChunkLoader.ts (1)

43-54: Verify browser compatibility requirements.

Before implementing a polyfill, let's verify if it's necessary based on the engine's browser support requirements.

Consider using existing polyfill libraries like core-js if broader compatibility is needed.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 44-45: packages/loader/src/ShaderChunkLoader.ts#L44-L45
Added lines #L44 - L45 were not covered by tests


[warning] 47-47: packages/loader/src/ShaderChunkLoader.ts#L47
Added line #L47 was not covered by tests


[warning] 49-51: packages/loader/src/ShaderChunkLoader.ts#L49-L51
Added lines #L49 - L51 were not covered by tests


[warning] 53-53: packages/loader/src/ShaderChunkLoader.ts#L53
Added line #L53 was not covered by tests

packages/loader/src/ShaderChunkLoader.ts Outdated Show resolved Hide resolved
packages/loader/src/ShaderChunkLoader.ts Outdated Show resolved Hide resolved
@Sway007 Sway007 marked this pull request as draft November 21, 2024 02:51
@Sway007 Sway007 marked this pull request as ready for review November 21, 2024 03:11
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Outside diff range and nitpick comments (8)
packages/loader/src/index.ts (1)

24-24: Consider grouping utility imports together.

The import looks good, but consider moving it up with other utility imports (if any) for better organization.

packages/loader/src/Polyfill.ts (3)

3-7: Consider using standalone functions instead of a static class

While the current implementation using a static class works, consider refactoring to use standalone functions for a more functional approach. However, if you plan to add more polyfills in the future, the class can serve as a good namespace.

-export class Polyfill {
-  static registerPolyFill() {
-    Polyfill._registerMatchAll();
-  }
-}
+export function registerPolyfills() {
+  registerMatchAllPolyfill();
+}
🧰 Tools
🪛 Biome

[error] 3-33: Avoid classes that contain only static members.

Prefer using simple functions instead of classes with only static members.

(lint/complexity/noStaticOnlyClass)


8-32: Add unit tests for the polyfill implementation

The PR objectives mention that tests haven't been added yet. This polyfill needs comprehensive testing to ensure compatibility.

Would you like me to help create a test suite that covers:

  • Basic matching functionality
  • Capture groups handling
  • Error cases (non-global regex)
  • Edge cases (empty strings, no matches)

9-10: Consider adding browser compatibility comment

Add a comment indicating which browsers/versions need this polyfill for better maintainability.

     if (!String.prototype.matchAll) {
+      // Polyfill needed for: IE11, older versions of Safari (<13), etc.
       Logger.info("polyfill String.prototype.matchAll");
tests/src/loader/Polyfill.test.ts (4)

5-12: Consider adding JSDoc comments for the test patterns

Adding documentation for the regex pattern and test content would improve maintainability and help other developers understand the test cases better.

+  /** Regex pattern to match GLSL include statements with relative or absolute paths */
   const regex = /#include\s+"([./][^\\"]+)"/gm;
+  /** Sample GLSL content with various include patterns to test */
   const content = `#include "./f1.glsl"

14-15: Add error handling for module import

The import could fail if the module is not available. Consider wrapping it in a try-catch block.

   String.prototype.matchAll = null;
-  await import("@galacean/engine-loader");
+  try {
+    await import("@galacean/engine-loader");
+  } catch (error) {
+    throw new Error("Failed to load polyfill module: " + error.message);
+  }

17-22: Consider adding more error cases

The test covers the missing global flag case, but consider adding tests for other edge cases:

  • null/undefined regex
  • invalid regex patterns
  • empty string content

Would you like me to provide example test cases for these scenarios?


3-39: Consider splitting the test into smaller, focused test cases

The current test contains multiple assertions testing different aspects of the polyfill. Consider splitting it into separate test cases:

  1. Basic functionality test
  2. Error handling test
  3. Edge cases test

This would improve test maintainability and make failures easier to diagnose.

🧰 Tools
🪛 Biome

[error] 25-25: This is an unexpected use of the debugger statement.

Unsafe fix: Remove debugger statement

(lint/suspicious/noDebugger)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between baee551 and 85bc53a.

📒 Files selected for processing (4)
  • packages/loader/src/Polyfill.ts (1 hunks)
  • packages/loader/src/ShaderChunkLoader.ts (1 hunks)
  • packages/loader/src/index.ts (2 hunks)
  • tests/src/loader/Polyfill.test.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/loader/src/ShaderChunkLoader.ts
🧰 Additional context used
🪛 Biome
packages/loader/src/Polyfill.ts

[error] 3-33: Avoid classes that contain only static members.

Prefer using simple functions instead of classes with only static members.

(lint/complexity/noStaticOnlyClass)

tests/src/loader/Polyfill.test.ts

[error] 25-25: This is an unexpected use of the debugger statement.

Unsafe fix: Remove debugger statement

(lint/suspicious/noDebugger)

🔇 Additional comments (2)
packages/loader/src/index.ts (1)

37-37: Reconsider the placement and implementation of polyfill registration.

The unconditional polyfill registration in a barrel file raises several concerns:

  1. Multiple registrations could occur if this file is imported multiple times
  2. Side effects in barrel files can lead to unexpected behavior
  3. No clear indication why this specific polyfill is needed at this level

Consider these alternatives:

  1. Move the registration to the application entry point
  2. Add a guard to prevent multiple registrations:
-Polyfill.registerPolyFill();
+if (!String.prototype.matchAll) {
+  Polyfill.registerPolyFill();
+}
  1. Document why this polyfill is necessary at this level

Let's verify the potential impact:

packages/loader/src/Polyfill.ts (1)

1-33: Verify all String.matchAll usage patterns

Let's verify all usage patterns of String.matchAll in the codebase to ensure this polyfill meets all requirements.

✅ Verification successful

Let me gather more context about the test cases to ensure the polyfill implementation matches the expected behavior.


Polyfill implementation correctly handles all usage patterns

The polyfill implementation has been verified against all its usages in the codebase:

  • Test cases explicitly verify the polyfill's behavior with global regex flag requirement
  • Test cases validate correct capture group handling and iteration
  • The only production usage in ShaderChunkLoader matches the tested pattern (#include regex with capture groups)
  • The implementation correctly throws TypeError for non-global regex as per spec
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for all String.matchAll usage patterns
rg -A 2 "\.matchAll\(" 

# Search for potential regex patterns that might be used with matchAll
ast-grep --pattern 'new RegExp($$$, $$$)'

Length of output: 1140


Script:

#!/bin/bash
# Get the test file content to understand the test cases
cat tests/src/loader/Polyfill.test.ts

# Get the shader chunk loader implementation to understand its usage
cat packages/loader/src/ShaderChunkLoader.ts

Length of output: 2982

🧰 Tools
🪛 Biome

[error] 3-33: Avoid classes that contain only static members.

Prefer using simple functions instead of classes with only static members.

(lint/complexity/noStaticOnlyClass)

packages/loader/src/Polyfill.ts Outdated Show resolved Hide resolved
packages/loader/src/Polyfill.ts Outdated Show resolved Hide resolved
tests/src/loader/Polyfill.test.ts Outdated Show resolved Hide resolved
tests/src/loader/Polyfill.test.ts Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (3)
packages/loader/src/Polyfill.ts (3)

1-3: Consider refactoring static class to module

The class contains only static members which is an anti-pattern in TypeScript. Consider refactoring to a module with named exports for better maintainability.

 import { Logger } from "@galacean/engine-core";

-export class Polyfill {
+export const registerPolyfill = () => {
+  registerMatchAll();
+};
+
+const registerMatchAll = () => {

4-6: Fix method name casing

The method name registerPolyFill uses inconsistent casing. In JavaScript/TypeScript, camelCase is the convention for method names.

-  static registerPolyFill() {
+  static registerPolyfill() {

11-11: Fix return type annotation

The return type annotation uses String instead of the primitive string type. TypeScript best practices recommend using lowercase primitive types.

-      String.prototype.matchAll = function (pattern: RegExp): ReturnType<String["matchAll"]> {
+      String.prototype.matchAll = function (pattern: RegExp): ReturnType<string["matchAll"]> {
🧰 Tools
🪛 Biome

[error] 11-11: Don't use 'String' as a type.

Use lowercase primitives for consistency.
Safe fix: Use 'string' instead

(lint/complexity/noBannedTypes)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 85bc53a and bfa39da.

📒 Files selected for processing (1)
  • packages/loader/src/Polyfill.ts (1 hunks)
🧰 Additional context used
🪛 Biome
packages/loader/src/Polyfill.ts

[error] 3-33: Avoid classes that contain only static members.

Prefer using simple functions instead of classes with only static members.

(lint/complexity/noStaticOnlyClass)


[error] 11-11: Don't use 'String' as a type.

Use lowercase primitives for consistency.
Safe fix: Use 'string' instead

(lint/complexity/noBannedTypes)

packages/loader/src/Polyfill.ts Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (3)
packages/loader/src/Polyfill.ts (3)

3-6: Consider refactoring to module-level functions

The class currently only contains static methods. Consider refactoring to module-level functions for better maintainability and simpler structure.

-export class Polyfill {
-  static registerPolyfill() {
-    Polyfill._registerMatchAll();
-  }
+export function registerPolyfill() {
+  _registerMatchAll();
+}
🧰 Tools
🪛 Biome

[error] 3-33: Avoid classes that contain only static members.

Prefer using simple functions instead of classes with only static members.

(lint/complexity/noStaticOnlyClass)


8-32: Add JSDoc documentation for the polyfill implementation

Add comprehensive JSDoc documentation to explain:

  • Purpose of the polyfill
  • Browser compatibility requirements
  • Parameters and return type
  • Usage examples
+/**
+ * Polyfills the String.prototype.matchAll method for browsers that don't support it.
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll
+ * 
+ * @example
+ * const str = 'test1test2';
+ * const regexp = /t(e)(st(\d?))/g;
+ * const matches = str.matchAll(regexp);
+ * for (const match of matches) {
+ *   console.log(match);
+ * }
+ */
 private static _registerMatchAll() {
🧰 Tools
🪛 Biome

[error] 11-11: Don't use 'String' as a type.

Use lowercase primitives for consistency.
Safe fix: Use 'string' instead

(lint/complexity/noBannedTypes)

🪛 GitHub Check: codecov/patch

[warning] 10-13: packages/loader/src/Polyfill.ts#L10-L13
Added lines #L10 - L13 were not covered by tests


[warning] 15-15: packages/loader/src/Polyfill.ts#L15
Added line #L15 was not covered by tests


[warning] 17-17: packages/loader/src/Polyfill.ts#L17
Added line #L17 was not covered by tests


[warning] 19-20: packages/loader/src/Polyfill.ts#L19-L20
Added lines #L19 - L20 were not covered by tests


[warning] 22-23: packages/loader/src/Polyfill.ts#L22-L23
Added lines #L22 - L23 were not covered by tests


[warning] 25-27: packages/loader/src/Polyfill.ts#L25-L27
Added lines #L25 - L27 were not covered by tests


11-11: Use lowercase primitive type

Replace String type with lowercase string for consistency with TypeScript best practices.

-      String.prototype.matchAll = function (pattern: RegExp): ReturnType<String["matchAll"]> {
+      String.prototype.matchAll = function (pattern: RegExp): ReturnType<string["matchAll"]> {
🧰 Tools
🪛 Biome

[error] 11-11: Don't use 'String' as a type.

Use lowercase primitives for consistency.
Safe fix: Use 'string' instead

(lint/complexity/noBannedTypes)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between ce4989d and 9516fdf.

📒 Files selected for processing (2)
  • packages/loader/src/Polyfill.ts (1 hunks)
  • packages/loader/src/index.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/loader/src/index.ts
🧰 Additional context used
🪛 Biome
packages/loader/src/Polyfill.ts

[error] 3-33: Avoid classes that contain only static members.

Prefer using simple functions instead of classes with only static members.

(lint/complexity/noStaticOnlyClass)


[error] 11-11: Don't use 'String' as a type.

Use lowercase primitives for consistency.
Safe fix: Use 'string' instead

(lint/complexity/noBannedTypes)

🪛 GitHub Check: codecov/patch
packages/loader/src/Polyfill.ts

[warning] 10-13: packages/loader/src/Polyfill.ts#L10-L13
Added lines #L10 - L13 were not covered by tests


[warning] 15-15: packages/loader/src/Polyfill.ts#L15
Added line #L15 was not covered by tests


[warning] 17-17: packages/loader/src/Polyfill.ts#L17
Added line #L17 was not covered by tests


[warning] 19-20: packages/loader/src/Polyfill.ts#L19-L20
Added lines #L19 - L20 were not covered by tests


[warning] 22-23: packages/loader/src/Polyfill.ts#L22-L23
Added lines #L22 - L23 were not covered by tests


[warning] 25-27: packages/loader/src/Polyfill.ts#L25-L27
Added lines #L25 - L27 were not covered by tests

🔇 Additional comments (1)
packages/loader/src/Polyfill.ts (1)

1-33: Add test coverage for the polyfill

The code coverage report indicates missing test coverage. Please add tests to verify:

  1. Basic functionality with global RegExp
  2. Error handling for non-global RegExp
  3. Edge cases (null matches, empty strings)
  4. Compatibility with the native implementation
🧰 Tools
🪛 Biome

[error] 3-33: Avoid classes that contain only static members.

Prefer using simple functions instead of classes with only static members.

(lint/complexity/noStaticOnlyClass)


[error] 11-11: Don't use 'String' as a type.

Use lowercase primitives for consistency.
Safe fix: Use 'string' instead

(lint/complexity/noBannedTypes)

🪛 GitHub Check: codecov/patch

[warning] 10-13: packages/loader/src/Polyfill.ts#L10-L13
Added lines #L10 - L13 were not covered by tests


[warning] 15-15: packages/loader/src/Polyfill.ts#L15
Added line #L15 was not covered by tests


[warning] 17-17: packages/loader/src/Polyfill.ts#L17
Added line #L17 was not covered by tests


[warning] 19-20: packages/loader/src/Polyfill.ts#L19-L20
Added lines #L19 - L20 were not covered by tests


[warning] 22-23: packages/loader/src/Polyfill.ts#L22-L23
Added lines #L22 - L23 were not covered by tests


[warning] 25-27: packages/loader/src/Polyfill.ts#L25-L27
Added lines #L25 - L27 were not covered by tests

@@ -0,0 +1,33 @@
import { Logger } from "@galacean/engine-core";

export class Polyfill {
Copy link
Member

Choose a reason for hiding this comment

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

Add internal comments

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (3)
packages/loader/src/Polyfill.ts (3)

3-6: Enhance documentation with more details.

The @internal JSDoc comment should provide more context about the purpose and usage of this polyfill.

Add more details to the documentation:

 /**
  * @internal
+ * Provides polyfills for missing String prototype methods in older JavaScript environments.
+ * Currently implements:
+ * - String.prototype.matchAll: Adds support for iterating over regex matches with capture groups
  */

6-10: Consider converting static-only class to namespace functions.

The Polyfill class only contains static members. Consider using a more functional approach with a namespace or individual functions.

Example refactor:

-export class Polyfill {
-  static registerPolyfill(): void {
-    Polyfill._registerMatchAll();
-  }
+export const registerPolyfills = (): void => {
+  registerMatchAll();
+};
+
+const registerMatchAll = (): void => {
🧰 Tools
🪛 Biome

[error] 6-36: Avoid classes that contain only static members.

Prefer using simple functions instead of classes with only static members.

(lint/complexity/noStaticOnlyClass)


14-14: Use lowercase primitive type.

Replace String with lowercase string in the return type for consistency with TypeScript conventions.

-  String.prototype.matchAll = function (pattern: RegExp): ReturnType<String["matchAll"]> {
+  String.prototype.matchAll = function (pattern: RegExp): ReturnType<string["matchAll"]> {
🧰 Tools
🪛 Biome

[error] 14-14: Don't use 'String' as a type.

Use lowercase primitives for consistency.
Safe fix: Use 'string' instead

(lint/complexity/noBannedTypes)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 9516fdf and 5d3132a.

📒 Files selected for processing (1)
  • packages/loader/src/Polyfill.ts (1 hunks)
🧰 Additional context used
🪛 Biome
packages/loader/src/Polyfill.ts

[error] 6-36: Avoid classes that contain only static members.

Prefer using simple functions instead of classes with only static members.

(lint/complexity/noStaticOnlyClass)


[error] 14-14: Don't use 'String' as a type.

Use lowercase primitives for consistency.
Safe fix: Use 'string' instead

(lint/complexity/noBannedTypes)

🔇 Additional comments (1)
packages/loader/src/Polyfill.ts (1)

17-19: LGTM! Proper error handling for non-global RegExp.

The implementation correctly validates the global flag requirement and throws an appropriate TypeError, matching the standard behavior.

@GuoLei1990 GuoLei1990 merged commit f4cdbf7 into galacean:main Nov 21, 2024
7 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compatibility risk There may be a certain compatibility risk, which needs to be verified by real machine testing high priority High priority issue loader
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants