Skip to content

Conversation

@JBR-0100
Copy link
Contributor

@JBR-0100 JBR-0100 commented Dec 1, 2025

Fixes #6078

Summary

This PR replaces all occurrences of the deprecated String.prototype.substr() method with String.prototype.slice() across the codebase.

Changes

  • Replaced .substr() with .slice() in 34 files
  • All replacements maintain the same functionality
  • Linter passes with no errors

Why slice over substring?

  • slice handles negative indices intuitively
  • slice matches the existing usage patterns in the codebase (e.g., substr(0, length-1)slice(0, -1))
  • slice is the modern recommended replacement for substr

Testing

  • ✅ Linter passes (npm run lint)
  • All existing functionality preserved through equivalent transformations

Fixes plotly#6078

String.prototype.substr is deprecated and should be replaced with
String.prototype.slice or String.prototype.substring.

This commit replaces all occurrences of .substr() with .slice() across
the codebase. The slice method is preferred as it handles negative
indices more intuitively and matches the existing usage patterns.
- Fixes test failure in svg_text_utils_test.js where slice(0, negative)
  returned a string instead of empty string (unlike substr).
- Fixes similar potential issues in rangeslider/defaults.js and
  cartesian/axes.js using Math.max(0, ...).
Copy link
Contributor

@camdecoster camdecoster left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! It looks like there a handful of build files that also use substr. Could you update those as well? You can ignore anything in stackgl_modules. Other than that, this looks good.

Fixed remaining deprecated substr() calls in build/test files:
- test/strict-d3.js
- test/jasmine/assets/domain_ref_components.js
- test/jasmine/bundle_tests/no_webgl_test.js
- test/jasmine/assets/custom_assertions.js
- test/jasmine/bundle_tests/plotschema_test.js

Excluded stackgl_modules as requested.
@JBR-0100
Copy link
Contributor Author

JBR-0100 commented Dec 3, 2025

Done! Found and fixed all the remaining substr calls in the build files (excluding stackgl_modules like you mentioned). Updated 5 files total.

@JBR-0100 JBR-0100 requested a review from camdecoster December 3, 2025 07:27
@JBR-0100
Copy link
Contributor Author

JBR-0100 commented Dec 3, 2025

Hey, while fixing those I noticed there are a few more substr calls in test/jasmine/tests/ (like snapshot_test.js, toimage_test.js, axes_test.js, etc.). Want me to update those too or are those fine to leave as-is?

@camdecoster
Copy link
Contributor

Ah, I had those filtered from my search. Yeah, go ahead. Might as well clean it all up.

@camdecoster
Copy link
Contributor

Just curious: why did you go with substring instead of slice for the test files?

@JBR-0100
Copy link
Contributor Author

JBR-0100 commented Dec 3, 2025

swapped substr to substring only because of a quick search/replace, not because of a deliberate choice. There’s no functional reason for substring here — shall I update these to slice as well to keep everything consistent ?

@camdecoster
Copy link
Contributor

Go ahead and use slice. That way we won't have to deal with the slight differences between the two methods.

Replaced substring() with slice() in test files to maintain
consistency with the source code changes where substr() was
replaced with slice().
@camdecoster
Copy link
Contributor

One last thing: could you please add a draftlog?

@JBR-0100
Copy link
Contributor Author

JBR-0100 commented Dec 3, 2025

I added a draftlog, Is everything good now?

@camdecoster camdecoster merged commit 27a23e1 into plotly:master Dec 4, 2025
5 checks passed
@camdecoster
Copy link
Contributor

Thanks for the contribution! There are plenty of other opportunities to update the syntax or switch from deprecated methods. We'd love to have more help getting things modernized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

String.substr is deprecated

2 participants