|
| 1 | +import { ExtendedCodeAction } from '../getCodeActions' |
| 2 | + |
| 3 | +const errorCodes = [ |
| 4 | + // ts.Diagnostics.Property_0_does_not_exist_on_type_1.code, |
| 5 | + // ts.Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_2.code, |
| 6 | + // ts.Diagnostics.Property_0_is_missing_in_type_1_but_required_in_type_2.code, |
| 7 | + // ts.Diagnostics.Type_0_is_missing_the_following_properties_from_type_1_Colon_2.code, |
| 8 | + // ts.Diagnostics.Type_0_is_missing_the_following_properties_from_type_1_Colon_2_and_3_more.code, |
| 9 | + // // ts.Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1.code, |
| 10 | + // // ts.Diagnostics.Cannot_find_name_0.code, |
| 11 | + 2339, 2551, 2741, 2739, 2740 /* 2345, 2304, */, |
| 12 | +] |
| 13 | + |
| 14 | +export default { |
| 15 | + codes: errorCodes, |
| 16 | + kind: 'quickfix', |
| 17 | + title: 'Declare missing attributes', |
| 18 | + tryToApply({ sourceFile, node, c, languageService, position, formatOptions, range }) { |
| 19 | + // todo maybe cache from prev request? |
| 20 | + if (!node) return |
| 21 | + const codeFixes = languageService.getCodeFixesAtPosition( |
| 22 | + sourceFile.fileName, |
| 23 | + node.getStart(), |
| 24 | + range?.end ?? node.getStart(), |
| 25 | + errorCodes, |
| 26 | + formatOptions ?? {}, |
| 27 | + {}, |
| 28 | + ) |
| 29 | + const fix = codeFixes.find(codeFix => codeFix.fixName === 'fixMissingAttributes') |
| 30 | + if (fix && fix.changes[0]?.textChanges.length === 1) { |
| 31 | + const changes = fix.changes[0]!.textChanges |
| 32 | + let i = 1 |
| 33 | + return { |
| 34 | + snippetEdits: [ |
| 35 | + { |
| 36 | + newText: changes[0]!.newText.replaceAll('$', '\\$').replaceAll('={undefined}', () => `={$${i++}}`), |
| 37 | + span: fix.changes[0]!.textChanges[0]!.span, |
| 38 | + }, |
| 39 | + ], |
| 40 | + } |
| 41 | + } |
| 42 | + return |
| 43 | + }, |
| 44 | +} as ExtendedCodeAction |
0 commit comments