Skip to content

chore(chat-widget): enforce prettier via pre-commit and npm scripts#3234

Merged
snopoke merged 3 commits intomainfrom
sk/chat-widget-prettier
Apr 28, 2026
Merged

chore(chat-widget): enforce prettier via pre-commit and npm scripts#3234
snopoke merged 3 commits intomainfrom
sk/chat-widget-prettier

Conversation

@snopoke
Copy link
Copy Markdown
Contributor

@snopoke snopoke commented Apr 28, 2026

Product Description

No user-facing change — internal tooling for the chat widget.

Technical Description

Contributors' IDEs were silently auto-formatting chat-widget files on save against the existing components/chat_widget/.prettierrc.json, producing large unrelated diffs in otherwise small PRs (e.g. #3196). Prettier was the de-facto formatter but had no enforcement: it wasn't a declared dependency, no npm script ran it, and no pre-commit hook checked it.

Two commits, kept deliberately separate:

  1. chore(chat-widget): add prettier tooling and pre-commit hook

    • Adds prettier@^3.8.3 as a chat-widget devDependency.
    • Adds format (prettier --write) and format:check (prettier --check) npm scripts scoped to src/**/*.{ts,tsx,css}.
    • Adds components/chat_widget/.prettierignore excluding Stencil-generated files (src/components.d.ts, src/components/**/readme.md) and build outputs (dist/, www/, loader/).
    • Adds an rbubley/mirrors-prettier@v3.8.3 hook to .pre-commit-config.yaml scoped to ^components/chat_widget/src/.*\.(ts|tsx|css)$.
    • Renames the deprecated jsxBracketSameLine key in .prettierrc.json to bracketSameLine (prettier 3.x).
  2. style(chat-widget): baseline-format all src files with prettier
    Pure npm run format output across 12 pre-existing files. Isolated from commit 1 so future PRs touching these files don't mix prettier reformatting noise with real changes. No behavior changes; reproducible via npm run format.

The chat widget's prettier config (printWidth: 180, singleQuote: true, bracketSpacing: true, trailingComma: 'all', etc.) is unchanged — only enforcement was added.

Migrations

  • The migrations are backwards compatible — N/A, no migrations.

Demo

N/A — tooling only. Verify locally with:
```
cd components/chat_widget
npm install
npm run format:check # should pass
```

Docs and Changelog

  • This PR requires docs/changelog update — no user-facing change.

🤖 Generated with Claude Code

snopoke and others added 2 commits April 28, 2026 08:48
Adds an explicit prettier dependency, npm scripts (`format`, `format:check`),
a `.prettierignore` for Stencil-generated files, and a repo-wide pre-commit
hook scoped to the chat widget. Also renames the deprecated
`jsxBracketSameLine` key in `.prettierrc.json` to `bracketSameLine`.

