Skip to content

Conversation

@sand1018
Copy link

@sand1018 sand1018 commented Mar 6, 2025

Description 描述

在使用 vite-plugin-uni-layouts 插件时,小程序的 page-meta 组件不能正常工作,因为它需要作为页面的直接子元素才能生效。当页面被布局组件包裹后,page-meta 就不再是页面的直接子元素了。

解决方案

修改了插件的 transform 方法,使其能够:

  1. 检测页面模板中的 page-meta 组件
  2. 将 page-meta 组件从原始内容中提取出来
  3. 在页面渲染时,将 page-meta 组件放在布局组件之外,作为页面的直接子元素

Linked Issues 关联的 Issues

fixes #34

Summary by CodeRabbit

  • New Features

    • Improved page-meta handling so meta content is correctly extracted and placed ahead of layouts, improving page rendering order.
  • Bug Fixes

    • More robust detection and binding of static/dynamic layouts and safer handling of missing values to reduce runtime issues.
  • Chores

    • Minor formatting and readability improvements across template processing.

@coderabbitai
Copy link

coderabbitai bot commented Mar 6, 2025

Walkthrough

Adds page-meta extraction and reinjection to SFC template processing, enhances dynamic/static layout name matching and emitted layout wrapper formatting, and introduces null-safe access patterns and minor template/formatting reflows in src/context.ts.

Changes

Cohort / File(s) Change Summary
Context implementation
src/context.ts
Adds collection and removal of page-meta elements from SFC templates, computes pageMetaContent and contentWithoutPageMeta, and injects extracted meta outside a generated layout wrapper. Refactors dynamic/static layout binding predicates and normalizes emitted layout wrapper string formatting; introduces additional null/undefined tolerant access and minor template reflow/formatting changes.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Parser as SFC Parser
  participant Context as src/context.ts
  participant Emitter as Layout Emitter

  Parser->>Context: provide SFC template AST/string
  Note over Context: find `page-meta` nodes\nand uni-layout bindings
  Context->>Context: extract `pageMetaContent`\ncompute `contentWithoutPageMeta`
  Context->>Emitter: generate layout wrapper (static/dynamic)\n(normalized formatting)
  Emitter-->>Context: wrapped template string
  Context-->>Parser: return modified template\n(with page-meta prefixed and wrapped content)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20-30 minutes

Poem

I nibble through templates at dawn’s first light,
Pulling page-meta gently out of sight.
I stitch a layout, tidy and neat,
Hop, tuck, and format — a rabbit’s feat.
Bravo, small changes — a carrot-worthy bite! 🥕🐇

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The pull request title "feat: add support for page-meta component in Mini Program" directly addresses the primary objective of the changeset. The raw summary confirms that the main changes involve adding page-meta handling by collecting page-meta elements from SFC templates, extracting them from the original content, and injecting them outside the layout wrapper to maintain proper DOM hierarchy. The title is clear, concise, and accurately reflects this core feature addition without unnecessary verbosity.
Linked Issues Check ✅ Passed The code changes align with the requirements from linked issue #34. The issue seeks to enable page-meta/page-style support to address scroll-through problems caused by popups. The PR implements this by detecting page-meta elements in page templates, extracting them from the original content, and rendering them outside the layout component wrapper, ensuring page-meta remains a direct child of the page as required. This addresses the core objective of supporting page-meta functionality within the layout plugin context.
Out of Scope Changes Check ✅ Passed The changes in this PR are focused and aligned with the objective of adding page-meta support. The raw summary indicates modifications include page-meta extraction and injection logic, refinements to dynamic layout generation binding logic necessary for proper layout handling, safety improvements with null/undefined tolerant access patterns, and formatting improvements for readability. All modifications support the stated objective of enabling page-meta functionality within the layout system, with no apparent unrelated or tangential changes present.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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

🧹 Nitpick comments (8)
src/context.ts (8)

1-17: Consistency in Import Statements
The imports and type declarations now end consistently with semicolons, which improves readability. However, note that the static analysis hints indicate potential issues with string quoting (e.g. preferring single quotes) and extra semicolons. Please double-check that these formatting choices align with your team’s ESLint configuration and overall coding standards.

🧰 Tools
🪛 ESLint

[error] 1-1: Strings must use singlequote.

(style/quotes)


[error] 1-1: Extra semicolon.

(style/semi)


[error] 2-2: Strings must use singlequote.

(style/quotes)


[error] 2-2: Extra semicolon.

(style/semi)


[error] 8-8: Strings must use singlequote.

(style/quotes)


[error] 8-8: Extra semicolon.

(style/semi)


[error] 9-9: Strings must use singlequote.

(style/quotes)


[error] 9-9: Extra semicolon.

(style/semi)


[error] 10-10: Strings must use singlequote.

(style/quotes)


[error] 10-10: Extra semicolon.

(style/semi)


[error] 11-11: Strings must use singlequote.

(style/quotes)


[error] 11-11: Extra semicolon.

(style/semi)


[error] 12-12: Strings must use singlequote.

(style/quotes)


[error] 12-12: Extra semicolon.

(style/semi)


[error] 13-13: Strings must use singlequote.

(style/quotes)


[error] 13-13: Extra semicolon.

(style/semi)


[error] 14-14: Strings must use singlequote.

(style/quotes)


[error] 14-14: Extra semicolon.

(style/semi)


[error] 15-15: Strings must use singlequote.

(style/quotes)


[error] 15-15: Extra semicolon.

(style/semi)


[error] 16-16: Strings must use singlequote.

