|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import test from 'node:test'; |
| 3 | +import { parseSync } from 'oxc-parser'; |
| 4 | +import { buildJSDocTagLookup } from '../../src/typescript/visitors/jsdoc.ts'; |
| 5 | + |
| 6 | +const cases: [string, string, string[][]][] = [ |
| 7 | + ['JSDoc tags', '/** @public */\nexport const value = 1;', [['@public']]], |
| 8 | + ['regular block tags', '/* @public */\nexport const value = 1;', [['@public']]], |
| 9 | + ['line tags', '// @public\nexport const value = 1;', [['@public']]], |
| 10 | + ['indented line tags', '\t // @lintignore\nexport const value = 1;', [['@lintignore']]], |
| 11 | + ['stacked line tags', '// @internal\n// @public\nexport const value = 1;', [['@internal', '@public']]], |
| 12 | + ['line tags after JSDoc', '/** @internal */\n// @public\nexport const value = 1;', [['@internal', '@public']]], |
| 13 | + ['line tags before JSDoc', '// @public\n/** Description */\nexport const value = 1;', [['@public']]], |
| 14 | + ['intervening line comment', '// @public\n// Explanation\nexport const value = 1;', [['@public']]], |
| 15 | + ['CRLF line comments', '// @public\r\n// Explanation\r\nexport const value = 1;', [['@public']]], |
| 16 | + ['CR line comments', '// @public\r// Explanation\rexport const value = 1;', [['@public']]], |
| 17 | + ['blank line', '// @public\n\nexport const value = 1;', [['@public']]], |
| 18 | + ['trailing line tags', 'export const first = 1; // @public\nexport const second = 2;', [[], []]], |
| 19 | + ['intervening code', '// @public\nconst local = 1;\nexport const value = local;', [[]]], |
| 20 | + ['next declaration', '// @public\nexport const first = 1;\nexport const second = 2;', [['@public'], []]], |
| 21 | + ['string contents', 'export const value = "// @public";', [[]]], |
| 22 | + ['untagged comment', '// Description\nexport const value = 1;', [[]]], |
| 23 | +]; |
| 24 | + |
| 25 | +for (const [name, source, expected] of cases) { |
| 26 | + test(`Read comment tags: ${name}`, () => { |
| 27 | + const result = parseSync('module.ts', source); |
| 28 | + assert.deepEqual(result.errors, []); |
| 29 | + const getTags = buildJSDocTagLookup(result.comments, source); |
| 30 | + const actual = result.program.body |
| 31 | + .filter(node => node.type === 'ExportNamedDeclaration') |
| 32 | + .map(node => getTags(node.start)); |
| 33 | + assert.deepEqual( |
| 34 | + actual, |
| 35 | + expected.map(tags => new Set(tags)) |
| 36 | + ); |
| 37 | + }); |
| 38 | +} |
0 commit comments