-
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #277 from hey-api/chore/cleanup-compiler-api-objec…
…t-generation
- Loading branch information
Showing
10 changed files
with
225 additions
and
152 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,54 @@ | ||
import ts from 'typescript'; | ||
|
||
import { isType, ots } from './utils'; | ||
|
||
/** | ||
* Convert an unknown value to an expression. | ||
* @param value - the unknown value. | ||
* @returns ts.Expression | ||
*/ | ||
const toExpression = (value: unknown): ts.Expression | undefined => { | ||
if (Array.isArray(value)) { | ||
return createArrayType(value); | ||
} else if (typeof value === 'object' && value !== null) { | ||
return createObjectType(value); | ||
} else if (typeof value === 'number') { | ||
return ots.number(value); | ||
} else if (typeof value === 'boolean') { | ||
return ots.boolean(value); | ||
} else if (typeof value === 'string') { | ||
return ots.string(value); | ||
} else if (value === null) { | ||
return ts.factory.createNull(); | ||
} | ||
}; | ||
|
||
/** | ||
* Create Array type expression. | ||
* @param arr - The array to create. | ||
* @param multiLine - if the array should be multiline. | ||
* @returns ts.ArrayLiteralExpression | ||
*/ | ||
export const createArrayType = <T>(arr: T[], multiLine: boolean = false): ts.ArrayLiteralExpression => | ||
ts.factory.createArrayLiteralExpression( | ||
arr.map(v => toExpression(v)).filter(isType<ts.Expression>), | ||
// Multiline if the array contains objects, or if specified by the user. | ||
(!Array.isArray(arr[0]) && typeof arr[0] === 'object') || multiLine | ||
); | ||
|
||
/** | ||
* Create Object type expression. | ||
* @param obj - the object to create. | ||
* @param multiLine - if the object should be multiline. | ||
* @returns ts.ObjectLiteralExpression | ||
*/ | ||
export const createObjectType = <T extends object>(obj: T, multiLine: boolean = true): ts.ObjectLiteralExpression => | ||
ts.factory.createObjectLiteralExpression( | ||
Object.entries(obj) | ||
.map(([key, value]) => { | ||
const initializer = toExpression(value); | ||
return initializer ? ts.factory.createPropertyAssignment(key, initializer) : undefined; | ||
}) | ||
.filter(isType<ts.PropertyAssignment>), | ||
multiLine | ||
); |
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
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
Oops, something went wrong.