|
1 | 1 | import outdent from 'outdent'; |
| 2 | +import {typescriptEslintParser} from '../scripts/parsers.js'; |
2 | 3 | import notFunctionTypes from './utils/not-function-types.js'; |
3 | 4 | import {getTester} from './utils/test.js'; |
4 | 5 |
|
@@ -53,6 +54,26 @@ const invalidTestCase = (({code, options, method, name, suggestions}) => ({ |
53 | 54 | ], |
54 | 55 | })); |
55 | 56 |
|
| 57 | +const typeAware = testCase => ({ |
| 58 | + ...(typeof testCase === 'string' ? {code: testCase} : testCase), |
| 59 | + filename: 'file.ts', |
| 60 | + languageOptions: { |
| 61 | + parser: typescriptEslintParser, |
| 62 | + parserOptions: {projectService: {allowDefaultProject: ['*.ts']}}, |
| 63 | + }, |
| 64 | +}); |
| 65 | + |
| 66 | +const invalidTypeAwareMapCallbackTestCase = code => typeAware(invalidTestCase({ |
| 67 | + code, |
| 68 | + method: 'map', |
| 69 | + name: 'callback', |
| 70 | + suggestions: [ |
| 71 | + code.replace('map(callback)', 'map((element) => callback(element))'), |
| 72 | + code.replace('map(callback)', 'map((element, index) => callback(element, index))'), |
| 73 | + code.replace('map(callback)', 'map((element, index, array) => callback(element, index, array))'), |
| 74 | + ], |
| 75 | +})); |
| 76 | + |
56 | 77 | test({ |
57 | 78 | valid: [ |
58 | 79 | ...simpleMethods.map(method => `foo.${method}(element => fn(element))`), |
@@ -756,6 +777,86 @@ test.snapshot({ |
756 | 777 | ], |
757 | 778 | }); |
758 | 779 |
|
| 780 | +test({ |
| 781 | + valid: [ |
| 782 | + typeAware(outdent` |
| 783 | + interface SearchService { |
| 784 | + find(callback: Function): unknown; |
| 785 | + } |
| 786 | + declare const callback: Function; |
| 787 | + declare const service: SearchService; |
| 788 | + service.find(callback); |
| 789 | + `), |
| 790 | + typeAware(outdent` |
| 791 | + declare const callback: Function; |
| 792 | + class Collection { |
| 793 | + map(callback: Function) {} |
| 794 | + } |
| 795 | + const collection = new Collection(); |
| 796 | + collection.map(callback); |
| 797 | + `), |
| 798 | + typeAware(outdent` |
| 799 | + interface Model { |
| 800 | + find(query: object): unknown; |
| 801 | + } |
| 802 | + declare const AccountModel: Model; |
| 803 | + const query = {}; |
| 804 | + AccountModel.find(query); |
| 805 | + `), |
| 806 | + typeAware(outdent` |
| 807 | + interface NgMocks { |
| 808 | + find(component: unknown): unknown; |
| 809 | + } |
| 810 | + declare const ngMocks: NgMocks; |
| 811 | + declare const MyComponent: unknown; |
| 812 | + ngMocks.find(MyComponent); |
| 813 | + `), |
| 814 | + typeAware(outdent` |
| 815 | + declare const callback: Function; |
| 816 | + declare const collection: string[] | {map(callback: Function): unknown}; |
| 817 | + collection.map(callback); |
| 818 | + `), |
| 819 | + typeAware(outdent` |
| 820 | + export {}; |
| 821 | + type Array<T> = {map(callback: Function): unknown}; |
| 822 | + declare const callback: Function; |
| 823 | + declare const collection: Array<string>; |
| 824 | + collection.map(callback); |
| 825 | + `), |
| 826 | + typeAware(outdent` |
| 827 | + export {}; |
| 828 | + class Uint8Array { |
| 829 | + map(callback: Function) {} |
| 830 | + } |
| 831 | + declare const callback: Function; |
| 832 | + const collection = new Uint8Array(); |
| 833 | + collection.map(callback); |
| 834 | + `), |
| 835 | + ], |
| 836 | + invalid: [ |
| 837 | + ...[ |
| 838 | + 'declare const callback: Function; declare const array: string[]; array.map(callback);', |
| 839 | + 'declare const callback: Function; declare const array: readonly string[]; array.map(callback);', |
| 840 | + 'declare const callback: Function; declare const array: [string, string]; array.map(callback);', |
| 841 | + 'declare const callback: Function; declare const array: Array<string>; array.map(callback);', |
| 842 | + 'declare const callback: Function; declare const array: ReadonlyArray<string>; array.map(callback);', |
| 843 | + 'declare const callback: Function; declare const array: Uint8Array; array.map(callback);', |
| 844 | + 'declare const callback: Function; declare const array: string[] | readonly number[]; array.map(callback);', |
| 845 | + 'declare const callback: Function; declare const array: string[] | Uint8Array; array.map(callback);', |
| 846 | + 'declare const callback: Function; declare const array: string[] & {foo: string}; array.map(callback);', |
| 847 | + 'declare const callback: Function; declare const array: string[] | undefined; array?.map(callback);', |
| 848 | + 'declare const callback: Function; function run<T extends string[]>(array: T) { array.map(callback); }', |
| 849 | + 'declare const callback: Function; function run<T extends readonly string[]>(array: T) { array.map(callback); }', |
| 850 | + 'declare const callback: Function; class Strings extends Array<string> {} const array = new Strings(); array.map(callback);', |
| 851 | + 'declare const callback: Function; interface S extends ReadonlyArray<string> {} declare const array: S; array.map(callback);', |
| 852 | + 'declare const callback: Function; function run<T extends Uint8Array>(array: T) { array.map(callback); }', |
| 853 | + 'declare const callback: Function; declare const array: any; array.map(callback);', |
| 854 | + 'declare const callback: Function; declare const array: unknown; array.map(callback);', |
| 855 | + 'declare const callback: Function; declare const array: MissingType; array.map(callback);', |
| 856 | + ].map(code => invalidTypeAwareMapCallbackTestCase(code)), |
| 857 | + ], |
| 858 | +}); |
| 859 | + |
759 | 860 | test.typescript({ |
760 | 861 | valid: [ |
761 | 862 | outdent` |
|
0 commit comments