Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
d-kimsuon committed Dec 10, 2021
2 parents 26d14ce + 19f24c4 commit 8df53cd
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
1 change: 1 addition & 0 deletions example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { run } from "./src/generate"
watch: false,
asserts: true,
defaultArrayCheckOption: "all",
comment: false,
},
})
})()
3 changes: 3 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ program
"how to check child element type. 'all' or 'first'",
"all"
)
.option("-c, --comment", "generate JSDoc comments or not", false)
.parse(process.argv)

const option = program.opts<{
Expand All @@ -31,6 +32,7 @@ const option = program.opts<{
asserts: boolean
watch: boolean
defaultArrayCheckOption: ArrayCheckOption
comment: boolean
}>()

const cwd = process.cwd()
Expand All @@ -44,5 +46,6 @@ run({
asserts: option.asserts,
watch: option.watch,
defaultArrayCheckOption: option.defaultArrayCheckOption,
comment: option.comment,
},
})
47 changes: 44 additions & 3 deletions src/generate/generate-type-predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ export function generateTypePredicates(
}[]
}[],
asserts = false,
defaultArrayCheckOption: ArrayCheckOption = "all"
defaultArrayCheckOption: ArrayCheckOption = "all",
comment = false
): string {
const usedPrimitives: to.PrimitiveTO["kind"][] = []
const usedSpecials: to.SpecialTO["kind"][] = []
Expand Down Expand Up @@ -236,6 +237,38 @@ export function generateTypePredicates(
}`
}

const generateJSDocComment = ({
type,
typeName,
isAssertion,
}: {
type: to.TypeObject
typeName: string
isAssertion: boolean
}): string => {
return isAssertion
? `\
/**
* Assert if a variable is of type {@link ${typeName}} and throws a TypeError if the assertion fails.
* This function is automatically generated using [type-predicates-generator](https://www.npmjs.com/package/type-predicates-generator).
* @param value Argument to inspect.
* @throw TypeError if the given argument is not compatible with the type {@link ${typeName}}.
*/
`
: `\
/**
* Check if a variable is of type {@link ${typeName}} and narrow it down to that type if the check passes.
* This function is automatically generated using [type-predicates-generator](https://www.npmjs.com/package/type-predicates-generator).
* @param arg_0 Argument to inspect.${
type.__type === "ArrayTO"
? "\n * @param checkOpt Whether to check all elements of the array or only the first one."
: ""
}
* @return \`true\` if the argument is of type {@link ${typeName}}, \`false\` otherwise.
*/
`
}

const generatedTypeNames: string[] = []
const skipImports: {
typeName: string
Expand Down Expand Up @@ -281,14 +314,22 @@ export function generateTypePredicates(

generatedTypeNames.push(typeName)

return `export const is${typeName} = ${generateCheckFn({
return `${
comment
? generateJSDocComment({ type, typeName, isAssertion: false })
: ""
}export const is${typeName} = ${generateCheckFn({
type,
typeName,
parentArgCount: -1,
})};
${
asserts
? `export function assertIs${typeName}(value: unknown): asserts value is ${typeName} {
? `${
comment
? generateJSDocComment({ type, typeName, isAssertion: true })
: ""
}export function assertIs${typeName}(value: unknown): asserts value is ${typeName} {
if (!is${typeName}(value)) throw new TypeError(\`value must be ${typeName} but received \${value}\`)
};`
: ""
Expand Down
6 changes: 4 additions & 2 deletions src/generate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type GenerateOption = {
asserts: boolean
watch: boolean
defaultArrayCheckOption: ArrayCheckOption
comment: boolean
}

export async function run({
Expand Down Expand Up @@ -84,7 +85,7 @@ const generateAndWriteCodes = (
program: ts.Program,
files: string[],
output: string,
{ asserts, defaultArrayCheckOption }: GenerateOption
{ asserts, defaultArrayCheckOption, comment }: GenerateOption
) => {
const handler = new CompilerApiHandler(program)

Expand Down Expand Up @@ -116,7 +117,8 @@ const generateAndWriteCodes = (
const generatedCode = generateTypePredicates(
types,
asserts,
defaultArrayCheckOption
defaultArrayCheckOption,
comment
)
writeFileSync(output, generatedCode)
}

0 comments on commit 8df53cd

Please sign in to comment.