Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declare kind property #331

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
The format is based on [Keep a Changelog](http://keepachangelog.com/).

## Version 0.27.0 - TBD
### Fixed
- `static kind` property received the missing `declare` modifier

## Version 0.26.0 - 2024-09-11
### Added
Expand Down
26 changes: 25 additions & 1 deletion lib/components/typescript.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
/**
* @param {object} options - the options
* @param {string} options.name - name of the property
* @param {boolean} [options.declare] - whether the property receives a "declared" modifier
* @param {boolean} [options.readonly] - whether the property receives a "readonly" modifier
* @param {boolean} [options.override] - whether the property receives a "readonly" modifier
* @param {string} [options.type] - the type of the property
* @param {string} [options.rhs] - the right-hand side of the property (initialiser)
* @param {string} [options.statementEnd] - the statement end
*/
function staticProperty ({ name, declare = true, readonly = true, override = false, type, rhs, statementEnd = ';' }) {
if (declare && override) throw new Error('Cannot declare and override a property at the same time')
let property = name
if (readonly) property = `readonly ${property}`
if (override) property = `override ${property}`
property = `static ${property}`
if (declare) property = `declare ${property}`
if (type) property = `${property}: ${type}`
if (rhs) property = `${property} = ${rhs}`
if (statementEnd) property += statementEnd
return property
}

module.exports = {
empty: 'Record<never, never>'
empty: 'Record<never, never>',
staticProperty
}
10 changes: 9 additions & 1 deletion lib/components/wrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ const createPromiseOf = t => `Promise<${t}>`
*/
const deepRequire = (t, lookup = '') => `${base}.DeepRequired<${t}>${lookup}`

/**
* Wraps a string in single quotes. No escaping is done, so use with caution.
* @param {string} s - the string to wrap.
* @returns {string}
*/
const stringIdent = s => `'${s}'`

/**
* Puts a passed string in docstring format.
* @param {string | undefined} doc - raw string to docify. May contain linebreaks.
Expand All @@ -94,5 +101,6 @@ module.exports = {
createCompositionOfOne,
createCompositionOfMany,
deepRequire,
docify
docify,
stringIdent
}
15 changes: 10 additions & 5 deletions lib/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const { SourceFile, FileRepository, Buffer, Path } = require('./file')
const { FlatInlineDeclarationResolver, StructuredInlineDeclarationResolver } = require('./components/inline')
const { Resolver } = require('./resolution/resolver')
const { LOG } = require('./logging')
const { docify, createPromiseOf, createUnionOf } = require('./components/wrappers')
const { docify, createPromiseOf, createUnionOf, stringIdent } = require('./components/wrappers')
const { csnToEnumPairs, propertyToInlineEnumName, isInlineEnumType, stringifyEnumType } = require('./components/enum')
const { isReferenceType } = require('./components/reference')
const { empty } = require('./components/typescript')
const { empty, staticProperty } = require('./components/typescript')
const { baseDefinitions } = require('./components/basedefs')
const { EntityRepository, asIdentifier } = require('./resolution/entity')
const { last } = require('./components/identifier')
Expand Down Expand Up @@ -144,7 +144,7 @@ class Visitor {
? ancestors.map(a => `typeof ${asIdentifier({info: a, relative: file.path})}.actions`).join(' & ') + ' & '
: ''
if (actions.length) {
buffer.addIndentedBlock(`declare static readonly actions: ${inherited}{`,
buffer.addIndentedBlock(`${staticProperty({name: 'actions', statementEnd: ''})}: ${inherited}{`,
actions.map(([aname, action]) => SourceFile.stringifyLambda({
name: aname,
parameters: this.#stringifyFunctionParams(action.params, file),
Expand All @@ -156,7 +156,7 @@ class Visitor {
})), '}'
) // end of actions
} else {
buffer.add(`declare static readonly actions: ${inherited}${empty}`)
buffer.add(staticProperty({name: 'actions', type: `${inherited}${empty}`}))
}
}

Expand Down Expand Up @@ -261,7 +261,12 @@ class Visitor {
}

if ('kind' in entity) {
buffer.addIndented([`static readonly kind: 'entity' | 'type' | 'aspect' = '${entity.kind}';`])
buffer.addIndented(staticProperty({
name: 'kind',
type: '"entity" | "type" | "aspect"',
rhs: stringIdent(entity.kind),
declare: false
}))
}

buffer.addIndented(() => {
Expand Down