Skip to content

Commit 3cde16e

Browse files
Add default value for internal tags (#1589) (#1591)
Co-authored-by: Sylvain Wallez <[email protected]>
1 parent ce63699 commit 3cde16e

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

compiler/src/model/metamodel.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,10 @@ export class ExternalTag {
183183

184184
export class InternalTag {
185185
kind: 'internal_tag'
186-
tag: string // Name of the property that holds the variant tag
186+
/* Name of the property that holds the variant tag */
187+
tag: string
188+
/* Default value for the variant tag if it's missing */
189+
defaultTag?: string
187190
}
188191

189192
export class Container {

compiler/src/model/utils.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ export function parseVariantsTag (jsDoc: JSDoc[]): model.Variants | undefined {
924924
return undefined
925925
}
926926

927-
const [type, value] = tags.variants.split(' ')
927+
const [type, ...values] = tags.variants.split(' ')
928928
if (type === 'external') {
929929
return { kind: 'external_tag' }
930930
}
@@ -934,14 +934,14 @@ export function parseVariantsTag (jsDoc: JSDoc[]): model.Variants | undefined {
934934
}
935935

936936
assert(jsDoc, type === 'internal', `Bad variant type: ${type}`)
937-
assert(jsDoc, typeof value === 'string', 'Internal variant requires a tag definition')
938-
const [tag, property] = value.split('=')
939-
assert(jsDoc, tag === 'tag', 'The tag name should be "tag"')
940-
assert(jsDoc, typeof property === 'string', 'The tag property is not defined')
937+
938+
const pairs = parseKeyValues(jsDoc, values, 'tag', 'default')
939+
assert(jsDoc, typeof pairs.tag === 'string', 'Internal variant requires a tag definition')
941940

942941
return {
943942
kind: 'internal_tag',
944-
tag: property.replace(/'/g, '')
943+
tag: pairs.tag,
944+
defaultTag: pairs.default
945945
}
946946
}
947947

@@ -970,6 +970,21 @@ export function parseCommaSeparated (value: string): string[] {
970970
return value.split(',').map(v => v.trim().replace(/["']/g, ''))
971971
}
972972

973+
/**
974+
* Parses an array of "key=value" pairs and validate key names. Values can optionally be enclosed with single
975+
* or double quotes.
976+
*/
977+
export function parseKeyValues (node: Node | Node[], pairs: string[], ...validKeys: string[]): Record<string, string> {
978+
const result = {}
979+
pairs.forEach(item => {
980+
const kv = item.split('=')
981+
assert(node, kv.length === 2, 'Malformed key/value list')
982+
assert(node, validKeys.includes(kv[0]), `Unknown key '${kv[0]}'`)
983+
result[kv[0]] = kv[1].replace(/["']/g, '')
984+
})
985+
return result
986+
}
987+
973988
/**
974989
* Given a declaration, returns true if the declaration
975990
* if defined but never used, false otherwise.

output/schema/schema.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

specification/_types/analysis/analyzers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export class WhitespaceAnalyzer {
110110
version?: VersionString
111111
}
112112

113-
/** @variants internal tag='type' */
113+
/** @variants internal tag='type' default='custom' */
114114
export type Analyzer =
115115
| CustomAnalyzer
116116
| FingerprintAnalyzer

specification/_types/mapping/Property.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class PropertyBase {
4949
fields?: Dictionary<PropertyName, Property>
5050
}
5151

52-
/** @variants internal tag='type' */
52+
/** @variants internal tag='type' default='object' */
5353
export type Property =
5454
| FlattenedProperty
5555
| JoinProperty

0 commit comments

Comments
 (0)