(style/quotes)


[error] 16-16: Extra semicolon.

(style/semi)


18-30: Uniform Class Property Declarations and Constructor Initialization
The property declarations and constructor assignments now end with semicolons, enforcing a consistent style. This change is purely cosmetic and does not affect functionality. Just ensure that the style (including semicolon usage) is in agreement with your project’s guidelines.

🧰 Tools
🪛 ESLint

[error] 19-19: Extra semicolon.

(style/semi)


[error] 20-20: Extra semicolon.

(style/semi)


[error] 21-21: Extra semicolon.

(style/semi)


[error] 22-22: Extra semicolon.

(style/semi)


[error] 23-23: Extra semicolon.

(style/semi)


[error] 24-24: Extra semicolon.

(style/semi)


[error] 26-26: Extra semicolon.

(style/semi)


[error] 27-27: Extra semicolon.

(style/semi)


[error] 28-28: Extra semicolon.

(style/semi)


[error] 29-29: Strings must use singlequote.

(style/quotes)


[error] 29-29: Extra semicolon.

(style/semi)


32-37: Consistent Formatting in Vite Server Setup
The setupViteServer method now uses semicolons after assignments and control flow statements. The early return in line 33 (after checking for an identical server instance) is concise, though double-check that the newline after the if condition satisfies your style rules.

🧰 Tools
🪛 ESLint

[error] 33-33: Expect newline after if

(antfu/if-newline)


[error] 33-33: Extra semicolon.

(style/semi)


[error] 35-35: Extra semicolon.

(style/semi)


[error] 36-36: Extra semicolon.

(style/semi)


39-46: Event Handling in Watcher Setup
The setupWatcher method leverages consistent semicolon usage in its event listener registration. The inline callback formatting (including the semicolon in line 45) maintains uniformity—just verify that this aligns with your coding standards.

🧰 Tools
🪛 ESLint

[error] 40-40: Strings must use singlequote.

(style/quotes)


[error] 41-41: Strings must use singlequote.

(style/quotes)


[error] 42-42: Extra semicolon.

(style/semi)


[error] 45-45: Extra semicolon.

(style/semi)


105-112: Page-Meta Component Extraction
The extraction logic for page-meta components via filtering the template AST is implemented correctly. As an optional refinement, consider normalizing the condition by always applying kebabCase to v.tag if the tag naming is reliably consistent.

🧰 Tools
🪛 ESLint

[error] 106-106: Extra semicolon.

(style/semi)


[error] 109-109: Unexpected parentheses around single function argument having a body with no curly braces.

(arrow-parens)


[error] 110-110: '&&' should be placed at the beginning of the line.

(style/operator-linebreak)


[error] 111-111: Strings must use singlequote.

(style/quotes)


[error] 111-111: Strings must use singlequote.

(style/quotes)


[error] 111-112: Missing trailing comma.

(style/comma-dangle)


[error] 112-112: Extra semicolon.

(style/semi)


129-154: Reinserting Page-Meta Content Outside the Layout
The else branch elegantly extracts the content of the page-meta components, removes them from the original template, and then injects them outside the layout component. One suggestion: instead of using string replacement in a loop (lines 137–141), consider using MagicString’s dedicated removal methods (if available) to avoid potential pitfalls with non-unique substrings.

🧰 Tools
🪛 ESLint

[error] 132-132: Unexpected parentheses around single function argument having a body with no curly braces.

(arrow-parens)


[error] 133-133: Strings must use singlequote.

(style/quotes)


[error] 133-133: Extra semicolon.

(style/semi)


[error] 136-136: Extra semicolon.

(style/semi)


[error] 140-140: Strings must use singlequote.

(style/quotes)


[error] 140-141: Missing trailing comma.

(style/comma-dangle)


[error] 141-141: Extra semicolon.

(style/semi)


[error] 151-151: Strings must use singlequote.

(style/quotes)


[error] 151-152: Missing trailing comma.

(style/comma-dangle)


[error] 152-153: Missing trailing comma.

(style/comma-dangle)


[error] 153-153: Extra semicolon.

(style/semi)


193-262: Responsive Dynamic Layout Generation
The generateDynamicLayout method deftly handles both static and dynamic layout name bindings to reconstruct the layout markup. The bifurcation for Mini Program (MP) environments using the isMp flag and the corresponding template generation is well implemented. The use of a console warning for a missing layout bind is a useful debugging aid.

🧰 Tools
🪛 ESLint

[error] 195-195: Unexpected parentheses around single function argument having a body with no curly braces.

(arrow-parens)


[error] 195-195: Strings must use singlequote.

(style/quotes)


[error] 195-196: Missing trailing comma.

(style/comma-dangle)


[error] 196-196: Extra semicolon.

(style/semi)


[error] 198-198: Unexpected parentheses around single function argument having a body with no curly braces.

(arrow-parens)


[error] 199-199: '&&' should be placed at the beginning of the line.

(style/operator-linebreak)


[error] 200-200: Strings must use singlequote.

(style/quotes)


[error] 200-200: '&&' should be placed at the beginning of the line.

(style/operator-linebreak)


[error] 201-201: '&&' should be placed at the beginning of the line.

(style/operator-linebreak)


[error] 202-202: Strings must use singlequote.

(style/quotes)


[error] 202-202: '&&' should be placed at the beginning of the line.

(style/operator-linebreak)


[error] 203-203: '&&' should be placed at the beginning of the line.

(style/operator-linebreak)


[error] 204-205: Missing trailing comma.

(style/comma-dangle)


