|
| 1 | +// @ts-ignore TS6133 |
| 2 | +import { expect } from "https://deno.land/x/[email protected]/mod.ts"; |
| 3 | +const test = Deno.test; |
| 4 | +// import path from "path"; |
| 5 | +// import { Node, Project, SyntaxKind } from "ts-morph"; |
| 6 | + |
| 7 | +// import { filePath } from "./language-server.source"; |
| 8 | + |
| 9 | +// The following tool is helpful for understanding the TypeScript AST associated with these tests: |
| 10 | +// https://ts-ast-viewer.com/ (just copy the contents of languageServerFeatures.source into the viewer) |
| 11 | + |
| 12 | +test("", () => {}); |
| 13 | +// describe("Executing Go To Definition (and therefore Find Usages and Rename Refactoring) using an IDE works on inferred object properties", () => { |
| 14 | +// // Compile file developmentEnvironment.source |
| 15 | +// const project = new Project({ |
| 16 | +// tsConfigFilePath: path.join(__dirname, "..", "..", "tsconfig.json"), |
| 17 | +// skipAddingFilesFromTsConfig: true, |
| 18 | +// }); |
| 19 | +// const sourceFile = project.addSourceFileAtPath(filePath); |
| 20 | + |
| 21 | +// test("works for object properties inferred from z.object()", () => { |
| 22 | +// // Find usage of Test.f1 property |
| 23 | +// const instanceVariable = |
| 24 | +// sourceFile.getVariableDeclarationOrThrow("instanceOfTest"); |
| 25 | +// const propertyBeingAssigned = getPropertyBeingAssigned( |
| 26 | +// instanceVariable, |
| 27 | +// "f1" |
| 28 | +// ); |
| 29 | + |
| 30 | +// // Find definition of Test.f1 property |
| 31 | +// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0]; |
| 32 | +// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind( |
| 33 | +// SyntaxKind.VariableDeclaration |
| 34 | +// ); |
| 35 | + |
| 36 | +// // Assert that find definition returned the Zod definition of Test |
| 37 | +// expect(definitionOfProperty?.getText()).toEqual("f1: z.number()"); |
| 38 | +// expect(parentOfProperty?.getName()).toEqual("Test"); |
| 39 | +// }); |
| 40 | + |
| 41 | +// // test("works for first object properties inferred from z.object().merge()", () => { |
| 42 | +// // // Find usage of TestMerge.f1 property |
| 43 | +// // const instanceVariable = sourceFile.getVariableDeclarationOrThrow( |
| 44 | +// // "instanceOfTestMerge" |
| 45 | +// // ); |
| 46 | +// // const propertyBeingAssigned = getPropertyBeingAssigned( |
| 47 | +// // instanceVariable, |
| 48 | +// // "f1" |
| 49 | +// // ); |
| 50 | + |
| 51 | +// // // Find definition of TestMerge.f1 property |
| 52 | +// // const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0]; |
| 53 | +// // const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind( |
| 54 | +// // SyntaxKind.VariableDeclaration |
| 55 | +// // ); |
| 56 | + |
| 57 | +// // // Assert that find definition returned the Zod definition of Test |
| 58 | +// // expect(definitionOfProperty?.getText()).toEqual("f1: z.number()"); |
| 59 | +// // expect(parentOfProperty?.getName()).toEqual("Test"); |
| 60 | +// // }); |
| 61 | + |
| 62 | +// // test("works for second object properties inferred from z.object().merge()", () => { |
| 63 | +// // // Find usage of TestMerge.f2 property |
| 64 | +// // const instanceVariable = sourceFile.getVariableDeclarationOrThrow( |
| 65 | +// // "instanceOfTestMerge" |
| 66 | +// // ); |
| 67 | +// // const propertyBeingAssigned = getPropertyBeingAssigned( |
| 68 | +// // instanceVariable, |
| 69 | +// // "f2" |
| 70 | +// // ); |
| 71 | + |
| 72 | +// // // Find definition of TestMerge.f2 property |
| 73 | +// // const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0]; |
| 74 | +// // const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind( |
| 75 | +// // SyntaxKind.VariableDeclaration |
| 76 | +// // ); |
| 77 | + |
| 78 | +// // // Assert that find definition returned the Zod definition of TestMerge |
| 79 | +// // expect(definitionOfProperty?.getText()).toEqual( |
| 80 | +// // "f2: z.string().optional()" |
| 81 | +// // ); |
| 82 | +// // expect(parentOfProperty?.getName()).toEqual("TestMerge"); |
| 83 | +// // }); |
| 84 | + |
| 85 | +// test("works for first object properties inferred from z.union()", () => { |
| 86 | +// // Find usage of TestUnion.f1 property |
| 87 | +// const instanceVariable = sourceFile.getVariableDeclarationOrThrow( |
| 88 | +// "instanceOfTestUnion" |
| 89 | +// ); |
| 90 | +// const propertyBeingAssigned = getPropertyBeingAssigned( |
| 91 | +// instanceVariable, |
| 92 | +// "f1" |
| 93 | +// ); |
| 94 | + |
| 95 | +// // Find definition of TestUnion.f1 property |
| 96 | +// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0]; |
| 97 | +// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind( |
| 98 | +// SyntaxKind.VariableDeclaration |
| 99 | +// ); |
| 100 | + |
| 101 | +// // Assert that find definition returned the Zod definition of Test |
| 102 | +// expect(definitionOfProperty?.getText()).toEqual("f1: z.number()"); |
| 103 | +// expect(parentOfProperty?.getName()).toEqual("Test"); |
| 104 | +// }); |
| 105 | + |
| 106 | +// test("works for second object properties inferred from z.union()", () => { |
| 107 | +// // Find usage of TestUnion.f2 property |
| 108 | +// const instanceVariable = sourceFile.getVariableDeclarationOrThrow( |
| 109 | +// "instanceOfTestUnion" |
| 110 | +// ); |
| 111 | +// const propertyBeingAssigned = getPropertyBeingAssigned( |
| 112 | +// instanceVariable, |
| 113 | +// "f2" |
| 114 | +// ); |
| 115 | + |
| 116 | +// // Find definition of TestUnion.f2 property |
| 117 | +// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0]; |
| 118 | +// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind( |
| 119 | +// SyntaxKind.VariableDeclaration |
| 120 | +// ); |
| 121 | + |
| 122 | +// // Assert that find definition returned the Zod definition of TestUnion |
| 123 | +// expect(definitionOfProperty?.getText()).toEqual( |
| 124 | +// "f2: z.string().optional()" |
| 125 | +// ); |
| 126 | +// expect(parentOfProperty?.getName()).toEqual("TestUnion"); |
| 127 | +// }); |
| 128 | + |
| 129 | +// test("works for object properties inferred from z.object().partial()", () => { |
| 130 | +// // Find usage of TestPartial.f1 property |
| 131 | +// const instanceVariable = sourceFile.getVariableDeclarationOrThrow( |
| 132 | +// "instanceOfTestPartial" |
| 133 | +// ); |
| 134 | +// const propertyBeingAssigned = getPropertyBeingAssigned( |
| 135 | +// instanceVariable, |
| 136 | +// "f1" |
| 137 | +// ); |
| 138 | + |
| 139 | +// // Find definition of TestPartial.f1 property |
| 140 | +// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0]; |
| 141 | +// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind( |
| 142 | +// SyntaxKind.VariableDeclaration |
| 143 | +// ); |
| 144 | + |
| 145 | +// // Assert that find definition returned the Zod definition of Test |
| 146 | +// expect(definitionOfProperty?.getText()).toEqual("f1: z.number()"); |
| 147 | +// expect(parentOfProperty?.getName()).toEqual("Test"); |
| 148 | +// }); |
| 149 | + |
| 150 | +// test("works for object properties inferred from z.object().pick()", () => { |
| 151 | +// // Find usage of TestPick.f1 property |
| 152 | +// const instanceVariable = |
| 153 | +// sourceFile.getVariableDeclarationOrThrow("instanceOfTestPick"); |
| 154 | +// const propertyBeingAssigned = getPropertyBeingAssigned( |
| 155 | +// instanceVariable, |
| 156 | +// "f1" |
| 157 | +// ); |
| 158 | + |
| 159 | +// // Find definition of TestPick.f1 property |
| 160 | +// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0]; |
| 161 | +// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind( |
| 162 | +// SyntaxKind.VariableDeclaration |
| 163 | +// ); |
| 164 | + |
| 165 | +// // Assert that find definition returned the Zod definition of Test |
| 166 | +// expect(definitionOfProperty?.getText()).toEqual("f1: z.number()"); |
| 167 | +// expect(parentOfProperty?.getName()).toEqual("Test"); |
| 168 | +// }); |
| 169 | + |
| 170 | +// test("works for object properties inferred from z.object().omit()", () => { |
| 171 | +// // Find usage of TestOmit.f1 property |
| 172 | +// const instanceVariable = |
| 173 | +// sourceFile.getVariableDeclarationOrThrow("instanceOfTestOmit"); |
| 174 | +// const propertyBeingAssigned = getPropertyBeingAssigned( |
| 175 | +// instanceVariable, |
| 176 | +// "f1" |
| 177 | +// ); |
| 178 | + |
| 179 | +// // Find definition of TestOmit.f1 property |
| 180 | +// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0]; |
| 181 | +// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind( |
| 182 | +// SyntaxKind.VariableDeclaration |
| 183 | +// ); |
| 184 | + |
| 185 | +// // Assert that find definition returned the Zod definition of Test |
| 186 | +// expect(definitionOfProperty?.getText()).toEqual("f1: z.number()"); |
| 187 | +// expect(parentOfProperty?.getName()).toEqual("Test"); |
| 188 | +// }); |
| 189 | +// }); |
| 190 | + |
| 191 | +// const getPropertyBeingAssigned = (node: Node, name: string) => { |
| 192 | +// const propertyAssignment = node.forEachDescendant((descendent) => |
| 193 | +// Node.isPropertyAssignment(descendent) && descendent.getName() == name |
| 194 | +// ? descendent |
| 195 | +// : undefined |
| 196 | +// ); |
| 197 | + |
| 198 | +// if (propertyAssignment == null) |
| 199 | +// fail(`Could not find property assignment with name ${name}`); |
| 200 | + |
| 201 | +// const propertyLiteral = propertyAssignment.getFirstDescendantByKind( |
| 202 | +// SyntaxKind.Identifier |
| 203 | +// ); |
| 204 | + |
| 205 | +// if (propertyLiteral == null) |
| 206 | +// fail(`Could not find property literal with name ${name}`); |
| 207 | + |
| 208 | +// return propertyLiteral; |
| 209 | +// }; |
0 commit comments