|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { requirePropertyTsDoc } from '../require-property-tsdoc/index.js'; |
| 3 | +import type { Diagnostic, Rule } from '@equinor/fusion-framework-lint-core'; |
| 4 | + |
| 5 | +function lint( |
| 6 | + source: string, |
| 7 | + file = 'fixture.ts', |
| 8 | + rule: Rule = requirePropertyTsDoc(), |
| 9 | +): Diagnostic[] { |
| 10 | + // mirror the engine: skip `check` entirely when `match` opts the file out |
| 11 | + if (rule.match && !rule.match(file)) return []; |
| 12 | + return rule.check(source, { filePath: file }); |
| 13 | +} |
| 14 | + |
| 15 | +describe('require-property-tsdoc — passing', () => { |
| 16 | + it('passes: documented public field', () => { |
| 17 | + const source = ` |
| 18 | +class MyButton { |
| 19 | + /** The visual color variant to render. */ |
| 20 | + color = 'primary'; |
| 21 | +} |
| 22 | +`; |
| 23 | + expect(lint(source)).toHaveLength(0); |
| 24 | + }); |
| 25 | + |
| 26 | + it('passes: documented decorated field', () => { |
| 27 | + const source = ` |
| 28 | +class MyButton { |
| 29 | + /** The visual color variant to render. */ |
| 30 | + @property({ type: String }) |
| 31 | + color = 'primary'; |
| 32 | +} |
| 33 | +`; |
| 34 | + expect(lint(source)).toHaveLength(0); |
| 35 | + }); |
| 36 | + |
| 37 | + it('passes: private modifier field without TSDoc', () => { |
| 38 | + const source = ` |
| 39 | +class MyButton { |
| 40 | + private internalState = 1; |
| 41 | +} |
| 42 | +`; |
| 43 | + expect(lint(source)).toHaveLength(0); |
| 44 | + }); |
| 45 | + |
| 46 | + it('passes: #private field without TSDoc', () => { |
| 47 | + const source = ` |
| 48 | +class MyButton { |
| 49 | + #internalState = 1; |
| 50 | +} |
| 51 | +`; |
| 52 | + expect(lint(source)).toHaveLength(0); |
| 53 | + }); |
| 54 | + |
| 55 | + it('passes: static field without TSDoc', () => { |
| 56 | + const source = ` |
| 57 | +class MyButton { |
| 58 | + static styles = []; |
| 59 | +} |
| 60 | +`; |
| 61 | + expect(lint(source)).toHaveLength(0); |
| 62 | + }); |
| 63 | + |
| 64 | + it('passes: field in non-exported class when classScope is "exported"', () => { |
| 65 | + const rule = requirePropertyTsDoc({ classScope: 'exported' }); |
| 66 | + const source = ` |
| 67 | +class MyButton { |
| 68 | + color = 'primary'; |
| 69 | +} |
| 70 | +`; |
| 71 | + expect(lint(source, 'fixture.ts', rule)).toHaveLength(0); |
| 72 | + }); |
| 73 | + |
| 74 | + it('passes: documented decorated getter accessor', () => { |
| 75 | + const source = ` |
| 76 | +class MyButton { |
| 77 | + /** |
| 78 | + * @deprecated use \`variant="contained"\` instead. |
| 79 | + * @returns true when \`variant\` is \`contained\` |
| 80 | + */ |
| 81 | + @property({ type: Boolean, reflect: true }) |
| 82 | + get raised(): boolean { |
| 83 | + return this.variant === 'contained'; |
| 84 | + } |
| 85 | +} |
| 86 | +`; |
| 87 | + expect(lint(source)).toHaveLength(0); |
| 88 | + }); |
| 89 | + |
| 90 | + it('passes: undecorated getter accessor without TSDoc', () => { |
| 91 | + const source = ` |
| 92 | +class MyButton { |
| 93 | + get raised(): boolean { |
| 94 | + return this.variant === 'contained'; |
| 95 | + } |
| 96 | +} |
| 97 | +`; |
| 98 | + expect(lint(source)).toHaveLength(0); |
| 99 | + }); |
| 100 | +}); |
| 101 | + |
| 102 | +describe('require-property-tsdoc — failing', () => { |
| 103 | + it('fails: undocumented public field', () => { |
| 104 | + const source = ` |
| 105 | +class MyButton { |
| 106 | + color = 'primary'; |
| 107 | +} |
| 108 | +`; |
| 109 | + const diagnostics = lint(source); |
| 110 | + expect(diagnostics).toHaveLength(1); |
| 111 | + expect(diagnostics[0].message).toContain("'color'"); |
| 112 | + }); |
| 113 | + |
| 114 | + it('fails: undocumented decorated field', () => { |
| 115 | + const source = ` |
| 116 | +class MyButton { |
| 117 | + @property({ type: String }) |
| 118 | + color = 'primary'; |
| 119 | +} |
| 120 | +`; |
| 121 | + const diagnostics = lint(source); |
| 122 | + expect(diagnostics).toHaveLength(1); |
| 123 | + expect(diagnostics[0].message).toContain("'color'"); |
| 124 | + }); |
| 125 | + |
| 126 | + it('fails: undocumented protected field', () => { |
| 127 | + const source = ` |
| 128 | +class MyButton { |
| 129 | + protected color = 'primary'; |
| 130 | +} |
| 131 | +`; |
| 132 | + expect(lint(source)).toHaveLength(1); |
| 133 | + }); |
| 134 | + |
| 135 | + it('fails: undocumented field in exported class when classScope is "exported"', () => { |
| 136 | + const rule = requirePropertyTsDoc({ classScope: 'exported' }); |
| 137 | + const source = ` |
| 138 | +export class MyButton { |
| 139 | + color = 'primary'; |
| 140 | +} |
| 141 | +`; |
| 142 | + expect(lint(source, 'fixture.ts', rule)).toHaveLength(1); |
| 143 | + }); |
| 144 | + |
| 145 | + it('fails: non-TSDoc comment does not satisfy the rule', () => { |
| 146 | + const source = ` |
| 147 | +class MyButton { |
| 148 | + // just a regular comment |
| 149 | + color = 'primary'; |
| 150 | +} |
| 151 | +`; |
| 152 | + expect(lint(source)).toHaveLength(1); |
| 153 | + }); |
| 154 | + |
| 155 | + it('fails: undocumented decorated getter accessor', () => { |
| 156 | + const source = ` |
| 157 | +class MyButton { |
| 158 | + @property({ type: Boolean, reflect: true }) |
| 159 | + get raised(): boolean { |
| 160 | + return this.variant === 'contained'; |
| 161 | + } |
| 162 | +} |
| 163 | +`; |
| 164 | + const diagnostics = lint(source); |
| 165 | + expect(diagnostics).toHaveLength(1); |
| 166 | + expect(diagnostics[0].message).toContain("'raised'"); |
| 167 | + }); |
| 168 | +}); |
0 commit comments