[error] 205-205: Extra semicolon.

(style/semi)


[error] 206-206: Unexpected parentheses around single function argument having a body with no curly braces.

(arrow-parens)


[error] 206-206: Strings must use singlequote.

(style/quotes)


[error] 206-206: Extra semicolon.

(style/semi)


[error] 209-209: Unexpected parentheses around single function argument having a body with no curly braces.

(arrow-parens)


[error] 210-211: Missing trailing comma.

(style/comma-dangle)


[error] 212-212: Unexpected parentheses around single function argument having a body with no curly braces.

(arrow-parens)


[error] 212-212: Extra semicolon.

(style/semi)


[error] 215-217: Expected { after 'if' condition.

(curly)


[error] 216-216: Strings must use singlequote.

(style/quotes)


[error] 216-217: Missing trailing comma.

(style/comma-dangle)


[error] 217-217: Extra semicolon.

(style/semi)


[error] 220-220: Extra semicolon.

(style/semi)


[error] 222-222: Extra semicolon.

(style/semi)


[error] 224-224: Strings must use singlequote.

(style/quotes)


[error] 224-225: Missing trailing comma.

(style/comma-dangle)


[error] 225-225: Extra semicolon.

(style/semi)


[error] 228-228: Extra semicolon.

(style/semi)


[error] 232-232: Unexpected parentheses around single function argument having a body with no curly braces.

(arrow-parens)


[error] 232-232: Strings must use singlequote.

(style/quotes)


[error] 232-233: Missing trailing comma.

(style/comma-dangle)


[error] 233-233: Extra semicolon.

(style/semi)


[error] 234-234: Expect newline after if

(antfu/if-newline)


[error] 234-234: Strings must use singlequote.

(style/quotes)


[error] 234-234: Extra semicolon.

(style/semi)


[error] 236-236: Extra semicolon.

(style/semi)


[error] 237-237: Extra semicolon.

(style/semi)


[error] 238-238: Unexpected parentheses around single function argument having a body with no curly braces.

(arrow-parens)


[error] 238-238: Strings must use singlequote.

(style/quotes)


[error] 238-238: Extra semicolon.

(style/semi)


[error] 242-242: Strings must use singlequote.

(style/quotes)


[error] 242-242: Strings must use singlequote.

(style/quotes)


[error] 244-244: Strings must use singlequote.

(style/quotes)


[error] 244-245: Missing trailing comma.

(style/comma-dangle)


[error] 245-246: Missing trailing comma.

(style/comma-dangle)


[error] 246-246: Extra semicolon.

(style/semi)


[error] 247-247: Extra semicolon.

(style/semi)


[error] 249-249: Strings must use singlequote.

(style/quotes)


[error] 249-249: Extra semicolon.

(style/semi)


[error] 250-250: Closing curly brace appears on the same line as the subsequent block.

(style/brace-style)


[error] 251-251: Extra semicolon.

(style/semi)


[error] 253-253: Expected { after 'if' condition.

(curly)


[error] 253-253: Extra semicolon.

(style/semi)


[error] 255-259: Expected { after 'else'.

(curly)


[error] 258-259: Missing trailing comma.

(style/comma-dangle)


[error] 259-259: Extra semicolon.

(style/semi)


[error] 260-260: Strings must use singlequote.

(style/quotes)


[error] 260-260: Extra semicolon.

(style/semi)


264-291: Injection of Layout Components into the Application Instance
The importLayoutComponents method builds import and registration strings for layout components and uses a regex-based replacement to inject component registration code into the createApp block. While effective, the regex (lines 278–280) is somewhat “magic” and could be brittle if the surrounding source changes. It may help to document the assumptions behind it for future maintainers.

🧰 Tools
🪛 ESLint

[error] 265-265: Extra semicolon.

(style/semi)


[error] 266-266: Extra semicolon.

(style/semi)


[error] 267-267: Extra semicolon.

(style/semi)


[error] 270-271: Missing trailing comma.

(style/comma-dangle)


[error] 271-271: Extra semicolon.

(style/semi)


[error] 273-274: Missing trailing comma.

(style/comma-dangle)


[error] 274-274: Extra semicolon.

(style/semi)


[error] 276-276: Strings must use singlequote.

(style/quotes)


[error] 276-276: Extra semicolon.

(style/semi)


[error] 279-279: Strings must use singlequote.

(style/quotes)


[error] 279-280: Missing trailing comma.

(style/comma-dangle)


[error] 280-280: Extra semicolon.

(style/semi)


[error] 285-285: Extra semicolon.

(style/semi)


[error] 286-286: Extra semicolon.

(style/semi)


[error] 290-290: Extra semicolon.

(style/semi)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9b0f812 and fc11247.

📒 Files selected for processing (1)
  • src/context.ts (1 hunks)
🧰 Additional context used
🪛 ESLint
src/context.ts

[error] 1-1: Strings must use singlequote.

(style/quotes)


[error] 1-1: Extra semicolon.

(style/semi)


[error] 2-2: Strings must use singlequote.

(style/quotes)


[error] 2-2: Extra semicolon.

(style/semi)


[error] 8-8: Strings must use singlequote.

(style/quotes)


[error] 8-8: Extra semicolon.

(style/semi)


[error] 9-9: Strings must use singlequote.

(style/quotes)


[error] 9-9: Extra semicolon.

(style/semi)


[error] 10-10: Strings must use singlequote.

(style/quotes)


[error] 10-10: Extra semicolon.

(style/semi)


[error] 11-11: Strings must use singlequote.

(style/quotes)


[error] 11-11: Extra semicolon.

(style/semi)


[error] 12-12: Strings must use singlequote.

(style/quotes)


[error] 12-12: Extra semicolon.

(style/semi)


[error] 13-13: Strings must use singlequote.

(style/quotes)


[error] 13-13: Extra semicolon.

(style/semi)


[error] 14-14: Strings must use singlequote.

(style/quotes)


[error] 14-14: Extra semicolon.

(style/semi)


[error] 15-15: Strings must use singlequote.

(style/quotes)


[error] 15-15: Extra semicolon.

(style/semi)


[error] 16-16: Strings must use singlequote.

(style/quotes)


[error] 16-16: Extra semicolon.

(style/semi)


[error] 19-19: Extra semicolon.

(style/semi)


[error] 20-20: Extra semicolon.

(style/semi)


[error] 21-21: Extra semicolon.

(style/semi)


[error] 22-22: Extra semicolon.

(style/semi)


[error] 23-23: Extra semicolon.

(style/semi)


[error] 24-24: Extra semicolon.

(style/semi)


[error] 26-26: Extra semicolon.

(style/semi)


[error] 27-27: Extra semicolon.

(style/semi)


[error] 28-28: Extra semicolon.

(style/semi)


[error] 29-29: Strings must use singlequote.

(style/quotes)


[error] 29-29: Extra semicolon.

(style/semi)


[error] 33-33: Expect newline after if

(antfu/if-newline)


[error] 33-33: Extra semicolon.

(style/semi)


[error] 35-35: Extra semicolon.

(style/semi)


[error] 36-36: Extra semicolon.

(style/semi)


[error] 40-40: Strings must use singlequote.

(style/quotes)


[error] 41-41: Strings must use singlequote.

(style/quotes)


[error] 42-42: Extra semicolon.

(style/semi)


[error] 45-45: Extra semicolon.

(style/semi)


[error] 50-50: Expect newline after if

(antfu/if-newline)


[error] 50-50: Extra semicolon.

(style/semi)


[error] 53-53: Extra semicolon.

(style/semi)


[error] 59-60: Missing trailing comma.

(style/comma-dangle)


[error] 60-60: Extra semicolon.

(style/semi)


[error] 63-63: Expect newline after if

(antfu/if-newline)


[error] 63-63: Extra semicolon.

(style/semi)


[error] 65-65: Extra semicolon.

(style/semi)


[error] 66-66: Extra semicolon.

(style/semi)


[error] 67-67: Extra semicolon.

(style/semi)


[error] 69-69: Strings must use singlequote.

(style/quotes)


[error] 70-70: Strings must use singlequote.

(style/quotes)


[error] 70-70: Extra semicolon.

(style/semi)


[error] 72-72: Strings must use singlequote.

(style/quotes)


[error] 74-74: Expect newline after if

(antfu/if-newline)


[error] 74-74: Extra semicolon.

(style/semi)


[error] 76-76: Unexpected parentheses around single function argument having a body with no curly braces.

(arrow-parens)


[error] 76-77: Missing trailing comma.

(style/comma-dangle)


[error] 77-77: Extra semicolon.

(style/semi)


[error] 79-79: Expect newline after if

(antfu/if-newline)


[error] 79-79: Extra semicolon.

(style/semi)


[error] 81-81: Strings must use singlequote.

(style/quotes)


[error] 81-81: Extra semicolon.

(style/semi)


[error] 83-83: Extra semicolon.

(style/semi)


[error] 84-84: Extra semicolon.

(style/semi)


[error] 85-85: Extra semicolon.

(style/semi)


[error] 88-88: Extra semicolon.

(style/semi)


[error] 91-91: Strings must use singlequote.

(style/quotes)


[error] 92-92: '=' should be placed at the beginning of the line.

(style/operator-linebreak)


[error] 93-93: Strings must use singlequote.

(style/quotes)


[error] 93-93: Strings must use singlequote.

(style/quotes)


[error] 93-93: Extra semicolon.

(style/semi)


[error] 94-94: '=' should be placed at the beginning of the line.

(style/operator-linebreak)


[error] 95-95: Strings must use singlequote.

(style/quotes)


[error] 95-95: '&&' should be placed at the beginning of the line.

(style/operator-linebreak)


[error] 96-96: Strings must use singlequote.

(style/quotes)


[error] 96-96: '&&' should be placed at the beginning of the line.

(style/operator-linebreak)


[error] 97-97: Strings must use singlequote.

(style/quotes)


[error] 97-97: Extra semicolon.

(style/semi)


[error] 99-99: Extra semicolon.

(style/semi)


[error] 102-102: Extra semicolon.

(style/semi)


[error] 106-106: Extra semicolon.

(style/semi)


[error] 109-109: Unexpected parentheses around single function argument having a body with no curly braces.

(arrow-parens)


[error] 110-110: '&&' should be placed at the beginning of the line.

(style/operator-linebreak)


[error] 111-111: Strings must use singlequote.

(style/quotes)


[error] 111-111: Strings must use singlequote.

(style/quotes)


[error] 111-112: Missing trailing comma.

(style/comma-dangle)


[error] 112-112: Extra semicolon.

(style/semi)


[error] 118-118: Unexpected parentheses around single function argument having a body with no curly braces.

(arrow-parens)


[error] 118-118: Strings must use singlequote.

(style/quotes)


[error] 118-119: Missing trailing comma.

(style/comma-dangle)


[error] 119-119: Extra semicolon.

(style/semi)


[error] 121-121: Expect newline after if

(antfu/if-newline)


[error] 121-121: Extra semicolon.

(style/semi)


[error] 126-127: Missing trailing comma.

(style/comma-dangle)


[error] 127-127: Extra semicolon.

(style/semi)


[error] 128-128: Closing curly brace appears on the same line as the subsequent block.

(style/brace-style)


[error] 132-132: Unexpected parentheses around single function argument having a body with no curly braces.

(arrow-parens)


[error] 133-133: Strings must use singlequote.

(style/quotes)


[error] 133-133: Extra semicolon.

(style/semi)


[error] 136-136: Extra semicolon.

(style/semi)


[error] 140-140: Strings must use singlequote.

(style/quotes)


[error] 140-141: Missing trailing comma.

(style/comma-dangle)


[error] 141-141: Extra semicolon.

(style/semi)


[error] 151-151: Strings must use singlequote.

(style/quotes)


[error] 151-152: Missing trailing comma.

(style/comma-dangle)


[error] 152-153: Missing trailing comma.

(style/comma-dangle)


[error] 153-153: Extra semicolon.

(style/semi)


[error] 162-162: Extra semicolon.

(style/semi)


[error] 166-166: Extra semicolon.

(style/semi)


[error] 171-171: Extra semicolon.

(style/semi)


[error] 172-172: Extra semicolon.

(style/semi)


[error] 175-176: Missing trailing comma.

(style/comma-dangle)


[error] 176-176: Extra semicolon.

(style/semi)


[error] 178-179: Missing trailing comma.

(style/comma-dangle)


[error] 179-179: Extra semicolon.

(style/semi)


[error] 180-180: Extra semicolon.

(style/semi)


[error] 181-181: Extra semicolon.

(style/semi)


[error] 182-182: Strings must use singlequote.

(style/quotes)


[error] 184-184: Strings must use singlequote.

(style/quotes)


[error] 188-188: Strings must use singlequote.

(style/quotes)


[error] 190-190: Extra semicolon.

(style/semi)


[error] 195-195: Unexpected parentheses around single function argument having a body with no curly braces.

(arrow-parens)


[error] 195-195: Strings must use singlequote.

(style/quotes)


[error] 195-196: Missing trailing comma.

(style/comma-dangle)


[error] 196-196: Extra semicolon.

(style/semi)


[error] 198-198: Unexpected parentheses around single function argument having a body with no curly braces.

(arrow-parens)


[error] 199-199: '&&' should be placed at the beginning of the line.

(style/operator-linebreak)


[error] 200-200: Strings must use singlequote.

(style/quotes)


[error] 200-200: '&&' should be placed at the beginning of the line.

(style/operator-linebreak)


[error] 201-201: '&&' should be placed at the beginning of the line.

(style/operator-linebreak)


[error] 202-202: Strings must use singlequote.

(style/quotes)


[error] 202-202: '&&' should be placed at the beginning of the line.

(style/operator-linebreak)


[error] 203-203: '&&' should be placed at the beginning of the line.

(style/operator-linebreak)


[error] 204-205: Missing trailing comma.

(style/comma-dangle)


[error] 205-205: Extra semicolon.

(style/semi)


[error] 206-206: Unexpected parentheses around single function argument having a body with no curly braces.

(arrow-parens)


[error] 206-206: Strings must use singlequote.

(style/quotes)


[error] 206-206: Extra semicolon.

(style/semi)


[error] 209-209: Unexpected parentheses around single function argument having a body with no curly braces.

(arrow-parens)


[error] 210-211: Missing trailing comma.

(style/comma-dangle)


[error] 212-212: Unexpected parentheses around single function argument having a body with no curly braces.

(arrow-parens)


[error] 212-212: Extra semicolon.

(style/semi)


[error] 215-217: Expected { after 'if' condition.

(curly)


[error] 216-216: Strings must use singlequote.

(style/quotes)


[error] 216-217: Missing trailing comma.

(style/comma-dangle)


[error] 217-217: Extra semicolon.

(style/semi)


[error] 220-220: Extra semicolon.

(style/semi)


[error] 222-222: Extra semicolon.

(style/semi)


[error] 224-224: Strings must use singlequote.

(style/quotes)


[error] 224-225: Missing trailing comma.

(style/comma-dangle)


[error] 225-225: Extra semicolon.

(style/semi)


[error] 228-228: Extra semicolon.

(style/semi)


[error] 232-232: Unexpected parentheses around single function argument having a body with no curly braces.

(arrow-parens)


[error] 232-232: Strings must use singlequote.

(style/quotes)


[error] 232-233: Missing trailing comma.

(style/comma-dangle)


[error] 233-233: Extra semicolon.

(style/semi)


[error] 234-234: Expect newline after if

(antfu/if-newline)


[error] 234-234: Strings must use singlequote.

(style/quotes)


[error] 234-234: Extra semicolon.

(style/semi)


[error] 236-236: Extra semicolon.

(style/semi)


[error] 237-237: Extra semicolon.

(style/semi)


[error] 238-238: Unexpected parentheses around single function argument having a body with no curly braces.

(arrow-parens)


[error] 238-238: Strings must use singlequote.

(style/quotes)


[error] 238-238: Extra semicolon.

(style/semi)


[error] 242-242: Strings must use singlequote.

(style/quotes)


[error] 242-242: Strings must use singlequote.

(style/quotes)


[error] 244-244: Strings must use singlequote.

(style/quotes)


[error] 244-245: Missing trailing comma.

(style/comma-dangle)


[error] 245-246: Missing trailing comma.

(style/comma-dangle)


[error] 246-246: Extra semicolon.

(style/semi)


[error] 247-247: Extra semicolon.

(style/semi)


[error] 249-249: Strings must use singlequote.

(style/quotes)


[error] 249-249: Extra semicolon.

(style/semi)


[error] 250-250: Closing curly brace appears on the same line as the subsequent block.

(style/brace-style)


[error] 251-251: Extra semicolon.

(style/semi)


[error] 253-253: Expected { after 'if' condition.

(curly)


[error] 253-253: Extra semicolon.

(style/semi)


[error] 255-259: Expected { after 'else'.

(curly)


[error] 258-259: Missing trailing comma.

(style/comma-dangle)


[error] 259-259: Extra semicolon.

(style/semi)


[error] 260-260: Strings must use singlequote.

(style/quotes)


[error] 260-260: Extra semicolon.

(style/semi)


[error] 265-265: Extra semicolon.

(style/semi)


[error] 266-266: Extra semicolon.

(style/semi)


[error] 267-267: Extra semicolon.

(style/semi)


[error] 270-271: Missing trailing comma.

(style/comma-dangle)


[error] 271-271: Extra semicolon.

(style/semi)


[error] 273-274: Missing trailing comma.

(style/comma-dangle)


[error] 274-274: Extra semicolon.

(style/semi)


[error] 276-276: Strings must use singlequote.

(style/quotes)


[error] 276-276: Extra semicolon.

(style/semi)


[error] 279-279: Strings must use singlequote.

(style/quotes)


[error] 279-280: Missing trailing comma.

(style/comma-dangle)


[error] 280-280: Extra semicolon.

(style/semi)


[error] 285-285: Extra semicolon.

(style/semi)


[error] 286-286: Extra semicolon.

(style/semi)


[error] 290-290: Extra semicolon.

(style/semi)

🔇 Additional comments (5)
src/context.ts (5)

48-68: Early Returns and Target Page Detection in Transform Method
The first section of the transform method checks for available layouts and pages, then retrieves the target page via getTarget. The addition of semicolons in these conditionals and assignments improves consistency without altering the logic.

🧰 Tools
🪛 ESLint

[error] 50-50: Expect newline after if

(antfu/if-newline)


[error] 50-50: Extra semicolon.

(style/semi)


[error] 53-53: Extra semicolon.

(style/semi)


[error] 59-60: Missing trailing comma.

(style/comma-dangle)


[error] 60-60: Extra semicolon.

(style/semi)


[error] 63-63: Expect newline after if

(antfu/if-newline)


[error] 63-63: Extra semicolon.

(style/semi)


[error] 65-65: Extra semicolon.

(style/semi)


[error] 66-66: Extra semicolon.

(style/semi)


[error] 67-67: Extra semicolon.

(style/semi)


83-103: AST Parsing and 'uniLayout' Variable Detection
Using babelParse to obtain the AST from the script setup and then traversing it with walkAST is implemented correctly. The detection of a variable named uniLayout (and appending a corresponding property if it’s a ref) is both clear and functional.

🧰 Tools
🪛 ESLint

[error] 83-83: Extra semicolon.

(style/semi)


[error] 84-84: Extra semicolon.

(style/semi)


[error] 85-85: Extra semicolon.

(style/semi)


[error] 88-88: Extra semicolon.

(style/semi)


[error] 91-91: Strings must use singlequote.

(style/quotes)


[error] 92-92: '=' should be placed at the beginning of the line.

(style/operator-linebreak)


[error] 93-93: Strings must use singlequote.

(style/quotes)


[error] 93-93: Strings must use singlequote.

(style/quotes)


[error] 93-93: Extra semicolon.

(style/semi)


[error] 94-94: '=' should be placed at the beginning of the line.

(style/operator-linebreak)


[error] 95-95: Strings must use singlequote.

(style/quotes)


[error] 95-95: '&&' should be placed at the beginning of the line.

(style/operator-linebreak)


[error] 96-96: Strings must use singlequote.

(style/quotes)


[error] 96-96: '&&' should be placed at the beginning of the line.

(style/operator-linebreak)


[error] 97-97: Strings must use singlequote.

(style/quotes)


[error] 97-97: Extra semicolon.

(style/semi)


[error] 99-99: Extra semicolon.

(style/semi)


[error] 102-102: Extra semicolon.

(style/semi)


115-128: Dynamic Layout Handling in Disabled Mode
In the disabled branch of the transform method, the code locates the uni-layout element and uses ms.overwrite in conjunction with this.generateDynamicLayout to update the template. This approach is sound; however, verify that returning early when the layout node isn’t found (line 121) is the intended behavior for all edge cases.

🧰 Tools
🪛 ESLint

[error] 118-118: Unexpected parentheses around single function argument having a body with no curly braces.

(arrow-parens)


[error] 118-118: Strings must use singlequote.

(style/quotes)


[error] 118-119: Missing trailing comma.

(style/comma-dangle)


[error] 119-119: Extra semicolon.

(style/semi)


[error] 121-121: Expect newline after if

(antfu/if-newline)


[error] 121-121: Extra semicolon.

(style/semi)


[error] 126-127: Missing trailing comma.

(style/comma-dangle)


[error] 127-127: Extra semicolon.

(style/semi)


[error] 128-128: Closing curly brace appears on the same line as the subsequent block.

(style/brace-style)


157-167: Source Map Generation Post-Transformation
The check using ms.hasChanged() and the subsequent generation of a source map are standard practices that ensure the transformation is tracked accurately. This implementation is clear and effective.

🧰 Tools
🪛 ESLint

[error] 162-162: Extra semicolon.

(style/semi)


[error] 166-166: Extra semicolon.

(style/semi)


170-191: Dynamic Import and Component Registration via Virtual Module
The virtualModule method correctly constructs layout import statements and registers components with the app. The concatenation of strings and usage of template literals are clear. Just confirm that runtime component registration via this virtual module integrates seamlessly with the rest of your application architecture.

🧰 Tools
🪛 ESLint

[error] 171-171: Extra semicolon.

(style/semi)


[error] 172-172: Extra semicolon.

(style/semi)


[error] 175-176: Missing trailing comma.

(style/comma-dangle)


[error] 176-176: Extra semicolon.

(style/semi)


[error] 178-179: Missing trailing comma.

(style/comma-dangle)


[error] 179-179: Extra semicolon.

(style/semi)


[error] 180-180: Extra semicolon.

(style/semi)


[error] 181-181: Extra semicolon.

(style/semi)


[error] 182-182: Strings must use singlequote.

(style/quotes)


[error] 184-184: Strings must use singlequote.

(style/quotes)


[error] 188-188: Strings must use singlequote.

(style/quotes)


[error] 190-190: Extra semicolon.

(style/semi)

@KeJunMao
Copy link
Member

KeJunMao commented Aug 28, 2025

抱歉现在才来看,请问你有时间来处理下 lint 的问题吗?

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: 3

🧹 Nitpick comments (3)
src/context.ts (3)

79-81: Minor: consider pre-indexing layouts by name for O(1) lookup.

If transform runs often, turning this linear search into a map lookup would reduce overhead.


110-119: Scope page-meta detection to Mini Program builds.

page-meta is MP-only. Guarding with isMp avoids doing unnecessary work (and accidental leakage) on H5/other targets.

Apply this minimal change:

-    if (sfc.template?.ast) {
+    if (isMp && sfc.template?.ast) {
       pageMetaNodes = sfc.template.ast.children.filter(
         v =>
           v.type === 1
           && (kebabCase(v.tag) === 'page-meta' || v.tag === 'page-meta'),
       ) as ElementNode[]
     }

122-125: Type-safety nit: avoid hard cast on possible undefined.

find(...) can return undefined. Keep the union type to prevent accidental early property access.

-      const uniLayoutNode = sfc.template?.ast!.children.find(
-        v => v.type === 1 && kebabCase(v.tag) === 'uni-layout',
-      ) as ElementNode
+      const uniLayoutNode = sfc.template?.ast!.children.find(
+        v => v.type === 1 && kebabCase(v.tag) === 'uni-layout',
+      ) as ElementNode | undefined
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fc11247 and 5f31bae.

📒 Files selected for processing (1)
  • src/context.ts (4 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/context.ts (1)
client.d.ts (1)
  • layouts (4-4)
🪛 ESLint
src/context.ts

[error] 261-261: This line has 2 statements. Maximum allowed is 1.

(style/max-statements-per-line)

Comment on lines +136 to +161
if (sfc.template?.loc.start.offset && sfc.template?.loc.end.offset) {
// 提取 page-meta 组件内容
const pageMetaContent = pageMetaNodes
.map(node => node.loc.source)
.join('\n')

// 从原内容中移除 page-meta 组件
let contentWithoutPageMeta = sfc.template.content
for (const node of pageMetaNodes) {
contentWithoutPageMeta = contentWithoutPageMeta.replace(
node.loc.source,
'',
)
}

// 在布局外部添加 page-meta
ms.overwrite(
sfc.template?.loc.start.offset,
sfc.template?.loc.end.offset,
`\n${pageMetaContent}<layout-${
pageLayout?.kebabName
}-uni ${pageLayoutProps.join(
' ',
)}>${contentWithoutPageMeta}</layout-${pageLayout?.kebabName}-uni>\n`,
)
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Fix offset truthiness bug and guard reinjection; handle absence of page-meta cleanly.

Using truthy checks can skip when start.offset === 0. Also, only reflow page-meta for MP; otherwise, keep existing wrapper behavior without the page-meta preface.

Apply this refactor:

-      if (sfc.template?.loc.start.offset && sfc.template?.loc.end.offset) {
-        // 提取 page-meta 组件内容
-        const pageMetaContent = pageMetaNodes
-          .map(node => node.loc.source)
-          .join('\n')
-
-        // 从原内容中移除 page-meta 组件
-        let contentWithoutPageMeta = sfc.template.content
-        for (const node of pageMetaNodes) {
-          contentWithoutPageMeta = contentWithoutPageMeta.replace(
-            node.loc.source,
-            '',
-          )
-        }
-
-        // 在布局外部添加 page-meta
-        ms.overwrite(
-          sfc.template?.loc.start.offset,
-          sfc.template?.loc.end.offset,
-          `\n${pageMetaContent}<layout-${
-            pageLayout?.kebabName
-          }-uni ${pageLayoutProps.join(
-            ' ',
-          )}>${contentWithoutPageMeta}</layout-${pageLayout?.kebabName}-uni>\n`,
-        )
-      }
+      const start = sfc.template?.loc.start.offset
+      const end = sfc.template?.loc.end.offset
+      if (start != null && end != null) {
+        const hasPageMeta = isMp && pageMetaNodes.length > 0
+        let pageMetaContent = ''
+        let contentWithoutPageMeta = sfc.template.content
+        if (hasPageMeta) {
+          pageMetaContent = pageMetaNodes.map(n => n.loc.source).join('\n')
+          for (const n of pageMetaNodes)
+            contentWithoutPageMeta = contentWithoutPageMeta.replace(n.loc.source, '')
+        }
+        const prefix = hasPageMeta ? `${pageMetaContent}\n` : ''
+        ms.overwrite(
+          start,
+          end,
+          `${prefix}<layout-${pageLayout?.kebabName}-uni ${pageLayoutProps.join(' ')}>${contentWithoutPageMeta}</layout-${pageLayout?.kebabName}-uni>`,
+        )
+      }

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In src/context.ts around lines 136 to 161, the current truthy checks on
sfc.template.loc.start.offset and end.offset skip valid zero offsets and always
reinject page-meta into the layout; change the condition to explicitly check for
null/undefined (e.g., start.offset != null && end.offset != null), only perform
the overwrite if those offsets exist, and additionally guard the page-meta
reinjection so it runs only for MP targets (check the existing isMP/pageRuntime
flag) — if pageMetaNodes is empty or target is non-MP, preserve the existing
wrapper behavior (wrap content without prepending pageMetaContent) and avoid
modifying the template. Ensure contentWithoutPageMeta is computed only when
pageMetaNodes.length > 0 and that ms.overwrite uses the correct offsets and
chosen variant (with or without pageMetaContent) accordingly.

Comment on lines +221 to +225
if (!(staticLayoutNameBind || dynamicLayoutNameBind)) {
console.warn(
'[vite-plugin-uni-layouts] Dynamic layout not found name bind',
)
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Avoid runtime crash when no name bind is present.

You warn if neither static nor dynamic name is bound, but later dereference dynamicLayoutNameBind.exp unconditionally. Bail out early by returning slotsSource.

-    if (!(staticLayoutNameBind || dynamicLayoutNameBind)) {
-      console.warn(
-        '[vite-plugin-uni-layouts] Dynamic layout not found name bind',
-      )
-    }
+    if (!(staticLayoutNameBind || dynamicLayoutNameBind)) {
+      console.warn(
+        '[vite-plugin-uni-layouts] Dynamic layout not found name bind',
+      )
+      return slotsSource
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (!(staticLayoutNameBind || dynamicLayoutNameBind)) {
console.warn(
'[vite-plugin-uni-layouts] Dynamic layout not found name bind',
)
}
if (!(staticLayoutNameBind || dynamicLayoutNameBind)) {
console.warn(
'[vite-plugin-uni-layouts] Dynamic layout not found name bind',
)
return slotsSource
}
🤖 Prompt for AI Agents
In src/context.ts around lines 221 to 225, the code warns when neither
staticLayoutNameBind nor dynamicLayoutNameBind exists but then proceeds to
dereference dynamicLayoutNameBind.exp later, risking a runtime crash; change the
flow to bail out immediately by returning slotsSource right after the warning
when both binds are absent so no subsequent code attempts to access
dynamicLayoutNameBind.exp.

Comment on lines +261 to +268
if (staticLayoutNameBind) { props.push(`is="layout-${staticLayoutNameBind.value?.content}-uni"`) }
else {
props.push(
`:is="\`layout-\${${
(dynamicLayoutNameBind.exp as SimpleExpressionNode).content
}}-uni\`"`,
)
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Fix ESLint max-statements-per-line and guard dynamic bind access.

Split the one-liner if/else and avoid undefined access. This also resolves the reported ESLint error.

As per ESLint hint.

-      if (staticLayoutNameBind) { props.push(`is="layout-${staticLayoutNameBind.value?.content}-uni"`) }
-      else {
-        props.push(
-          `:is="\`layout-\${${
-            (dynamicLayoutNameBind.exp as SimpleExpressionNode).content
-          }}-uni\`"`,
-        )
-      }
+      if (staticLayoutNameBind) {
+        props.push(`is="layout-${staticLayoutNameBind.value?.content}-uni"`)
+      }
+      else {
+        const exp = (dynamicLayoutNameBind!.exp as SimpleExpressionNode).content
+        props.push(`:is="\`layout-\${${exp}}-uni\`"`)
+      }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (staticLayoutNameBind) { props.push(`is="layout-${staticLayoutNameBind.value?.content}-uni"`) }
else {
props.push(
`:is="\`layout-\${${
(dynamicLayoutNameBind.exp as SimpleExpressionNode).content
}}-uni\`"`,
)
}
if (staticLayoutNameBind) {
props.push(`is="layout-${staticLayoutNameBind.value?.content}-uni"`)
}
else {
const exp = (dynamicLayoutNameBind!.exp as SimpleExpressionNode).content
props.push(`:is="\`layout-\${${exp}}-uni\`"`)
}
🧰 Tools
🪛 ESLint

[error] 261-261: This line has 2 statements. Maximum allowed is 1.

(style/max-statements-per-line)

🤖 Prompt for AI Agents
In src/context.ts around lines 261 to 268, the current one-line if/else both
violates ESLint max-statements-per-line and risks accessing
dynamicLayoutNameBind.exp when undefined; refactor by expanding the conditional
into multi-line branches, add a guard to ensure dynamicLayoutNameBind and
dynamicLayoutNameBind.exp exist and are of the expected SimpleExpressionNode
shape before accessing .content, extract the content into a temporary variable,
and push the static or dynamic `is="layout-...-uni"` string accordingly so there
are no multiple statements on a single line and no unsafe property access.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

支持page-meta设置

3 participants