properties-to-ftl/lib/migrate-message.js

134 строки
3.3 KiB
JavaScript
Исходник Обычный вид История

import kebabCase from 'lodash.kebabcase'
2022-01-05 15:30:50 +03:00
import { fail } from './util-fail.js'
/**
2021-11-30 17:41:40 +03:00
* @typedef {{
* key: string,
* attr: string | null,
* plural: string | false | null,
* varNames: string[]
* }} MessageMigration
*/
/**
* Determines the message's FTL interface and returns it.
*
* May update `propData.migrate[propKey].varNames` if given additional `varNames`.
*
* @param {import('./parse-message-files.js').PropData} propData
* @param {string} propKey
2021-11-11 13:53:49 +03:00
* @param {string[] | null} [varNames]
* @returns {MessageMigration}
*/
export function migrateMessage(propData, propKey, varNames) {
2021-11-30 17:41:40 +03:00
const { ast, migrate } = propData
/** @type {import('dot-properties').Pair | undefined} */
const propNode = ast.find(
(node) => node.type === 'PAIR' && node.key === propKey
)
if (!varNames) varNames = initVarNames(propNode)
const prev = migrate[propKey]
if (prev) {
for (let i = prev.varNames.length; i < varNames.length; ++i) {
if (propData.hasMigrateConfig) {
2022-01-05 15:30:50 +03:00
fail(
`Expected all message variables to be included in migration config: ${propKey}`
)
}
prev.varNames[i] = varNames[i]
}
return prev
}
if (propData.hasMigrateConfig) {
2022-01-05 15:30:50 +03:00
fail(`Expected message to be included in migration config: ${propKey}`)
}
2022-01-05 17:48:37 +03:00
let dot, key, attr
switch (propData.attrDot) {
case 'first':
dot = propKey.indexOf('.')
break
case 'last':
dot = propKey.lastIndexOf('.')
break
default:
dot = -1
}
if (dot === -1) {
key = propKey
attr = null
} else {
key = propKey.substring(0, dot)
attr = propKey.substring(dot + 1)
}
return {
key: getFtlKey(propData, key),
attr: attr ? kebabCase(attr) : null,
2021-11-30 17:41:40 +03:00
plural: propNode?.value?.includes(';') ? 'FIXME' : null,
varNames
}
}
2021-11-11 13:53:49 +03:00
/**
2021-11-30 17:41:40 +03:00
* @param {import('dot-properties').Pair | undefined} node
2021-11-11 13:53:49 +03:00
*/
2021-11-30 17:41:40 +03:00
function initVarNames(node) {
2021-11-11 13:53:49 +03:00
/** @type {string[]} */
const varNames = []
if (node) {
let num = 0
for (const match of node.value.matchAll(/%(\d\$)?S/g)) {
num += 1
if (match[1]) {
const n = parseInt(match[1])
varNames[n - 1] = `var${n}`
} else {
varNames.push(`var${num}`)
}
}
}
return varNames
}
/**
* @param {import('./parse-message-files.js').PropData} propData
2021-11-30 17:41:40 +03:00
* @param {string} key
* @returns {string}
*/
function getFtlKey({ migrate, ftlPrefix, ftl }, key) {
const attrRoot = `${key}.`
const prev = Object.keys(migrate).find(
(m) => m === key || m.startsWith(attrRoot)
)
if (prev) return migrate[prev].key
let ftlKey = kebabCase(`${ftlPrefix}-${key}`)
const dm = ftlKey.match(/-\d+$/)
if (dm) {
// Try to drop numerical suffix
const bare = ftlKey.substring(0, ftlKey.length - dm[0].length)
if (!ftlKeyExists(ftl, migrate, bare)) ftlKey = bare
}
// If required, add a numerical suffix
let n = 1
while (ftlKeyExists(ftl, migrate, ftlKey))
ftlKey = ftlKey.replace(/(-\d+)?$/, `-${++n}`)
return ftlKey
}
/**
2021-11-02 14:24:06 +03:00
* @param {import('@fluent/syntax').Resource} ftl
* @param {Record<string, MessageMigration>} migrate
* @param {string} key
* @returns
*/
function ftlKeyExists(ftl, migrate, key) {
for (const entry of ftl.body)
if (entry.type === 'Message' && entry.id.name === key) return true
for (const entry of Object.values(migrate)) if (entry.key === key) return true
return false
}