Without enforcement, contributors' IDEs were silently auto-formatting on save
against the existing `.prettierrc.json`, producing large unrelated diffs in
otherwise small PRs (e.g. #3196). This makes formatting deterministic across
contributors and CI.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Pure formatting commit, no behavior changes. Run `npm run format` to
reproduce. Isolated from the prior tooling commit so future PRs touching
these files don't mix prettier reformatting with real changes.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
codescene-delta-analysis[bot]

This comment was marked as outdated.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 28, 2026

📝 Walkthrough

Walkthrough

This pull request introduces Prettier code formatting infrastructure to the components/chat_widget package and applies consistent formatting across the codebase. Changes include adding a .pre-commit-config.yaml hook, creating .prettierignore and .prettierrc.json configuration files, and updating package.json with Prettier as a dev dependency and new format/format:check npm scripts. The formatting standardization across TypeScript/TSX/CSS source files includes quote style normalization (double to single quotes), trailing comma adjustments, semicolon additions, multi-line to single-line expression restructuring, and arrow function parameter syntax simplification. No exported API signatures are meaningfully altered, and the changes are primarily cosmetic except for minor control flow normalization in varToPixels.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: adding Prettier enforcement via pre-commit hooks and npm scripts to the chat-widget component.
Description check ✅ Passed The description is comprehensive and follows the template structure with all key sections completed: Product Description, Technical Description, Migrations, Demo, and Docs/Changelog.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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
Copy Markdown
Contributor

@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 (2)
.pre-commit-config.yaml (1)

49-54: Consider adding a CI format:check gate in addition to pre-commit.

Pre-commit is helpful but bypassable; a CI check for components/chat_widget keeps formatting enforcement consistent on every PR.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In @.pre-commit-config.yaml around lines 49 - 54, Add a CI job named
"format:check" that runs the same Prettier check as the pre-commit hook to
enforce formatting on every PR; implement it to run the equivalent command used
by the "prettier" hook (or an npm script like format:check) targeting the same
files pattern "components/chat_widget/src/.*\.(ts|tsx|css)$" so the workflow
fails when formatting is incorrect and cannot be bypassed by skipping
pre-commit.
components/chat_widget/package.json (1)

56-56: Pin Prettier to exact version to match pre-commit configuration.

package.json uses ^3.8.3, while pre-commit is pinned to v3.8.3. Using a caret allows npm to install any version >= 3.8.3 but < 4.0.0, which could lead to inconsistent formatting output between npm scripts and pre-commit hooks.

♻️ Suggested change
-    "prettier": "^3.8.3",
+    "prettier": "3.8.3",
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@components/chat_widget/package.json` at line 56, The Prettier dependency in
package.json is declared as "prettier": "^3.8.3", which allows non-exact
upgrades and can cause formatting mismatches with the pre-commit hook pinned to
v3.8.3; update the dependency to an exact version by replacing the caret range
with "prettier": "3.8.3" so npm installs the exact same Prettier used by
pre-commit and ensure consistency between npm scripts and hooks.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@components/chat_widget/src/components/ocs-chat/ocs-chat.spec.tsx`:
- Line 17: Install and declare the missing testing/type packages and fix the
runtime TypeScript error: add devDependencies for `@stencil/core`,
`@stencil/core/testing` and appropriate `@types` (e.g., `@types/jest` or `@types/mocha`)
or add module declarations for them so the test environment type-checks; in
ocs-chat.tsx replace the non‑standard Property 'at' usage on ChatMessage[]
(search for usages of ChatMessage[] .at(...), likely in function/variable
handling message retrieval) with a compatible alternative such as indexing with
length-1 (messages[messages.length - 1]) or messages.slice(-1)[0]; update any
tests in ocs-chat.spec.tsx to use Stencil's testing API imports from
`@stencil/core/testing` and ensure the test harness is available in package.json
scripts so stencil tests run in CI.

In `@components/chat_widget/src/components/ocs-chat/ocs-chat.tsx`:
- Around line 1799-1801: The anchor in the OcsChat component (the <a> with
href="https://www.dimagi.com" and target="_blank") is missing the rel attribute;
update that anchor in ocs-chat.tsx (within the OcsChat component/render
function) to include rel="noopener noreferrer" to prevent reverse-tabnabbing
when opening the external link in a new tab.
- Around line 2-18: Update the build_components.yml CI workflow to enforce
linting and typechecking for the chat_widget component by adding two steps after
the existing Test step: a "Lint" step that runs npm run lint with
working-directory set to ./components/chat_widget, and a "Type Check" step that
runs npm run type-check with working-directory set to ./components/chat_widget;
ensure the step names are exactly "Lint" and "Type Check" so they are visible in
job output and place them in the same job that runs the current Test step.

---

Nitpick comments:
In @.pre-commit-config.yaml:
- Around line 49-54: Add a CI job named "format:check" that runs the same
Prettier check as the pre-commit hook to enforce formatting on every PR;
implement it to run the equivalent command used by the "prettier" hook (or an
npm script like format:check) targeting the same files pattern
"components/chat_widget/src/.*\.(ts|tsx|css)$" so the workflow fails when
formatting is incorrect and cannot be bypassed by skipping pre-commit.

In `@components/chat_widget/package.json`:
- Line 56: The Prettier dependency in package.json is declared as "prettier":
"^3.8.3", which allows non-exact upgrades and can cause formatting mismatches
with the pre-commit hook pinned to v3.8.3; update the dependency to an exact
version by replacing the caret range with "prettier": "3.8.3" so npm installs
the exact same Prettier used by pre-commit and ensure consistency between npm
scripts and hooks.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 19c44160-db2f-4258-8681-52a77aea1bca

📥 Commits

Reviewing files that changed from the base of the PR and between 643f77e and 89572f6.

⛔ Files ignored due to path filters (1)
  • components/chat_widget/package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (16)
  • .pre-commit-config.yaml
  • components/chat_widget/.prettierignore
  • components/chat_widget/.prettierrc.json
  • components/chat_widget/package.json
  • components/chat_widget/src/components/ocs-chat/icons.tsx
  • components/chat_widget/src/components/ocs-chat/ocs-chat.css
  • components/chat_widget/src/components/ocs-chat/ocs-chat.spec.tsx
  • components/chat_widget/src/components/ocs-chat/ocs-chat.tsx
  • components/chat_widget/src/components/ocs-chat/ocs-chat_session_handling.spec.tsx
  • components/chat_widget/src/services/chat-session-service.ts
  • components/chat_widget/src/services/file-attachment-manager.ts
  • components/chat_widget/src/utils/cookies.ts
  • components/chat_widget/src/utils/markdown.ts
  • components/chat_widget/src/utils/translations.spec.ts
  • components/chat_widget/src/utils/translations.ts
  • components/chat_widget/src/utils/utils.ts

// Mock translation manager to return welcome messages
component.translationManager = new TranslationManager('en', {
'content.welcomeMessages': ['Hello from translations!', 'Welcome to our chat.']
'content.welcomeMessages': ['Hello from translations!', 'Welcome to our chat.'],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

cd components/chat_widget
npm run lint
npm run test
npm run type-check

Repository: dimagi/open-chat-studio

Length of output: 37413


Address type-checking and testing failures in component widget before submitting.

The lint/test/type-check commands were executed but failed with exit code 2. Specifically:

  • Linting completed
  • Testing failed: stencil tool is not available in the environment
  • Type-checking failed with multiple TypeScript errors, including missing module declarations (@stencil/core, @stencil/core/testing, @types/jest/@types/mocha) and an actual code issue (Property 'at' does not exist on type 'ChatMessage[]' at line 793 in ocs-chat.tsx)

Resolve the missing dependencies and type errors in components/chat_widget to satisfy validation requirements before this PR is ready.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@components/chat_widget/src/components/ocs-chat/ocs-chat.spec.tsx` at line 17,
Install and declare the missing testing/type packages and fix the runtime
TypeScript error: add devDependencies for `@stencil/core`, `@stencil/core/testing`
and appropriate `@types` (e.g., `@types/jest` or `@types/mocha`) or add module
declarations for them so the test environment type-checks; in ocs-chat.tsx
replace the non‑standard Property 'at' usage on ChatMessage[] (search for usages
of ChatMessage[] .at(...), likely in function/variable handling message
retrieval) with a compatible alternative such as indexing with length-1
(messages[messages.length - 1]) or messages.slice(-1)[0]; update any tests in
ocs-chat.spec.tsx to use Stencil's testing API imports from
`@stencil/core/testing` and ensure the test harness is available in package.json
scripts so stencil tests run in CI.

Comment on lines +2 to +18
import { Component, Host, h, Prop, State, Element, Watch, Env } from '@stencil/core';
import {
XMarkIcon,
GripDotsVerticalIcon, PlusWithCircleIcon, ArrowsPointingOutIcon, ArrowsPointingInIcon,
PaperClipIcon, CheckDocumentIcon, XIcon, OcsWidgetAvatar
GripDotsVerticalIcon,
PlusWithCircleIcon,
ArrowsPointingOutIcon,
ArrowsPointingInIcon,
PaperClipIcon,
CheckDocumentIcon,
XIcon,
OcsWidgetAvatar,
} from './icons';
import { renderMarkdownSync as renderMarkdownComplete } from '../../utils/markdown';
import { varToPixels } from '../../utils/utils';
import {TranslationStrings, TranslationManager, defaultTranslations} from '../../utils/translations';
import {
ChatSessionService,
ChatMessage,
MessagePollingHandle,
TaskPollingHandle
} from '../../services/chat-session-service';
import {
FileAttachmentManager,
SelectedFile
} from '../../services/file-attachment-manager';
import { TranslationStrings, TranslationManager, defaultTranslations } from '../../utils/translations';
import { ChatSessionService, ChatMessage, MessagePollingHandle, TaskPollingHandle } from '../../services/chat-session-service';
import { FileAttachmentManager, SelectedFile } from '../../services/file-attachment-manager';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

# First, let's find the package.json and understand available npm scripts
find . -name "package.json" -type f | head -20

Repository: dimagi/open-chat-studio

Length of output: 1132


🏁 Script executed:

# Look for CI configuration files
find . -name ".github" -o -name ".gitlab-ci.yml" -o -name "tox.ini" -o -name ".travis.yml" | head -10

Repository: dimagi/open-chat-studio

Length of output: 498


🏁 Script executed:

# Check if there's a tsconfig.json in the components/chat_widget directory
find ./components/chat_widget -name "tsconfig.json" -o -name "jest.config.*" -o -name ".eslintrc*" | head -20

Repository: dimagi/open-chat-studio

Length of output: 106


🏁 Script executed:

# Check root package.json for npm scripts
cat package.json | grep -A 50 '"scripts"'

Repository: dimagi/open-chat-studio

Length of output: 1788


🏁 Script executed:

# Check components/chat_widget package.json
cat ./components/chat_widget/package.json | grep -A 30 '"scripts"'

Repository: dimagi/open-chat-studio

Length of output: 1235


🏁 Script executed:

# List GitHub Actions workflow files
ls -la ./.github/workflows/

Repository: dimagi/open-chat-studio

Length of output: 1364


🏁 Script executed:

# Check the lint_and_test.yml workflow
cat ./.github/workflows/lint_and_test.yml

Repository: dimagi/open-chat-studio

Length of output: 5495


🏁 Script executed:

# Check the build_components.yml workflow
cat ./.github/workflows/build_components.yml

Repository: dimagi/open-chat-studio

Length of output: 1650


Add missing lint and typecheck to CI for components/chat_widget.

The build_components.yml workflow currently runs tests (npm run test, npm run test:security) but is missing lint and typecheck steps. Per coding guidelines, all three must be enforced in CI.

Update build_components.yml

Add these steps after the Test step:

      - name: Lint
        run: npm run lint
        working-directory: ./components/chat_widget
      - name: Type Check
        run: npm run type-check
        working-directory: ./components/chat_widget
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@components/chat_widget/src/components/ocs-chat/ocs-chat.tsx` around lines 2 -
18, Update the build_components.yml CI workflow to enforce linting and
typechecking for the chat_widget component by adding two steps after the
existing Test step: a "Lint" step that runs npm run lint with working-directory
set to ./components/chat_widget, and a "Type Check" step that runs npm run
type-check with working-directory set to ./components/chat_widget; ensure the
step names are exactly "Lint" and "Type Check" so they are visible in job output
and place them in the same job that runs the current Test step.

Comment thread components/chat_widget/src/components/ocs-chat/ocs-chat.tsx Outdated
codescene-delta-analysis[bot]

This comment was marked as outdated.

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@snopoke snopoke merged commit 8d3efdf into main Apr 28, 2026
17 checks passed
@snopoke snopoke deleted the sk/chat-widget-prettier branch April 28, 2026 07:21
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.

1 participant