From 8e8594f6da1364e8982fa5e3917e1e9b09091113 Mon Sep 17 00:00:00 2001 From: Brijesh Bittu <717550+brijeshb42@users.noreply.github.com> Date: Tue, 13 Oct 2026 15:00:36 +0530 Subject: [PATCH 1/2] Handle JSDoc demo directives --- .../emphasisCommentUtils.ts | 4 +- .../enhanceCodeEmphasis.test.ts | 13 ++++ .../lintJavascriptDemoFocus.test.ts | 17 ++++++ .../loaderUtils/normalizeCommentContent.ts | 17 ++++++ .../parseImportsAndComments.test.ts | 59 +++++++++++++++++++ .../loaderUtils/parseImportsAndComments.ts | 30 ++-------- 6 files changed, 114 insertions(+), 26 deletions(-) create mode 100644 packages/docs-infra/src/pipeline/loaderUtils/normalizeCommentContent.ts diff --git a/packages/docs-infra/src/pipeline/enhanceCodeEmphasis/emphasisCommentUtils.ts b/packages/docs-infra/src/pipeline/enhanceCodeEmphasis/emphasisCommentUtils.ts index 4e7e3f1126..5833f86ef7 100644 --- a/packages/docs-infra/src/pipeline/enhanceCodeEmphasis/emphasisCommentUtils.ts +++ b/packages/docs-infra/src/pipeline/enhanceCodeEmphasis/emphasisCommentUtils.ts @@ -1,3 +1,5 @@ +import { normalizeCommentContent } from '../loaderUtils/normalizeCommentContent'; + /** * The prefix used to identify emphasis comments in source code. * Comments starting with this prefix will be processed for emphasis. @@ -133,7 +135,7 @@ export function getUnquotedCommentTokens(comment: string): string[] { * Returns whether a parsed comment contains a real focus directive. */ export function hasFocusDirective(comment: string): boolean { - const tokens = getUnquotedCommentTokens(comment); + const tokens = getUnquotedCommentTokens(normalizeCommentContent(comment)); const firstToken = tokens[0]; if ( diff --git a/packages/docs-infra/src/pipeline/enhanceCodeEmphasis/enhanceCodeEmphasis.test.ts b/packages/docs-infra/src/pipeline/enhanceCodeEmphasis/enhanceCodeEmphasis.test.ts index a9d641e3f9..c7aef398f4 100644 --- a/packages/docs-infra/src/pipeline/enhanceCodeEmphasis/enhanceCodeEmphasis.test.ts +++ b/packages/docs-infra/src/pipeline/enhanceCodeEmphasis/enhanceCodeEmphasis.test.ts @@ -78,6 +78,19 @@ describe('enhanceCodeEmphasis', () => { parseSource = await createParseSource(); }); + it('renders JSDoc focus directives like plain comments without exposing metadata', async () => { + const plain = `const node =
+ {/* @focus-start */} + Hello + {/* @focus-end */} +
;`; + const jsdoc = plain.replaceAll('{/* @focus', '{/** @focus'); + const expected = await testEmphasis(plain, parseSource); + const actual = await testEmphasis(jsdoc, parseSource); + expect(actual).toBe(expected); + expect(actual).not.toContain('@focus'); + }); + describe('EMPHASIS_COMMENT_PREFIX', () => { it('should export the correct prefix', () => { expect(EMPHASIS_COMMENT_PREFIX).toBe('@highlight'); diff --git a/packages/docs-infra/src/pipeline/lintJavascriptDemoFocus/lintJavascriptDemoFocus.test.ts b/packages/docs-infra/src/pipeline/lintJavascriptDemoFocus/lintJavascriptDemoFocus.test.ts index 4ff753f627..3ac0214ada 100644 --- a/packages/docs-infra/src/pipeline/lintJavascriptDemoFocus/lintJavascriptDemoFocus.test.ts +++ b/packages/docs-infra/src/pipeline/lintJavascriptDemoFocus/lintJavascriptDemoFocus.test.ts @@ -17,6 +17,23 @@ describe('lintJavascriptDemoFocus', () => { it('should pass RuleTester', () => { ruleTester.run('require-demo-focus', lintJavascriptDemoFocus, { valid: [ + { + code: `export default function Demo() { + return
{/** @focus */}Hello
; +}`, + }, + { + code: `export default function Demo() { + /** + * @focus-start + */ + const value = 'Hello'; + return
{value}
; + /** + * @focus-end + */ +}`, + }, // File already has @highlight-start with @focus — skip { code: ` diff --git a/packages/docs-infra/src/pipeline/loaderUtils/normalizeCommentContent.ts b/packages/docs-infra/src/pipeline/loaderUtils/normalizeCommentContent.ts new file mode 100644 index 0000000000..136c800392 --- /dev/null +++ b/packages/docs-infra/src/pipeline/loaderUtils/normalizeCommentContent.ts @@ -0,0 +1,17 @@ +/** + * Removes JSDoc decoration from parsed block comment content. Comment delimiters + * have already been removed by the source parser or ESLint. + */ +export function normalizeCommentContent(content: string): string { + if (!content.trimStart().startsWith('*')) { + return content; + } + + return content + .split('\n') + .map((line) => { + const trimmed = line.trimStart(); + return trimmed.startsWith('*') ? trimmed.slice(1).trimStart() : line; + }) + .join('\n'); +} diff --git a/packages/docs-infra/src/pipeline/loaderUtils/parseImportsAndComments.test.ts b/packages/docs-infra/src/pipeline/loaderUtils/parseImportsAndComments.test.ts index 71409e87cb..96b44024c4 100644 --- a/packages/docs-infra/src/pipeline/loaderUtils/parseImportsAndComments.test.ts +++ b/packages/docs-infra/src/pipeline/loaderUtils/parseImportsAndComments.test.ts @@ -2,6 +2,65 @@ import { describe, expect, it } from 'vitest'; import { parseImportsAndComments } from './parseImportsAndComments'; describe('parseImportsAndComments', () => { + describe('JSDoc directives', () => { + const options = { + removeCommentsWithPrefix: ['@focus', '@highlight'], + notableCommentsPrefix: ['@focus', '@highlight'], + }; + + it('removes single-line JSX JSDoc directives and preserves import offsets', () => { + const code = `const node =
+ {/** @focus */} + +
; +import Value from './Value';`; + const result = parseImportsAndComments(code, '/src/demo.tsx', options); + expect(result.code).toBe(`const node =
+ +
; +import Value from './Value';`); + expect(result.comments).toEqual({ 2: ['@focus'] }); + const [position] = result.relative['./Value'].positions; + expect(result.code?.slice(position.start, position.end)).toBe("'./Value'"); + }); + + it('normalizes multiline decoration and keeps quoted directive descriptions intact', () => { + const code = `/** + * @focus-start @padding 2 + */ +const value = 1; +/** + * @focus-end + */ +/** @highlight "* @focus is quoted" */ +const next = 2;`; + const result = parseImportsAndComments(code, '/src/demo.ts', options); + expect(result.code).toBe('const value = 1;\nconst next = 2;'); + expect(result.comments).toEqual({ + 1: ['@focus-start @padding 2'], + 2: ['@focus-end', '@highlight "* @focus is quoted"'], + }); + }); + + it('preserves ordinary JSDoc, prose mentions, and directive strings', () => { + const code = `/** + * Describes @focus rather than declaring a directive. + * @param value - The input. + */ +const value = "{/** @focus */}"; +/** "@focus" is quoted. */ +const next = '@highlight';`; + const result = parseImportsAndComments(code, '/src/demo.ts', options); + expect(result).toEqual({ relative: {}, externals: {} }); + const collected = parseImportsAndComments(code, '/src/demo.ts', { + notableCommentsPrefix: ['Describes'], + }); + expect(collected.comments).toEqual({ + 1: ['Describes @focus rather than declaring a directive.', '@param value - The input.'], + }); + }); + }); + it('should resolve relative import paths and group by import path', async () => { const code = ` import Component1 from './Component1'; diff --git a/packages/docs-infra/src/pipeline/loaderUtils/parseImportsAndComments.ts b/packages/docs-infra/src/pipeline/loaderUtils/parseImportsAndComments.ts index ee06219410..07efa0a2d9 100644 --- a/packages/docs-infra/src/pipeline/loaderUtils/parseImportsAndComments.ts +++ b/packages/docs-infra/src/pipeline/loaderUtils/parseImportsAndComments.ts @@ -1,4 +1,5 @@ import * as path from 'path-module'; +import { normalizeCommentContent } from './normalizeCommentContent'; import { fileUrlToPortablePath, portablePathToFileUrl } from './fileUrlToPortablePath'; /** @@ -118,19 +119,8 @@ function isStringStart(ch: string, withinMdx?: boolean): boolean { * @returns True if the comment starts with any of the specified prefixes */ function matchesCommentPrefix(commentText: string, removeCommentsWithPrefix: string[]): boolean { - return removeCommentsWithPrefix.some((prefix) => { - // For single-line comments, check after the // - if (commentText.startsWith('//')) { - const content = commentText.slice(2).trim(); - return content.startsWith(prefix); - } - // For multi-line comments, check after the /* - if (commentText.startsWith('/*')) { - const content = commentText.slice(2, -2).trim(); - return content.startsWith(prefix); - } - return false; - }); + const content = stripCommentMarkers(commentText).join('\n'); + return removeCommentsWithPrefix.some((prefix) => content.startsWith(prefix)); } /** @@ -145,7 +135,7 @@ function stripCommentMarkers(commentText: string): string[] { } // For multi-line comments, remove /* and */, split by lines, and trim each line if (commentText.startsWith('/*') && commentText.endsWith('*/')) { - const content = commentText.slice(2, -2); + const content = normalizeCommentContent(commentText.slice(2, -2)); return content .split('\n') .map((line) => line.trim()) @@ -217,17 +207,7 @@ function scanForImports( if (!notableCommentsPrefix || notableCommentsPrefix.length === 0) { return false; // If no notable prefix specified, don't match any comments as notable } - return notableCommentsPrefix.some((prefix) => { - if (commentText.startsWith('//')) { - const content = commentText.slice(2).trim(); - return content.startsWith(prefix); - } - if (commentText.startsWith('/*')) { - const content = commentText.slice(2, -2).trim(); - return content.startsWith(prefix); - } - return false; - }); + return matchesCommentPrefix(commentText, notableCommentsPrefix); }; let i = 0; let outputLine = 0; // Line number in output code after comment removal From 8a372354e63c9890e3b8559e8469a3a0c82fd4e0 Mon Sep 17 00:00:00 2001 From: Brijesh Bittu <717550+brijeshb42@users.noreply.github.com> Date: Thu, 15 Oct 2026 11:15:14 +0530 Subject: [PATCH 2/2] Snapshot whole directive line removal --- .../enhanceCodeEmphasis.test.ts | 37 +++++++++++++++---- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/packages/docs-infra/src/pipeline/enhanceCodeEmphasis/enhanceCodeEmphasis.test.ts b/packages/docs-infra/src/pipeline/enhanceCodeEmphasis/enhanceCodeEmphasis.test.ts index c7aef398f4..95c3fed217 100644 --- a/packages/docs-infra/src/pipeline/enhanceCodeEmphasis/enhanceCodeEmphasis.test.ts +++ b/packages/docs-infra/src/pipeline/enhanceCodeEmphasis/enhanceCodeEmphasis.test.ts @@ -78,17 +78,38 @@ describe('enhanceCodeEmphasis', () => { parseSource = await createParseSource(); }); - it('renders JSDoc focus directives like plain comments without exposing metadata', async () => { - const plain = `const node =
+ it('removes whole JSX focus directive lines', async () => { + const result = await testEmphasis( + `const node =
{/* @focus-start */} Hello {/* @focus-end */} -
;`; - const jsdoc = plain.replaceAll('{/* @focus', '{/** @focus'); - const expected = await testEmphasis(plain, parseSource); - const actual = await testEmphasis(jsdoc, parseSource); - expect(actual).toBe(expected); - expect(actual).not.toContain('@focus'); +
;`, + parseSource, + ); + + expect(result).toMatchInlineSnapshot(` + "const node = <div> + <span>Hello</span> + </div>;" + `); + }); + + it('removes whole JSX JSDoc focus directive lines', async () => { + const result = await testEmphasis( + `const node =
+ {/** @focus-start */} + Hello + {/** @focus-end */} +
;`, + parseSource, + ); + + expect(result).toMatchInlineSnapshot(` + "const node = <div> + <span>Hello</span> + </div>;" + `); }); describe('EMPHASIS_COMMENT_PREFIX', () => {