-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent extra constructor params from being serialized
- Loading branch information
1 parent
1c1e6cd
commit f40a649
Showing
7 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env node | ||
import "../dist/extract_serializable_fields.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import ts from "typescript"; | ||
import * as path from "path"; | ||
|
||
function extractConstructorParams( | ||
sourceFile: string, | ||
className: string | ||
): { type: string; fields: string[] } | null { | ||
const absolutePath = path.resolve(sourceFile); | ||
const program = ts.createProgram([absolutePath], { | ||
target: ts.ScriptTarget.ES2015, | ||
module: ts.ModuleKind.CommonJS, | ||
}); | ||
const source = program.getSourceFile(absolutePath); | ||
const typeChecker = program.getTypeChecker(); | ||
|
||
if (!source) { | ||
console.error(`Could not find source file: ${absolutePath}`); | ||
return null; | ||
} | ||
|
||
let result: { type: string; fields: string[] } | null = null; | ||
|
||
function visit(node: ts.Node) { | ||
if (ts.isClassDeclaration(node) && node.name?.text === className) { | ||
node.members.forEach((member) => { | ||
if ( | ||
ts.isConstructorDeclaration(member) && | ||
member.parameters.length > 0 | ||
) { | ||
const firstParam = member.parameters[0]; | ||
const type = typeChecker.getTypeAtLocation(firstParam); | ||
const typeString = typeChecker.typeToString(type); | ||
|
||
// Get properties of the type | ||
const fields: string[] = []; | ||
type.getProperties().forEach((prop) => { | ||
// Get the type of the property | ||
const propType = typeChecker.getTypeOfSymbolAtLocation( | ||
prop, | ||
firstParam | ||
); | ||
// Only include non-function properties that don't start with __ | ||
if ( | ||
!prop.getName().startsWith("__") && | ||
prop.getName() !== "callbackManager" && | ||
!(propType.getCallSignatures().length > 0) | ||
) { | ||
fields.push(prop.getName()); | ||
} | ||
}); | ||
|
||
result = { | ||
type: typeString, | ||
fields: fields, | ||
}; | ||
} | ||
}); | ||
} | ||
ts.forEachChild(node, visit); | ||
} | ||
|
||
visit(source); | ||
return result; | ||
} | ||
const filepath = process.argv[2]; | ||
const className = process.argv[3]; | ||
|
||
if (!filepath || !className) { | ||
console.error( | ||
"Usage: node extract_serializable_fields.ts <filepath> <className>" | ||
); | ||
process.exit(1); | ||
} | ||
|
||
const results = extractConstructorParams(filepath, className); | ||
|
||
if (results?.fields?.length) { | ||
console.log(JSON.stringify(results?.fields, null, 2)); | ||
} else { | ||
console.error("No constructor parameters found"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters