|
| 1 | +package fourslash_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "strconv" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/microsoft/TypeScript/tsc/internal/diagnostics" |
| 8 | + "github.com/microsoft/TypeScript/tsc/internal/fourslash" |
| 9 | + "github.com/microsoft/TypeScript/tsc/internal/locale" |
| 10 | + "github.com/microsoft/TypeScript/tsc/internal/testutil" |
| 11 | +) |
| 12 | + |
| 13 | +func TestCodeFixAddMissingAttributes11(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + for i, test := range []struct { |
| 16 | + content string |
| 17 | + result string |
| 18 | + fileResult string |
| 19 | + filename string |
| 20 | + }{ |
| 21 | + { |
| 22 | + content: `// @jsx: preserve |
| 23 | +// @Filename: a.tsx |
| 24 | +interface T { a: number; b?: string } |
| 25 | +const A = (a: T) => null; |
| 26 | +const a = [|<A />|];`, |
| 27 | + result: `<A a={0} />`, |
| 28 | + }, |
| 29 | + { |
| 30 | + content: `// @jsx: preserve |
| 31 | +// @Filename: /a.ts |
| 32 | +export enum E { a, b } |
| 33 | +export class C {} |
| 34 | +// @Filename: /b.tsx |
| 35 | +import { E as B, C as D } from "./a"; |
| 36 | +const A = (a: { a: B; b: D }) => null; |
| 37 | +const a = [|<A />|];`, |
| 38 | + result: `<A a={B.a} b={new D} />`, |
| 39 | + filename: "/b.tsx", |
| 40 | + }, |
| 41 | + { |
| 42 | + content: `// @jsx: preserve |
| 43 | +// @Filename: a.tsx |
| 44 | +declare namespace JSX { |
| 45 | + interface Element {} |
| 46 | + interface ElementChildrenAttribute { children: {} } |
| 47 | +} |
| 48 | +const A = (a: { children: string; b: number }) => null; |
| 49 | +const a = [|<A>text</A>|];`, |
| 50 | + result: `<A b={0}>text</A>`, |
| 51 | + }, |
| 52 | + { |
| 53 | + content: `// @jsx: preserve |
| 54 | +// @Filename: a.tsx |
| 55 | +declare namespace JSX { |
| 56 | + interface Element {} |
| 57 | + interface ElementChildrenAttribute { c: {} } |
| 58 | +} |
| 59 | +const A = (a: { c: string; b: number }) => null; |
| 60 | +const a = [|<A>{"text"}</A>|];`, |
| 61 | + result: `<A b={0}>{"text"}</A>`, |
| 62 | + }, |
| 63 | + { |
| 64 | + content: `// @jsx: preserve |
| 65 | +// @Filename: a.tsx |
| 66 | +declare namespace JSX { |
| 67 | + interface Element {} |
| 68 | + interface ElementChildrenAttribute { children: {} } |
| 69 | +} |
| 70 | +const A = (a: { children: string; b: number }) => null; |
| 71 | +const a = [|<A>{}</A>|];`, |
| 72 | + result: `<A children={""} b={0}>{}</A>`, |
| 73 | + }, |
| 74 | + { |
| 75 | + content: `// @jsx: preserve |
| 76 | +// @Filename: a.tsx |
| 77 | +const A = (a: { a: number; b: string }) => null; |
| 78 | +declare const b: { a?: number }; |
| 79 | +const a = [|<A {...b} />|];`, |
| 80 | + result: `<A a={0} b={""} {...b} />`, |
| 81 | + }, |
| 82 | + { |
| 83 | + content: `// @jsx: preserve |
| 84 | +// @Filename: a.tsx |
| 85 | +const A = (a: { a: number; b: string }) => null; |
| 86 | +declare const b: { a: number } | {}; |
| 87 | +const a = [|<A {...b} />|];`, |
| 88 | + result: `<A a={0} b={""} {...b} />`, |
| 89 | + }, |
| 90 | + { |
| 91 | + content: `// @jsx: preserve |
| 92 | +// @Filename: a.tsx |
| 93 | +const A = (a: { a: number; b: string }) => null; |
| 94 | +declare const b: { a?: number }; |
| 95 | +const a = [|<A {...b} a={1} />|];`, |
| 96 | + result: `<A b={""} {...b} a={1} />`, |
| 97 | + }, |
| 98 | + { |
| 99 | + content: `// @jsx: preserve |
| 100 | +// @Filename: a.tsx |
| 101 | +const A = (a: { a: number }) => null; |
| 102 | +declare const b: { a?: number }; |
| 103 | +const a = [|<A {...b} />|];`, |
| 104 | + result: `<A a={0} {...b} />`, |
| 105 | + }, |
| 106 | + { |
| 107 | + content: `// @jsx: preserve |
| 108 | +// @Filename: a.tsx |
| 109 | +const A = (a: { a: number; b: string }) => null; |
| 110 | +declare const b: { a?: number }; |
| 111 | +declare const c: { a: number }; |
| 112 | +const a = [|<A {...c} {...b} />|];`, |
| 113 | + result: `<A b={""} {...c} {...b} />`, |
| 114 | + }, |
| 115 | + { |
| 116 | + content: `// @jsx: preserve |
| 117 | +// @Filename: /a.ts |
| 118 | +export enum E { a, b } |
| 119 | +export class C {} |
| 120 | +// @Filename: /b.tsx |
| 121 | +import type { C as D, E as B } from "./a"; |
| 122 | +const A = (a: { a: B; b: D }) => null; |
| 123 | +const a = [|<A />|];`, |
| 124 | + fileResult: `import { C as D, E as B } from "./a"; |
| 125 | +const A = (a: { a: B; b: D }) => null; |
| 126 | +const a = <A a={B.a} b={new D} />;`, |
| 127 | + filename: "/b.tsx", |
| 128 | + }, |
| 129 | + } { |
| 130 | + t.Run(strconv.Itoa(i+1), func(t *testing.T) { |
| 131 | + t.Parallel() |
| 132 | + defer testutil.RecoverAndFail(t, "Panic on fourslash test") |
| 133 | + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, test.content) |
| 134 | + defer done() |
| 135 | + if test.filename != "" { |
| 136 | + f.GoToFile(t, test.filename) |
| 137 | + } |
| 138 | + f.VerifyCodeFix(t, fourslash.VerifyCodeFixOptions{ |
| 139 | + Description: diagnostics.Add_missing_attributes.Localize(locale.Default), |
| 140 | + NewFileContent: test.fileResult, |
| 141 | + NewRangeContent: test.result, |
| 142 | + ApplyChanges: true, |
| 143 | + }) |
| 144 | + f.VerifyNoErrors(t) |
| 145 | + }) |
| 146 | + } |
| 147 | +} |
0 commit comments