|
| 1 | +import { RuleTester } from 'eslint'; |
| 2 | + |
| 3 | +import php from '../../index'; |
| 4 | +import { requireTypeAnnotations } from '../require-type-annotations'; |
| 5 | + |
| 6 | +const ruleTester = new RuleTester({ |
| 7 | + plugins: { php }, |
| 8 | + language: 'php/php', |
| 9 | +}); |
| 10 | + |
| 11 | +ruleTester.run('require-type-annotations', requireTypeAnnotations, { |
| 12 | + valid: [ |
| 13 | + '<?php class Foo { public int $a = 1; }', |
| 14 | + '<?php class Foo { public function bar(): int {} }', |
| 15 | + '<?php class Foo { public function __construct() {} }', |
| 16 | + '<?php class Foo { public function bar( int $param ): int {} }', |
| 17 | + '<?php function foo(): int {}', |
| 18 | + '<?php function foo( int $param ): int {}', |
| 19 | + '<?php array_map(function(): int {}, []);', |
| 20 | + '<?php array_map(function( int $param ): int {}, []);', |
| 21 | + '<?php array_map(fn(): int => 1, []);', |
| 22 | + '<?php array_map(fn( int $param ): int => 1, []);', |
| 23 | + ], |
| 24 | + invalid: [ |
| 25 | + { |
| 26 | + code: '<?php class Foo { public $bar; }', |
| 27 | + errors: [ |
| 28 | + { |
| 29 | + messageId: 'requireTypesForProperty', |
| 30 | + data: { name: 'bar' }, |
| 31 | + line: 1, |
| 32 | + column: 26, |
| 33 | + endLine: 1, |
| 34 | + endColumn: 30, |
| 35 | + }, |
| 36 | + ], |
| 37 | + }, |
| 38 | + { |
| 39 | + code: '<?php class Foo { public function bar() {} }', |
| 40 | + errors: [ |
| 41 | + { |
| 42 | + messageId: 'requireTypesForMethodReturnType', |
| 43 | + data: { name: 'bar' }, |
| 44 | + line: 1, |
| 45 | + column: 35, |
| 46 | + endLine: 1, |
| 47 | + endColumn: 38, |
| 48 | + }, |
| 49 | + ], |
| 50 | + }, |
| 51 | + { |
| 52 | + code: '<?php class Foo { public function bar( $param ): int {} }', |
| 53 | + errors: [ |
| 54 | + { |
| 55 | + messageId: 'requireTypesForParameter', |
| 56 | + data: { name: 'param' }, |
| 57 | + line: 1, |
| 58 | + column: 40, |
| 59 | + endLine: 1, |
| 60 | + endColumn: 46, |
| 61 | + }, |
| 62 | + ], |
| 63 | + }, |
| 64 | + { |
| 65 | + code: '<?php function foo() {}', |
| 66 | + errors: [ |
| 67 | + { |
| 68 | + messageId: 'requireTypesForFunctionReturnType', |
| 69 | + data: { name: 'foo' }, |
| 70 | + line: 1, |
| 71 | + column: 16, |
| 72 | + endLine: 1, |
| 73 | + endColumn: 19, |
| 74 | + }, |
| 75 | + ], |
| 76 | + }, |
| 77 | + { |
| 78 | + code: '<?php function foo( $param ): int {}', |
| 79 | + errors: [ |
| 80 | + { |
| 81 | + messageId: 'requireTypesForParameter', |
| 82 | + data: { name: 'param' }, |
| 83 | + line: 1, |
| 84 | + column: 21, |
| 85 | + endLine: 1, |
| 86 | + endColumn: 27, |
| 87 | + }, |
| 88 | + ], |
| 89 | + }, |
| 90 | + { |
| 91 | + code: '<?php array_map(function() {}, []);', |
| 92 | + errors: [ |
| 93 | + { |
| 94 | + messageId: 'requireTypesForClosureReturnType', |
| 95 | + line: 1, |
| 96 | + column: 17, |
| 97 | + endLine: 1, |
| 98 | + endColumn: 25, |
| 99 | + }, |
| 100 | + ], |
| 101 | + }, |
| 102 | + { |
| 103 | + code: '<?php array_map(function( $param ): int {}, []);', |
| 104 | + errors: [ |
| 105 | + { |
| 106 | + messageId: 'requireTypesForParameter', |
| 107 | + line: 1, |
| 108 | + column: 27, |
| 109 | + endLine: 1, |
| 110 | + endColumn: 33, |
| 111 | + }, |
| 112 | + ], |
| 113 | + }, |
| 114 | + { |
| 115 | + code: '<?php array_map(fn() => 1, []);', |
| 116 | + errors: [ |
| 117 | + { |
| 118 | + messageId: 'requireTypesForClosureReturnType', |
| 119 | + line: 1, |
| 120 | + column: 17, |
| 121 | + endLine: 1, |
| 122 | + endColumn: 19, |
| 123 | + }, |
| 124 | + ], |
| 125 | + }, |
| 126 | + { |
| 127 | + code: '<?php array_map(fn( $param ): int => 1, []);', |
| 128 | + errors: [ |
| 129 | + { |
| 130 | + messageId: 'requireTypesForParameter', |
| 131 | + line: 1, |
| 132 | + column: 21, |
| 133 | + endLine: 1, |
| 134 | + endColumn: 27, |
| 135 | + }, |
| 136 | + ], |
| 137 | + }, |
| 138 | + ], |
| 139 | +}); |
0 commit comments