map-diff: fix when reading attributes with comments

This commit is contained in:
macabeus 2022-07-16 23:27:26 +01:00 committed by Gunter Labes
parent 5129d61dd2
commit 2d00b7ebd4
2 changed files with 35 additions and 7 deletions

View file

@ -83,7 +83,7 @@ const WmlLang = createLanguage({
AttributeValueText: () => { AttributeValueText: () => {
return seqMap( return seqMap(
regex(/(_ )?/), regex(/(_ )?/),
regex(/[^+\n]*/), regex(/[^+\n#]*/),
(translatable, value) => ( (translatable, value) => (
{ {
value: value.trim(), value: value.trim(),

View file

@ -17,7 +17,7 @@ const wmlName = () => fc.stringOf(
const wmlValue = () => fc.stringOf( const wmlValue = () => fc.stringOf(
fc.constantFrom(...valueChars), { minLength: 1 } fc.constantFrom(...valueChars), { minLength: 1 }
).filter((value) => (value.trimStart().length > 0) && (value.includes('_ ') === false)) ).filter((value) => (value.replace(/_|\s/, '').length > 0) && (value.includes('_ ') === false))
const wmlValueString = () => fc.stringOf( const wmlValueString = () => fc.stringOf(
fc.constantFrom(...valueChars, '\n', '"'), { minLength: 1 } fc.constantFrom(...valueChars, '\n', '"'), { minLength: 1 }
@ -32,9 +32,35 @@ const wmlValueTranslatable = (): fc.Arbitrary<string> => {
) )
} }
const wmlCommentUnmapper = (comment: string) => {
const [padding, value] = comment.split(/(\s*)#(.+)/)
return { padding: padding.length, value }
}
const wmlComment = (): fc.Arbitrary<string> => {
return fc.convertFromNext(
fc.convertToNext(
fc.tuple(
fc.nat({ max: 5 }),
fc.lorem()
)
).map(
([padding, value]) => `${' '.repeat(padding)}#${value}`,
(comment) => {
if (typeof comment !== 'string') {
throw new Error('Invalid type')
}
const segments = wmlCommentUnmapper(comment)
return [segments.padding, segments.value]
}
)
)
}
const fullAttributeUnmapper = (fullAttribute: string) => { const fullAttributeUnmapper = (fullAttribute: string) => {
const [key, keyPadding, valuePadding, value] = fullAttribute.split(/(\s*)=(\s*)/) const { key, keyPadding, valuePadding, value, comment } = fullAttribute.match(/(?<key>[^\s]+)(?<keyPadding>\s*)=(?<valuePadding>\s*)(?<value>[^#]+)(?<comment>#.+)?/)!.groups!
return { key, keyPadding: keyPadding.length, valuePadding: valuePadding.length, value } return { key, keyPadding: keyPadding.length, valuePadding: valuePadding.length, value, comment }
} }
const fullAttributeStringUnmapper = (fullAttributeString: string) => { const fullAttributeStringUnmapper = (fullAttributeString: string) => {
@ -52,17 +78,18 @@ const fullAttribute = (): fc.Arbitrary<string> => {
wmlName(), wmlName(),
fc.nat({ max: 5 }), fc.nat({ max: 5 }),
fc.nat({ max: 5 }), fc.nat({ max: 5 }),
wmlValue() wmlValue(),
fc.option(wmlComment())
) )
).map( ).map(
([key, keyPadding, valuePadding, value]) => `${key}${' '.repeat(keyPadding)}=${' '.repeat(valuePadding)}${value}`, ([key, keyPadding, valuePadding, value, comment]) => `${key}${' '.repeat(keyPadding)}=${' '.repeat(valuePadding)}${value}${comment ? comment : ''}`,
(attribute) => { (attribute) => {
if (typeof attribute !== 'string') { if (typeof attribute !== 'string') {
throw new Error('Invalid type') throw new Error('Invalid type')
} }
const segments = fullAttributeUnmapper(attribute) const segments = fullAttributeUnmapper(attribute)
return [segments.key, segments.keyPadding, segments.valuePadding, segments.value] return [segments.key, segments.keyPadding, segments.valuePadding, segments.value, segments.comment]
} }
) )
) )
@ -95,6 +122,7 @@ export {
wmlName, wmlName,
wmlValue, wmlValue,
wmlValueTranslatable, wmlValueTranslatable,
wmlComment,
fullAttributeUnmapper, fullAttributeUnmapper,
fullAttribute, fullAttribute,
fullAttributeString, fullAttributeString,