|
| 1 | +/** |
| 2 | + * Tests for JSDoc comment sanitization in babel-ast and hooks-ast |
| 3 | + */ |
| 4 | +import * as t from '@babel/types'; |
| 5 | + |
| 6 | +import { |
| 7 | + addJSDocComment, |
| 8 | + generateCode, |
| 9 | +} from '../../core/codegen/babel-ast'; |
| 10 | +import { addJSDocComment as addJSDocCommentHooks } from '../../core/codegen/hooks-ast'; |
| 11 | + |
| 12 | +describe('addJSDocComment', () => { |
| 13 | + describe('babel-ast', () => { |
| 14 | + it('produces valid JSDoc for simple descriptions', () => { |
| 15 | + const node = t.identifier('x'); |
| 16 | + addJSDocComment(node, ['A simple description']); |
| 17 | + const code = generateCode([ |
| 18 | + t.variableDeclaration('const', [ |
| 19 | + t.variableDeclarator(node, t.numericLiteral(1)), |
| 20 | + ]), |
| 21 | + ]); |
| 22 | + expect(code).toContain('/** A simple description */'); |
| 23 | + expect(code).not.toContain('*/\n'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('sanitizes */ in single-line descriptions', () => { |
| 27 | + const node = t.identifier('x'); |
| 28 | + addJSDocComment(node, [ |
| 29 | + 'Reads and enables pagination through a set of */ values.', |
| 30 | + ]); |
| 31 | + const code = generateCode([ |
| 32 | + t.variableDeclaration('const', [ |
| 33 | + t.variableDeclarator(node, t.numericLiteral(1)), |
| 34 | + ]), |
| 35 | + ]); |
| 36 | + expect(code).toContain('*\\/'); |
| 37 | + expect(code).not.toMatch(/\/\*.*\*\/.*\*\//); |
| 38 | + }); |
| 39 | + |
| 40 | + it('sanitizes */ in multi-line descriptions', () => { |
| 41 | + const node = t.identifier('x'); |
| 42 | + addJSDocComment(node, [ |
| 43 | + 'First line with */ embedded', |
| 44 | + 'Second line is fine', |
| 45 | + 'Third line also has */ inside', |
| 46 | + ]); |
| 47 | + const code = generateCode([ |
| 48 | + t.variableDeclaration('const', [ |
| 49 | + t.variableDeclarator(node, t.numericLiteral(1)), |
| 50 | + ]), |
| 51 | + ]); |
| 52 | + const commentMatch = code.match(/\/\*[\s\S]*?\*\//); |
| 53 | + expect(commentMatch).not.toBeNull(); |
| 54 | + const comment = commentMatch![0]; |
| 55 | + const innerSlashes = comment.slice(2, -2); |
| 56 | + expect(innerSlashes).not.toContain('*/'); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('hooks-ast', () => { |
| 61 | + it('produces valid JSDoc for simple descriptions', () => { |
| 62 | + const node = t.identifier('y'); |
| 63 | + addJSDocCommentHooks(node, ['A simple description']); |
| 64 | + expect(node.leadingComments).toHaveLength(1); |
| 65 | + expect(node.leadingComments![0].value).toBe('* A simple description '); |
| 66 | + }); |
| 67 | + |
| 68 | + it('sanitizes */ in single-line descriptions', () => { |
| 69 | + const node = t.identifier('y'); |
| 70 | + addJSDocCommentHooks(node, [ |
| 71 | + 'Reads and enables pagination through a set of */ values.', |
| 72 | + ]); |
| 73 | + expect(node.leadingComments![0].value).not.toContain('*/'); |
| 74 | + expect(node.leadingComments![0].value).toContain('*\\/'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('sanitizes */ in multi-line descriptions', () => { |
| 78 | + const node = t.identifier('y'); |
| 79 | + addJSDocCommentHooks(node, [ |
| 80 | + 'Line with */ problem', |
| 81 | + 'Normal line', |
| 82 | + ]); |
| 83 | + const commentValue = node.leadingComments![0].value; |
| 84 | + expect(commentValue).not.toContain('*/'); |
| 85 | + expect(commentValue).toContain('*\\/'); |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments