-
-
Notifications
You must be signed in to change notification settings - Fork 635
fix: detect stale Pro migration module specifiers #3232
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2752,8 +2752,10 @@ def check_pro_renderer_mode | |||||||||||||||||||||||||||||||||||||||||||||
| # caching, RSC), and may cause "component not registered" errors at runtime. | ||||||||||||||||||||||||||||||||||||||||||||||
| BASE_PACKAGE_IMPORT_PATTERN = %r{\bfrom\s+['"]react-on-rails(?:/[^'"]*)?['"]} | ||||||||||||||||||||||||||||||||||||||||||||||
| BASE_PACKAGE_REQUIRE_PATTERN = %r{\brequire\s*\(\s*['"]react-on-rails(?:/[^'"]*)?['"]\s*\)} | ||||||||||||||||||||||||||||||||||||||||||||||
| BASE_PACKAGE_MOCK_PATTERN = %r{\b\w+\.(?:mock|unmock|doMock|dontMock|requireActual|requireMock)\s*\(\s*['"]react-on-rails(?:/[^'"]*)?['"]} # rubocop:disable Layout/LineLength | ||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The inline
Suggested change
Side note: |
||||||||||||||||||||||||||||||||||||||||||||||
| BASE_PACKAGE_DECLARE_MODULE_PATTERN = %r{^\s*declare\s+module\s+['"]react-on-rails(?:/[^'"]*)?['"]} | ||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| def check_base_package_imports # rubocop:disable Metrics/CyclomaticComplexity | ||||||||||||||||||||||||||||||||||||||||||||||
| def check_base_package_imports # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity | ||||||||||||||||||||||||||||||||||||||||||||||
| source_path = resolve_js_source_path | ||||||||||||||||||||||||||||||||||||||||||||||
| js_extensions = %w[js jsx ts tsx] | ||||||||||||||||||||||||||||||||||||||||||||||
| js_patterns = js_extensions.map { |ext| "#{source_path}/**/*.#{ext}" } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -2762,7 +2764,10 @@ def check_base_package_imports # rubocop:disable Metrics/CyclomaticComplexity | |||||||||||||||||||||||||||||||||||||||||||||
| js_patterns.each do |pattern| | ||||||||||||||||||||||||||||||||||||||||||||||
| Dir.glob(pattern).each do |file| | ||||||||||||||||||||||||||||||||||||||||||||||
| content = File.read(file) | ||||||||||||||||||||||||||||||||||||||||||||||
| next unless content.match?(BASE_PACKAGE_IMPORT_PATTERN) || content.match?(BASE_PACKAGE_REQUIRE_PATTERN) | ||||||||||||||||||||||||||||||||||||||||||||||
| next unless content.match?(BASE_PACKAGE_IMPORT_PATTERN) || | ||||||||||||||||||||||||||||||||||||||||||||||
| content.match?(BASE_PACKAGE_REQUIRE_PATTERN) || | ||||||||||||||||||||||||||||||||||||||||||||||
| content.match?(BASE_PACKAGE_MOCK_PATTERN) || | ||||||||||||||||||||||||||||||||||||||||||||||
| content.match?(BASE_PACKAGE_DECLARE_MODULE_PATTERN) | ||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Warning message misleads for mock and declaration matchesLow Severity The new Reviewed by Cursor Bugbot for commit fa3c407. Configure here.
Comment on lines
2758
to
+2770
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The growing number of patterns and the two
Suggested change
This also makes it trivial to add a fifth pattern in the future without touching the loop logic or bumping the complexity metrics. |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| files_with_base_import << file | ||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2530,6 +2530,44 @@ class << self | |
| end | ||
| end | ||
|
|
||
| context "when JS tests mock the base package after a Pro migration" do | ||
| around do |example| | ||
| Dir.mktmpdir do |tmpdir| | ||
| Dir.chdir(tmpdir) do | ||
| FileUtils.mkdir_p("app/javascript/packs") | ||
| File.write("app/javascript/packs/app.test.ts", | ||
| "jest.mock('react-on-rails', () => ({ authenticityHeaders: jest.fn() }));\n") | ||
| example.run | ||
| end | ||
| end | ||
| end | ||
|
|
||
| it "reports warning" do | ||
| doctor.send(:check_base_package_imports) | ||
| warning_msgs = checker.messages.select { |m| m[:type] == :warning } | ||
| expect(warning_msgs.any? { |m| m[:content].include?("app.test.ts") }).to be true | ||
| end | ||
|
Comment on lines
+2533
to
+2549
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two gaps worth adding:
context "when JS tests mock the base package subpath (e.g. react-on-rails/client)" do
# ...
File.write("app/javascript/packs/app.test.ts",
"jest.mock('react-on-rails/client', () => ({}));\n")
# expects warning
end
context "when JS tests mock the Pro package (no warning expected)" do
# ...
File.write("app/javascript/packs/app.test.ts",
"jest.mock('react-on-rails-pro', () => ({}));\n")
# expects success, not warning
end |
||
| end | ||
|
|
||
| context "when TypeScript declaration files augment the base package after a Pro migration" do | ||
| around do |example| | ||
| Dir.mktmpdir do |tmpdir| | ||
| Dir.chdir(tmpdir) do | ||
| FileUtils.mkdir_p("app/javascript/types") | ||
| File.write("app/javascript/types/react-on-rails.d.ts", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| "declare module 'react-on-rails' {\n export function register(): void;\n}\n") | ||
| example.run | ||
| end | ||
| end | ||
| end | ||
|
|
||
| it "reports warning" do | ||
| doctor.send(:check_base_package_imports) | ||
| warning_msgs = checker.messages.select { |m| m[:type] == :warning } | ||
| expect(warning_msgs.any? { |m| m[:content].include?("react-on-rails.d.ts") }).to be true | ||
| end | ||
| end | ||
|
|
||
| context "when JS files correctly import from 'react-on-rails-pro'" do | ||
| around do |example| | ||
| Dir.mktmpdir do |tmpdir| | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
# rubocop:disable Layout/LineLengthis needed because of the long method list inline. A small readability improvement would be to extract the alternation into a named group or split across assignment lines:Not a blocker, just worth considering if this constant ever needs extending again.