Skip to content

Commit

Permalink
fix(R): Improve code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Jul 27, 2019
1 parent 8d1176b commit 9a438f3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
40 changes: 26 additions & 14 deletions src/r.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,44 +25,56 @@ async function build(): Promise<void> {
.map(unionGenerator)
.join('\n')

const code = `
const code = `# This file was automatically generated by \`${path.basename(
__filename
)}\`.
# Do not modify it by hand. Instead, modify the source \`.schema.yaml\` files
# in the \`schema\` directory and run \`npm run build:py\` to regenerate it.
${classesCode}
${unionsCode}
`

await fs.writeFile(path.join(__dirname, '..', 'R', 'types.R'), code)
await fs.writeFile(path.join(__dirname, '..', 'r', 'R', 'types.R'), code)
}

/**
* Generate a constructor function for a normal type.
*/
export function classGenerator(schema: Schema): string {
const { title = 'Untitled', extends: parent, description = title } = schema
const { inherited, own, required, optional } = props(schema)
const { all, inherited, own } = props(schema)

let code = docComment(description, [
...[...required, ...optional].map(
({ name, schema }) => `@param ${name} ${schema.description}`
`@name ${title}`,
...all.map(
({ name, schema, optional }) =>
`@param ${name} ${schema.description}. ${
!optional ? `\\bold{Required}` : ''
}.`
),
`@seealso \\code{\\link{${parent}}}`,
'@export'
])
code += `${title} <- function (\n`
code += [...required, ...optional].map(({ name }) => ` ${name}`).join(',\n')
code += `${title} <- function(\n`
code += all.map(({ name }) => ` ${name}`).join(',\n')
code += `\n){\n`

if (parent === undefined) {
code += ` self <- list()\n`
} else {
code += ` self <- ${parent}(\n`
code += inherited.map(({ name }) => ` ${name}=${name}`).join(',\n')
code += inherited.map(({ name }) => ` ${name} = ${name}`).join(',\n')
code += '\n )\n'
}

code += own
.map(({ name, schema }) => {
.map(({ name, optional, schema }) => {
const type = schemaToType(schema)
return ` if(!missing(${name})) setProp(self, "${name}", ${type}, ${name})`
return ` self[["${name}"]] <- check_property("${title}", "${name}", ${
optional ? 'FALSE' : 'TRUE'
}, missing(${name}), ${type}, ${name})`
})
.join('\n')

Expand Down Expand Up @@ -90,11 +102,11 @@ export function unionGenerator(schema: Schema): string {
*/
function docComment(description: string, tags: string[] = []): string {
return (
'#` ' +
description.trim().replace('\n', '\n#` ') +
"#' " +
description.trim().replace(/[\n\r]+/g, ' ') +
'\n' +
'#`\n' +
tags.map(tag => '#` ' + tag.trim().replace('\n', ' ')).join('\n') +
"#'\n" +
tags.map(tag => "#' " + tag.trim().replace(/[\n\r]+/g, ' ')).join('\n') +
'\n'
)
}
Expand Down
4 changes: 3 additions & 1 deletion src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ if (module.parent === null) build()
* and can be used to get a type from its name at compile time.
*/
export const typesInterface = (schemas: Schema[]): string => {
return `export interface Types {\n${schemas.map(({title}) => ` ${title}: ${title}`).join('\n')}\n}`
return `export interface Types {\n${schemas
.map(({ title }) => ` ${title}: ${title}`)
.join('\n')}\n}`
}

/**
Expand Down
22 changes: 19 additions & 3 deletions util/type-maps.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
import { BlockContent, CreativeWork, Delete, Emphasis, InlineContent, Quote, Strong, Subscript, Superscript } from '../types';
import { TypeMap } from './type-map';
import {
BlockContent,
CreativeWork,
Delete,
Emphasis,
InlineContent,
Quote,
Strong,
Subscript,
Superscript
} from '../types'
import { TypeMap } from './type-map'

export type MarkTypes = Delete | Emphasis | Quote | Strong | Subscript | Superscript
export type MarkTypes =
| Delete
| Emphasis
| Quote
| Strong
| Subscript
| Superscript

export const markTypes: TypeMap<MarkTypes> = {
Delete: 'Delete',
Expand Down

0 comments on commit 9a438f3

Please sign in to comment.