UNPKG

@zohodesk/client_build_tool

Version:

A CLI tool to build web applications and client libraries

66 lines (51 loc) 1.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.collectI18nKeysfromAST = collectI18nKeysfromAST; exports.collectI18nKeysfromComments = collectI18nKeysfromComments; var _estreeWalker = require("estree-walker"); function collectI18nKeysfromAST(ast, allI18n) { const i18nKeys = {}; (0, _estreeWalker.walk)(ast, { enter(node) { if (node.type === 'Literal') { if ((node.raw[0] === '\'' || node.raw[0] === '"') && Object.hasOwnProperty.call(allI18n, node.value)) { i18nKeys[node.value] = allI18n[node.value]; } } } }); return Object.keys(i18nKeys); } const prefixI18nComment = 'I18N'; const prefixI18nComment1 = 'dynamic-i18n-key'; function getI18nKeysFromComment(comment) { const commentString = comment.value.trim(); let i18nKeyStr; if (commentString.startsWith(prefixI18nComment)) { i18nKeyStr = commentString.slice(prefixI18nComment.length).trim(); } else if (commentString.startsWith(prefixI18nComment1)) { i18nKeyStr = commentString.slice(prefixI18nComment1.length).trim(); } if (!i18nKeyStr) { return []; } const i18nKeys = i18nKeyStr.split(','); return i18nKeys; } // export function fromComments(comments, allI18n) { function collectI18nKeysfromComments(comments, allI18n) { // TODO: need to implement const i18nKeys = {}; /* eslint-disable no-restricted-syntax */ for (const comment of comments) { const keys = getI18nKeysFromComment(comment); for (const key of keys) { if (key && allI18n[key]) { i18nKeys[key] = allI18n[key]; } } } /* eslint-enable no-restricted-syntax */ return Object.keys(i18nKeys); }