From ed87d7b5918926c3502592ee54d7c5c416f710c2 Mon Sep 17 00:00:00 2001 From: Erik Golinelli Date: Fri, 10 May 2024 22:01:09 +0200 Subject: [PATCH] wip --- src/moparser.js | 1 - src/pocompiler.js | 47 ++++++++++++++++++++++++++++++----------------- src/poparser.js | 15 +++++++++++---- src/shared.js | 12 ++++++++---- src/types.js | 8 +++++++- tsconfig.json | 2 +- 6 files changed, 57 insertions(+), 28 deletions(-) diff --git a/src/moparser.js b/src/moparser.js index 9618dbe..5e57afa 100644 --- a/src/moparser.js +++ b/src/moparser.js @@ -40,7 +40,6 @@ function Parser (fileContents, defaultCharset = 'iso-8859-1') { * Translation table * * @type {import('./types.js').GetTextTranslations} table Translation object - * @private */ this._table = { charset: this._charset, diff --git a/src/pocompiler.js b/src/pocompiler.js index f6aafea..26bf9a4 100644 --- a/src/pocompiler.js +++ b/src/pocompiler.js @@ -19,33 +19,46 @@ export default function (table, options) { } /** - * Creates a PO compiler object. + * Takes the header object and converts all headers into the lowercase format * - * @constructor - * @param {import('./types.js').GetTextTranslations|{}} table Translation table to be compiled - * @param {import('./types.js').parserOptions|{}} [options] Options + * @param {{ [x: string]: any; }} headersRaw the headers to prepare + * @returns {{ [x: string]: any; }} the headers in the lowercase format */ -function Compiler (table = {}, options = {}) { - this._table = table; - this._options = options; - - this._table.translations = this._table.translations || {}; - - let { headers = {} } = this._table; - - headers = Object.keys(headers).reduce((result, key) => { +function prepareHeaders(headersRaw) { + return Object.keys(headersRaw).reduce((/** @type {{ [x: string]: any; }} */ result, key) => { const lowerKey = key.toLowerCase(); + const value = HEADERS.get(lowerKey) - if (HEADERS.has(lowerKey)) { - result[HEADERS.get(lowerKey)] = headers[key]; + if (typeof value === 'string') { + result[value] = headersRaw[key]; } else { - result[key] = headers[key]; + result[key] = headersRaw[key]; } return result; }, {}); +} - this._table.headers = headers; +/** + * Creates a PO compiler object. + * + * @constructor + * @param {import('./types.js').GetTextTranslations} [table] Translation table to be compiled + * @param {import('./types.js').parserOptions} [options] Options + */ +function Compiler (table , options ) { + /** @type {import('./types.js').parserOptions} _options the parser options object */ + this._options = options ?? {}; + /** + * The compiled translation table + * @type {import('./types.js').GetTextTranslations} _table Translation table + */ + this._table = table ?? { translations: {}, charset: (this._options && this._options.defaultCharset ? this._options.defaultCharset : 'utf-8') }; + /** + * Headers of the PO file + * @type {Headers} The Headers of the PO file + */ + this._table.headers = this._table.headers ?? {}; if (!('foldLength' in this._options)) { this._options.foldLength = 76; diff --git a/src/poparser.js b/src/poparser.js index 6b48144..37d327f 100644 --- a/src/poparser.js +++ b/src/poparser.js @@ -339,11 +339,14 @@ Parser.prototype._handleKeys = function (tokens) { /** * Separate different values into individual translation objects * - * @param {Object} tokens Parsed tokens - * @return {Object} Tokens + * @param {import("./types.js").GetTextTranslation[]} tokens Parsed tokens + * @return {import("./types.js").GetTextTranslations} Tokens */ Parser.prototype._handleValues = function (tokens) { const response = []; + /** + * @type {import("./types.js").GetTextTranslation} + */ let lastNode; let curContext; let curComments; @@ -445,10 +448,14 @@ Parser.prototype._validateToken = function ( /** * Compose a translation table from tokens object * - * @param {Object} tokens Parsed tokens - * @return {Object} Translation table + * @param {import("./types.js").GetTextTranslation[]} tokens Parsed tokens + * @return {import("./types.js").GetTextTranslations} Translation table */ Parser.prototype._normalize = function (tokens) { + /** + * Translation table to be returned + * @type {import("./types.js").GetTextTranslations} Translation table + */ const table = { charset: this._charset, headers: undefined, diff --git a/src/shared.js b/src/shared.js index cfe3abe..e079f02 100644 --- a/src/shared.js +++ b/src/shared.js @@ -1,5 +1,7 @@ // see https://www.gnu.org/software/gettext/manual/html_node/Header-Entry.html +/** @type {string} Header name for "Plural-Forms" */ const PLURAL_FORMS = 'Plural-Forms'; +/** @typedef {Map} Headers Map of header keys to header names */ export const HEADERS = new Map([ ['project-id-version', 'Project-Id-Version'], ['report-msgid-bugs-to', 'Report-Msgid-Bugs-To'], @@ -22,8 +24,10 @@ const PLURAL_FORM_HEADER_NPLURALS_REGEX = /nplurals\s*=\s*(?\d+)/; * @return {Object} An object of key-value pairs */ export function parseHeader (str = '') { - return str.split('\n') - .reduce((headers, line) => { + /** @type {string} Header string */ + return str + .split('\n') + .reduce((/** @type {Record} */ headers , line) => { const parts = line.split(':'); let key = (parts.shift() || '').trim(); @@ -42,7 +46,7 @@ export function parseHeader (str = '') { /** * Attempts to safely parse 'nplurals" value from "Plural-Forms" header * - * @param {Object} [headers = {}] An object with parsed headers + * @param {{[key: string]: string}} [headers] An object with parsed headers * @param {number} fallback Fallback value if "Plural-Forms" header is absent * @returns {number} Parsed result */ @@ -63,7 +67,7 @@ export function parseNPluralFromHeadersSafely (headers, fallback = 1) { /** * Joins a header object of key value pairs into a header string * - * @param {Object} header Object of key value pairs + * @param {{[key: string]: string}} header Object of key value pairs * @return {String} Header string */ export function generateHeader (header = {}) { diff --git a/src/types.js b/src/types.js index 269befa..b443507 100644 --- a/src/types.js +++ b/src/types.js @@ -16,13 +16,15 @@ * @property {string} [msgid_plural] The plural message ID. * @property {string[]} msgstr Array of translated strings. * @property {GetTextComment} [comments] Comments associated with the translation. + * @property {GetTextTranslation} [obsolete] Whether the translation is obsolete. */ /** * Represents GetText translations. * @typedef {Object} GetTextTranslations * @property {string} charset Character set. - * @property {Object.} headers Headers. + * @property {Object.} [headers] Headers. + * @property {Object.} [obsolete] Obsolete messages. * @property {Object.>} translations Translations. */ @@ -31,4 +33,8 @@ * @typedef {Object} parserOptions * @property {string} [defaultCharset] Default character set. * @property {boolean} [validation] Whether to perform validation. + * @property {number} [foldLength] the fold length. + * @property {boolean} [escapeCharacters] Whether to escape characters. + * @property {boolean} [sort] Whether to sort messages. + * @property {string} [eol] End of line character. */ diff --git a/tsconfig.json b/tsconfig.json index 5dd7090..2186fcd 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,7 +13,7 @@ "allowJs": true, // Check js files for errors - "checkJs": false, + "checkJs": true, // the directory sources are in "rootDir": "src",