Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,40 @@ describe('enhanceCodeEmphasis', () => {
parseSource = await createParseSource();
});

it('removes whole JSX focus directive lines', async () => {
const result = await testEmphasis(
`const node = <div>
{/* @focus-start */}
<span>Hello</span>
{/* @focus-end */}
</div>;`,
parseSource,
);

expect(result).toMatchInlineSnapshot(`
"<span class="frame" data-lined=""><span class="line" data-ln="1"><span class="pl-k">const</span> <span class="pl-c1">node</span> <span class="pl-k di-pu">=</span> &#x3C;<span class="pl-ent">div</span>></span>
</span><span class="frame" data-lined="" data-frame-type="focus" data-frame-focus-target=""><span class="line" data-ln="2"> &#x3C;<span class="pl-ent">span</span>>Hello&#x3C;/<span class="pl-ent">span</span>></span>
</span><span class="frame" data-lined=""><span class="line" data-ln="3">&#x3C;/<span class="pl-ent">div</span>>;</span></span>"
`);
});

it('removes whole JSX JSDoc focus directive lines', async () => {
const result = await testEmphasis(
`const node = <div>
{/** @focus-start */}
<span>Hello</span>
{/** @focus-end */}
</div>;`,
parseSource,
);

expect(result).toMatchInlineSnapshot(`
"<span class="frame" data-lined=""><span class="line" data-ln="1"><span class="pl-k">const</span> <span class="pl-c1">node</span> <span class="pl-k di-pu">=</span> &#x3C;<span class="pl-ent">div</span>></span>
</span><span class="frame" data-lined="" data-frame-type="focus" data-frame-focus-target=""><span class="line" data-ln="2"> &#x3C;<span class="pl-ent">span</span>>Hello&#x3C;/<span class="pl-ent">span</span>></span>
</span><span class="frame" data-lined=""><span class="line" data-ln="3">&#x3C;/<span class="pl-ent">div</span>>;</span></span>"
`);
});

describe('EMPHASIS_COMMENT_PREFIX', () => {
it('should export the correct prefix', () => {
expect(EMPHASIS_COMMENT_PREFIX).toBe('@highlight');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ describe('lintJavascriptDemoFocus', () => {
it('should pass RuleTester', () => {
ruleTester.run('require-demo-focus', lintJavascriptDemoFocus, {
valid: [
{
code: `export default function Demo() {
return <div>{/** @focus */}<span>Hello</span></div>;
}`,
},
{
code: `export default function Demo() {
/**
* @focus-start
*/
const value = 'Hello';
return <div>{value}</div>;
/**
* @focus-end
*/
}`,
},
// File already has @highlight-start with @focus — skip
{
code: `
Expand Down
Original file line number Diff line number Diff line change
@@ -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');
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <div>
{/** @focus */}
<span />
</div>;
import Value from './Value';`;
const result = parseImportsAndComments(code, '/src/demo.tsx', options);
expect(result.code).toBe(`const node = <div>
<span />
</div>;
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';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as path from 'path-module';
import { normalizeCommentContent } from './normalizeCommentContent';
import { fileUrlToPortablePath, portablePathToFileUrl } from './fileUrlToPortablePath';

/**
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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())
Expand Down Expand Up @@ -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
Expand Down
Loading