|
| 1 | +import markdown from '@eslint/markdown'; |
| 2 | +import {createRuleTester, createFixtures, dedenter} from './test-utils.js'; |
| 3 | +import {readmeJSDocSyncRule} from './readme-jsdoc-sync.js'; |
| 4 | + |
| 5 | +const {fixturePath} = createFixtures({ |
| 6 | + 'source/some-test.d.ts': dedenter` |
| 7 | + /** |
| 8 | + Some description for \`Test\` type. |
| 9 | + Note: This is a note. |
| 10 | + @example |
| 11 | + type Test = string; |
| 12 | + */ |
| 13 | + export type Test = string; |
| 14 | + `, |
| 15 | + 'source/some-interface.d.ts': dedenter` |
| 16 | + /** |
| 17 | + Some description for \`MyInterface\` interface. |
| 18 | + This is second line. |
| 19 | + @category Test |
| 20 | + */ |
| 21 | + export interface MyInterface { |
| 22 | + prop: string; |
| 23 | + } |
| 24 | + `, |
| 25 | + 'source/multiple-exports.d.ts': dedenter` |
| 26 | + /** |
| 27 | + First line for \`Multi\`. |
| 28 | + Second line for \`Multi\`. |
| 29 | + */ |
| 30 | + export type Multi = string; |
| 31 | +
|
| 32 | + /** |
| 33 | + Description for \`Other\`. |
| 34 | + */ |
| 35 | + export type Other = number; |
| 36 | + `, |
| 37 | + 'source/noDoc.d.ts': dedenter` |
| 38 | + export type NoDoc = string; |
| 39 | + `, |
| 40 | + 'source/hyphen.d.ts': dedenter` |
| 41 | + /** |
| 42 | + Contains a - inside. |
| 43 | + */ |
| 44 | + export type Hyphen = string; |
| 45 | + `, |
| 46 | + 'source/complex-format.d.ts': dedenter` |
| 47 | + /** |
| 48 | + Description with [link to \`type-fest\`](https://github.com/sindresorhus/type-fest) and some \`code\`. And another sentence. |
| 49 | + @category Test |
| 50 | + */ |
| 51 | + export type ComplexFormat = string; |
| 52 | + `, |
| 53 | + 'source/asterisk-prefix.d.ts': dedenter` |
| 54 | + /** |
| 55 | + * Some description. |
| 56 | + */ |
| 57 | + type Test = string; |
| 58 | + export type AsteriskPrefix = string; |
| 59 | + `, |
| 60 | +}); |
| 61 | + |
| 62 | +const ruleTester = createRuleTester({ |
| 63 | + plugins: {markdown}, |
| 64 | +}); |
| 65 | + |
| 66 | +const testCase = test => ({ |
| 67 | + filename: fixturePath('readme.md'), |
| 68 | + language: 'markdown/commonmark', |
| 69 | + ...test, |
| 70 | +}); |
| 71 | + |
| 72 | +ruleTester.run('readme-jsdoc-sync', readmeJSDocSyncRule, { |
| 73 | + valid: [ |
| 74 | + // Type alias |
| 75 | + testCase({ |
| 76 | + code: '- [`Test`](source/some-test.d.ts) - Some description for `Test` type.', |
| 77 | + }), |
| 78 | + // Interface |
| 79 | + testCase({ |
| 80 | + code: '- [`MyInterface`](source/some-interface.d.ts) - Some description for `MyInterface` interface.', |
| 81 | + }), |
| 82 | + // Multiple exports |
| 83 | + testCase({ |
| 84 | + code: '- [`Multi`](source/multiple-exports.d.ts) - First line for `Multi`.', |
| 85 | + }), |
| 86 | + testCase({ |
| 87 | + code: '- [`Other`](source/multiple-exports.d.ts) - Description for `Other`.', |
| 88 | + }), |
| 89 | + // Description containing a hyphen |
| 90 | + testCase({ |
| 91 | + code: '- [`Hyphen`](source/hyphen.d.ts) - Contains a - inside.', |
| 92 | + }), |
| 93 | + // Description with links, inline code, and multiple sentences |
| 94 | + testCase({ |
| 95 | + code: '- [`ComplexFormat`](source/complex-format.d.ts) - Description with [link to `type-fest`](https://github.com/sindresorhus/type-fest) and some `code`. And another sentence.', |
| 96 | + }), |
| 97 | + // JSDoc with asterisk prefix style |
| 98 | + testCase({ |
| 99 | + code: '- [`AsteriskPrefix`](source/asterisk-prefix.d.ts) - Some description.', |
| 100 | + }), |
| 101 | + // Normal list item without a link |
| 102 | + testCase({ |
| 103 | + code: '- Some normal list item.', |
| 104 | + }), |
| 105 | + // Non `.d.ts` link |
| 106 | + testCase({ |
| 107 | + code: '- [`Partial<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype) - Make all properties in `T` optional.', |
| 108 | + }), |
| 109 | + // Link is not the first element |
| 110 | + testCase({ |
| 111 | + code: '- `Prettify`- See [`Simplify`](source/simplify.d.ts)', |
| 112 | + }), |
| 113 | + // Multiple list items |
| 114 | + testCase({ |
| 115 | + code: dedenter` |
| 116 | + Some introduction paragraph. |
| 117 | +
|
| 118 | + ## Types |
| 119 | +
|
| 120 | + ### Some group |
| 121 | + - [\`Test\`](source/some-test.d.ts) - Some description for \`Test\` type. |
| 122 | + - [\`MyInterface\`](source/some-interface.d.ts) - Some description for \`MyInterface\` interface. |
| 123 | +
|
| 124 | + ### Another group |
| 125 | + - [\`Multi\`](source/multiple-exports.d.ts) - First line for \`Multi\`. |
| 126 | + - [\`Other\`](source/multiple-exports.d.ts) - Description for \`Other\`. |
| 127 | + - [\`Hyphen\`](source/hyphen.d.ts) - Contains a - inside. |
| 128 | +
|
| 129 | +
|
| 130 | + ## Alternatives |
| 131 | + - \`Prettify\`- See [\`Simplify\`](source/simplify.d.ts) |
| 132 | + `, |
| 133 | + }), |
| 134 | + ], |
| 135 | + invalid: [ |
| 136 | + testCase({ |
| 137 | + // Mismatch between README description and source JSDoc |
| 138 | + code: dedenter` |
| 139 | + - [\`Test\`](source/some-test.d.ts) - Some description for Test type. |
| 140 | + - [\`ComplexFormat\`](source/complex-format.d.ts) - Wrong description. |
| 141 | + `, |
| 142 | + errors: [{messageId: 'mismatch'}, {messageId: 'mismatch'}], |
| 143 | + output: dedenter` |
| 144 | + - [\`Test\`](source/some-test.d.ts) - Some description for \`Test\` type. |
| 145 | + - [\`ComplexFormat\`](source/complex-format.d.ts) - Description with [link to \`type-fest\`](https://github.com/sindresorhus/type-fest) and some \`code\`. And another sentence. |
| 146 | + `, |
| 147 | + }), |
| 148 | + // Linked `.d.ts` file does not exist |
| 149 | + testCase({ |
| 150 | + code: '- [`Missing`](source/does-not-exist.d.ts) - Some description.', |
| 151 | + errors: [{messageId: 'fileNotFound'}], |
| 152 | + }), |
| 153 | + ], |
| 154 | +}); |
0 commit comments