Skip to content

Commit

Permalink
refactor: improve style of generated code
Browse files Browse the repository at this point in the history
The generated code should be closer to formatting standads.
  • Loading branch information
Conaclos committed Jun 11, 2023
1 parent f8aec83 commit 93636d9
Show file tree
Hide file tree
Showing 208 changed files with 692 additions and 688 deletions.
64 changes: 36 additions & 28 deletions src/generator/js-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
indent,
jsDoc,
jsRpr,
softSpace,
unindent,
} from "../utils/formatting.js"
import * as utils from "./bare-ast-utils.js"
Expand Down Expand Up @@ -112,7 +113,7 @@ export function generate(schema: ast.Ast, config: Config): string {
}
}
}
return head.trim() + "\n\n" + body.trim() + "\n"
return (head.trim() + "\n\n" + body).trim() + "\n"
}

type Gen = {
Expand Down Expand Up @@ -145,9 +146,7 @@ function genAliasedType(g: Gen, aliased: ast.AliasedType): string {
}
}
const def = genType(g, type)
return def[0] === "\n"
? `${doc}export type ${alias} =${def}`
: `${doc}export type ${alias} = ${def}`
return `${doc}export type ${alias} =${softSpace(def)}`
}

function namespaced(g: Gen, alias: string): string {
Expand Down Expand Up @@ -260,28 +259,30 @@ function genSetType(g: Gen, type: ast.ListType): string {

function genStructType(g: Gen, type: ast.StructType): string {
return unindent(`{
${indent(genStructTypeBody(g, type, ","), 2)}
${indent(genStructTypeBody(g, type), 2)}
}`)
}

function genStructTypeBody(g: Gen, type: ast.StructType, sep = ""): string {
function genStructTypeBody(g: Gen, type: ast.StructType): string {
let result = ""
for (let i = 0; i < type.types.length; i++) {
const field = type.data[i]
const doc = jsDoc(field.comment)
const modifier = field.extra?.mut ? "" : "readonly "
const prop = field.extra?.quoted ? `"${field.name}"` : field.name
result += `${doc}${modifier}${prop}: ${genType(
g,
type.types[i],
)}${sep}\n`
result += `${doc}${modifier}${prop}:${softSpace(
genType(g, type.types[i]),
)}\n`
}
return result.trim()
}

function genStructTypeClassBody(g: Gen, type: ast.StructType): string {
const params = type.data
.map(({ name }, i) => `${name}_: ${genType(g, type.types[i])},`)
.map(
({ name }, i) =>
`${name}_:${softSpace(genType(g, type.types[i]))},`,
)
.join("\n")
return unindent(`${indent(genStructTypeBody(g, type))}
constructor(
Expand All @@ -305,7 +306,7 @@ function genUnionType(g: Gen, type: ast.UnionType): string {
: jsRpr(subtype.data)
result += type.extra?.flat
? `\n${doc}| ${valType}`
: `\n${doc}| { readonly ${tagProp}: ${tagVal}, readonly ${valProp}: ${valType} }`
: `\n${doc}| { readonly ${tagProp}: ${tagVal}; readonly ${valProp}: ${valType} }`
}
return indent(result)
}
Expand All @@ -315,15 +316,17 @@ function genReaderHead(g: Gen, aliased: ast.AliasedType): string {
const rType = aliased.internal ? genType(g, aliased.type) : alias
return g.config.generator === "js"
? `function read${alias}(bc)`
: `function read${alias}(bc: bare.ByteCursor): ${rType}`
: `function read${alias}(bc: bare.ByteCursor):${softSpace(rType)}`
}

function genWriterHead(g: Gen, aliased: ast.AliasedType): string {
const alias = aliased.alias
const xType = aliased.internal ? genType(g, aliased.type) : alias
return g.config.generator === "js"
? `function write${alias}(bc, x)`
: `function write${alias}(bc: bare.ByteCursor, x: ${xType}): void`
: `function write${alias}(bc: bare.ByteCursor, x:${softSpace(
xType,
)}): void`
}

function genDecoderHead(g: Gen, alias: string): string {
Expand Down Expand Up @@ -362,10 +365,10 @@ function genAliasedEnumCode(g: Gen, alias: string, type: ast.EnumType): string {
const body = type.data
.map(({ name, val }) =>
type.extra?.intEnum
? `${name}: ${val},\n${val}: "${name}"`
: `${name}: "${name}"`,
? `${name}: ${val},\n${val}: "${name}",`
: `${name}: "${name}",`,
)
.join(",\n")
.join("\n")
const constAssert = g.config.generator !== "js" ? "as const" : ""
return unindent(`const ${alias} = {
${indent(body, 2)}
Expand All @@ -382,7 +385,9 @@ function genAliasedStructCode(
const params = type.data
.map(
({ name }, i) =>
`${name}_${ts ? `: ${genType(g, type.types[i])},` : ","}`,
`${name}_${
ts ? `:${softSpace(genType(g, type.types[i]))},` : ","
}`,
)
.join("\n")
const assignments = type.data
Expand Down Expand Up @@ -420,7 +425,7 @@ function genReading(g: Gen, type: ast.Type): string {
const body = genReader(g, type)
switch (body[0]) {
case "{": // function body
return `(() => ${indent(body)})()`
return `(() => ${body})()`
case "(": // expression
return body.slice(1, -1) // remove parenthesis
}
Expand Down Expand Up @@ -473,7 +478,7 @@ function genListRawReader(g: Gen, type: ast.ListType): string {
const lenDecoding =
type.data !== null
? `${type.data.val}`
: "bare.readUintSafe(bc)\nif (len === 0) { return [] }"
: "bare.readUintSafe(bc)\nif (len === 0) {\n return []\n}"
const valReading = genReading(g, type.types[0])
return unindent(`{
const len = ${indent(lenDecoding, 2)}
Expand Down Expand Up @@ -555,9 +560,12 @@ function genMapReader(g: Gen, type: ast.MapType): string {
}

function genOptionalReader(g: Gen, type: ast.OptionalType): string {
return unindent(`(bare.readBool(bc)
? ${indent(genReading(g, type.types[0]), 3)}
: ${noneVal(type)})`)
return unindent(
`(bare.readBool(bc) ? ${indent(
genReading(g, type.types[0]),
3,
)} : ${noneVal(type)})`,
)
}

function genSetReader(g: Gen, type: ast.ListType): string {
Expand Down Expand Up @@ -589,8 +597,8 @@ function genStructReader(g: Gen, type: ast.StructType, alias: string): string {
g.symbols.get(alias)?.internal === false
) {
const factoryArgs = type.data
.map((_f, i) => `\n${genReading(g, type.types[i])}`)
.join(",")
.map((_f, i) => `\n${genReading(g, type.types[i])},`)
.join("")
objCreation = `(new ${alias}(${indent(factoryArgs)}\n))`
} else {
objCreation = genObjectReader(g, type)
Expand Down Expand Up @@ -794,7 +802,7 @@ function genMapWriter(g: Gen, type: ast.MapType): string {
const writingVal = genWriting(g, type.types[1], "kv[1]")
return unindent(`{
bare.writeUintSafe(bc, $x.size)
for(const kv of $x) {
for (const kv of $x) {
${indent(writingKey, 2)}
${indent(writingVal, 2)}
}
Expand Down Expand Up @@ -879,7 +887,7 @@ function genStructFlatUnionWriter(g: Gen, union: ast.UnionType): string {
${indent(valWriting, 4)}
} else `
}
body = body.slice(0, -6) // remove last 'else '
body = unindent(body.slice(0, -6)) // remove last 'else '
} else if (
resolved.every((t) => !t.extra?.class) &&
discriminators !== null
Expand Down Expand Up @@ -1009,7 +1017,7 @@ function genEncoder(g: Gen, alias: string): string {
return unindent(`${genEncoderHead(g, alias)} {
const bc = new bare.ByteCursor(
new ${uint8Array}(${config}.initialBufferLength),
${config}
${config},
)
write${alias}(bc, x)
return new ${uint8Array}(bc.view.buffer, bc.view.byteOffset, bc.offset)
Expand Down
4 changes: 4 additions & 0 deletions src/utils/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export function unindent(s: string, n = 1): string {
return s.replace(new RegExp(`\n[ ]{${4 * n}}`, "g"), "\n")
}

export function softSpace(s: string): string {
return s[0] === "\n" ? s : ` ${s}`
}

export function jsDoc(content: string | null): string {
if (content === null) {
return ""
Expand Down
2 changes: 1 addition & 1 deletion tests-corpus/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Config,
configure,
parse,
transform
transform,
} from "@bare-ts/tools"
import { strict as assert } from "node:assert"
import fs from "node:fs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import * as bare from "@bare-ts/lib"
export type u8 = number

export type Person = {
readonly name: string,
readonly age: u8,
readonly name: string
readonly age: u8
}

export function readPerson(bc: bare.ByteCursor): Person
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function writePerson(bc, x) {
export function encodePerson(x) {
const bc = new bare.ByteCursor(
new Uint8Array(config.initialBufferLength),
config
config,
)
writePerson(bc, x)
return new Uint8Array(bc.view.buffer, bc.view.byteOffset, bc.offset)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const config = /* @__PURE__ */ bare.Config({})
export type u8 = number

export type Person = {
readonly name: string,
readonly age: u8,
readonly name: string
readonly age: u8
}

export function readPerson(bc: bare.ByteCursor): Person {
Expand All @@ -24,7 +24,7 @@ export function writePerson(bc: bare.ByteCursor, x: Person): void {
export function encodePerson(x: Person): Uint8Array {
const bc = new bare.ByteCursor(
new Uint8Array(config.initialBufferLength),
config
config,
)
writePerson(bc, x)
return new Uint8Array(bc.view.buffer, bc.view.byteOffset, bc.offset)
Expand Down
4 changes: 2 additions & 2 deletions tests-corpus/valid-bare-schema/alias-doccomment/out.gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function writeU8Alias(bc, x) {
export function encodeU8Alias(x) {
const bc = new bare.ByteCursor(
new Uint8Array(config.initialBufferLength),
config
config,
)
writeU8Alias(bc, x)
return new Uint8Array(bc.view.buffer, bc.view.byteOffset, bc.offset)
Expand All @@ -39,7 +39,7 @@ export function writeU8Alias2(bc, x) {
export function encodeU8Alias2(x) {
const bc = new bare.ByteCursor(
new Uint8Array(config.initialBufferLength),
config
config,
)
writeU8Alias2(bc, x)
return new Uint8Array(bc.view.buffer, bc.view.byteOffset, bc.offset)
Expand Down
4 changes: 2 additions & 2 deletions tests-corpus/valid-bare-schema/alias-doccomment/out.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function writeU8Alias(bc: bare.ByteCursor, x: U8Alias): void {
export function encodeU8Alias(x: U8Alias): Uint8Array {
const bc = new bare.ByteCursor(
new Uint8Array(config.initialBufferLength),
config
config,
)
writeU8Alias(bc, x)
return new Uint8Array(bc.view.buffer, bc.view.byteOffset, bc.offset)
Expand Down Expand Up @@ -54,7 +54,7 @@ export function writeU8Alias2(bc: bare.ByteCursor, x: U8Alias2): void {
export function encodeU8Alias2(x: U8Alias2): Uint8Array {
const bc = new bare.ByteCursor(
new Uint8Array(config.initialBufferLength),
config
config,
)
writeU8Alias2(bc, x)
return new Uint8Array(bc.view.buffer, bc.view.byteOffset, bc.offset)
Expand Down
2 changes: 1 addition & 1 deletion tests-corpus/valid-bare-schema/alias/out.gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function writeU8Alias(bc, x) {
export function encodeU8Alias(x) {
const bc = new bare.ByteCursor(
new Uint8Array(config.initialBufferLength),
config
config,
)
writeU8Alias(bc, x)
return new Uint8Array(bc.view.buffer, bc.view.byteOffset, bc.offset)
Expand Down
2 changes: 1 addition & 1 deletion tests-corpus/valid-bare-schema/alias/out.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function writeU8Alias(bc: bare.ByteCursor, x: U8Alias): void {
export function encodeU8Alias(x: U8Alias): Uint8Array {
const bc = new bare.ByteCursor(
new Uint8Array(config.initialBufferLength),
config
config,
)
writeU8Alias(bc, x)
return new Uint8Array(bc.view.buffer, bc.view.byteOffset, bc.offset)
Expand Down
4 changes: 2 additions & 2 deletions tests-corpus/valid-bare-schema/bigint/out.gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function writeI64(bc, x) {
export function encodeI64(x) {
const bc = new bare.ByteCursor(
new Uint8Array(config.initialBufferLength),
config
config,
)
writeI64(bc, x)
return new Uint8Array(bc.view.buffer, bc.view.byteOffset, bc.offset)
Expand All @@ -39,7 +39,7 @@ export function writeU64(bc, x) {
export function encodeU64(x) {
const bc = new bare.ByteCursor(
new Uint8Array(config.initialBufferLength),
config
config,
)
writeU64(bc, x)
return new Uint8Array(bc.view.buffer, bc.view.byteOffset, bc.offset)
Expand Down
4 changes: 2 additions & 2 deletions tests-corpus/valid-bare-schema/bigint/out.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function writeI64(bc: bare.ByteCursor, x: I64): void {
export function encodeI64(x: I64): Uint8Array {
const bc = new bare.ByteCursor(
new Uint8Array(config.initialBufferLength),
config
config,
)
writeI64(bc, x)
return new Uint8Array(bc.view.buffer, bc.view.byteOffset, bc.offset)
Expand Down Expand Up @@ -46,7 +46,7 @@ export function writeU64(bc: bare.ByteCursor, x: U64): void {
export function encodeU64(x: U64): Uint8Array {
const bc = new bare.ByteCursor(
new Uint8Array(config.initialBufferLength),
config
config,
)
writeU64(bc, x)
return new Uint8Array(bc.view.buffer, bc.view.byteOffset, bc.offset)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function writeData(bc, x) {
export function encodeData(x) {
const bc = new bare.ByteCursor(
new Uint8Array(config.initialBufferLength),
config
config,
)
writeData(bc, x)
return new Uint8Array(bc.view.buffer, bc.view.byteOffset, bc.offset)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function writeData(bc: bare.ByteCursor, x: Data): void {
export function encodeData(x: Data): Uint8Array {
const bc = new bare.ByteCursor(
new Uint8Array(config.initialBufferLength),
config
config,
)
writeData(bc, x)
return new Uint8Array(bc.view.buffer, bc.view.byteOffset, bc.offset)
Expand Down
2 changes: 1 addition & 1 deletion tests-corpus/valid-bare-schema/data-fixed/out.gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function writeData(bc, x) {
export function encodeData(x) {
const bc = new bare.ByteCursor(
new Uint8Array(config.initialBufferLength),
config
config,
)
writeData(bc, x)
return new Uint8Array(bc.view.buffer, bc.view.byteOffset, bc.offset)
Expand Down
2 changes: 1 addition & 1 deletion tests-corpus/valid-bare-schema/data-fixed/out.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function writeData(bc: bare.ByteCursor, x: Data): void {
export function encodeData(x: Data): Uint8Array {
const bc = new bare.ByteCursor(
new Uint8Array(config.initialBufferLength),
config
config,
)
writeData(bc, x)
return new Uint8Array(bc.view.buffer, bc.view.byteOffset, bc.offset)
Expand Down
2 changes: 1 addition & 1 deletion tests-corpus/valid-bare-schema/data/out.gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function writeData(bc, x) {
export function encodeData(x) {
const bc = new bare.ByteCursor(
new Uint8Array(config.initialBufferLength),
config
config,
)
writeData(bc, x)
return new Uint8Array(bc.view.buffer, bc.view.byteOffset, bc.offset)
Expand Down
2 changes: 1 addition & 1 deletion tests-corpus/valid-bare-schema/data/out.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function writeData(bc: bare.ByteCursor, x: Data): void {
export function encodeData(x: Data): Uint8Array {
const bc = new bare.ByteCursor(
new Uint8Array(config.initialBufferLength),
config
config,
)
writeData(bc, x)
return new Uint8Array(bc.view.buffer, bc.view.byteOffset, bc.offset)
Expand Down
Loading

0 comments on commit 93636d9

Please sign in to comment.