diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..e3bd548 Binary files /dev/null and b/.DS_Store differ diff --git a/src/plugin-helpers.js b/src/plugin-helpers.js index 4db0a91..c5239d6 100644 --- a/src/plugin-helpers.js +++ b/src/plugin-helpers.js @@ -14,6 +14,21 @@ const isBlacklistedForJsxAttribute = (path) => { 'style', 'className', 'color', // Code 'dangerouslySetInnerHTML', 'src', + // custom + 'width', 'variant', 'dataTestId', 'borderRadius', 'id', 'alt', 'background', 'link', 'ariaLabel', + 'aria-label', 'alignItems', 'onMouseEnter', 'data-test-id', 'paddingLeft', 'maxWidth', 'padding', + 'marginTop', 'horizontal', 'vertical', 'anchorOrigin', 'transformOrigin', 'PaperProps', 'navigate', + '$overflowY', 'shortLivedTokenExpiredAction', 'height', 'field', 'active', 'emoji', 'trackInHeap', + 'onClick', 'paddingTop', 'icon', 'key', 'HelperTextProps', 'permissions', 'path', + 'featureFlag', 'unauthenticatedAction', 'minWidth','paperProps', 'paddingBottom', 'paddingRight', 'bgcolor', + 'pt', 'border', 'underline', 'whiteSpace', 'textOverflow', 'display', 'overflow', 'component', 'offsetforflash', + 'offsetforonboardingsurvey', 'mobile', 'headerpresent', 'footerpresent', 'nohoverunderlined', 'edition', + 'underlined', 'keyDataTestId', 'margin', 'maxHeight', 'top', 'position', 'placement', 'align', + 'textAlign', 'rootMargin', 'setAnswer', 'onValueChange', 'onChange', 'editImportsUrl','InputProps', + 'selectedCategory', 'setSelectedCategory', 'boxSizing', 'p', 'size', 'mt', 'keyTestId', 'panelTitleVariant', + 'cursor', 'storageKey', 'emoj', 'selectedEmissionFactor', 'track', 'color', 'justifyContent', 'inputVariant', 'updatedData', + 'dataTestIdRedirection', '$height', 'active', 'bgColor', 'pl', 'direction', 'target', 'getProps' + ]; const jsxAttributeParent = path.findParent(p => p.isJSXAttribute()); if (!jsxAttributeParent) return false; @@ -22,6 +37,48 @@ const isBlacklistedForJsxAttribute = (path) => { return false; }; +const handleBlacklistedNode = (path) => { + const blacklistValue = [ + '/app/organise' + + ]; + + if (blacklistValue.includes(path.node.value)) return true; + + +} + +const handleBlackListKey = (key) => { + const blacklistValue = [ + 'kind', 'hasPermission', 'InitiativesTableColumnSizes' + ]; + + if (blacklistValue.includes(key)) return true; +} + +const handleBlackListValue = (key) => { + const blacklistValues = [ + '/app/measure?show=ghg-protocol-upload', 'canEditTree', 'canEditOwnApiKey', 'canEditSegment', 'canEditCustomEmissionFactor' ,'canEditInitiative', 'canEditTarget', 'canEditEmissionIncrease', 'market_based', + '+99', 'edit-track', 'all-tracks', '45%', '12.5%', '12.5%', '17.5%', '12.5%', '0%','4%', '29%', '14%', '13%', '13%', '13%', '14%', '55%', '15%', '30%', '24%', '4%', '#DFE5F1', '#F7E9C5', '#E6F0F4', + + ]; + + if (blacklistValues.includes(key)) return true; +} + + + +const handleBlackListVariable= (key) => { + const blacklistValues = [ + 'padding2Px', 'borderRadiusPx','maxWidth', 'columnWidths', 'dispatch', 'emoji', 'scopesColWidth', + 'emissionsColWidth', 'tco2eColWidth', 'totalColWidth', 'columnSizes', 'roleColumnSizes', 'RulesListTableColumnSizes', + 'editImportsUrl', 'columnReportSizes', 'columnsWidth', 'InitiativesTableColumnSizes', 'ContributionTableColumnSizes', + 'SwSweepnitorAssessmentsList', 'color', 'dataTestIdRedirection' + ]; + + if (blacklistValues.includes(key)) return true; + if (blacklistValues.includes(key.name)) return true; +} const handleConditionalExpressions = (path) => { // For ternary operators @@ -34,21 +91,38 @@ const handleConditionalExpressions = (path) => { // Check for blacklist if (isBlacklistedForJsxAttribute(path)) return; + + const coreValue = _.get(path, 'node.value', '').trim(); if (!coreValue.length) return; + + if (handleBlackListValue(coreValue)) return const kValue = getUniqueKeyFromFreeText(coreValue); // TODO: OPTIMIZATION: Use quasi quotes to optimize this const srcString = `i18n.t(k.${kValue})`; + if (babel.types.isJSXAttribute(path.parent)) { // TODO: The next line does not parse // path.replaceWithSourceString(`{${srcString}}`); } else { + + path.replaceWithSourceString(srcString); } }; +const handleURLInlitterals = (value) => { + + if (value.startsWith('/')) return true +} + module.exports = { isBlacklistedForJsxAttribute, handleConditionalExpressions, + handleBlacklistedNode, + handleBlackListVariable, + handleBlackListKey, + handleBlackListValue, + handleURLInlitterals, }; diff --git a/src/plugin.js b/src/plugin.js index f924b17..2142fff 100644 --- a/src/plugin.js +++ b/src/plugin.js @@ -13,12 +13,19 @@ const { const { isBlacklistedForJsxAttribute, handleConditionalExpressions, + handleURLInlitterals, + handleBlackListKey, + handleBlackListVariable, + handleBlackListValue, } = require('./plugin-helpers'); const handleStringLiteral = (path, table, key) => { const { value } = path.node; if (!table[key]) table[key] = {}; if (!table[key].pairs) table[key].pairs = []; + if (handleURLInlitterals(value)) return + if (handleBlackListKey(key)) return; + if(handleBlackListValue(value)) return table[key].pairs.push({ path, value }); }; @@ -48,6 +55,7 @@ module.exports = ({ types: t }) => ({ this.state[key].pairs.forEach(({ path, value }) => { // TODO: OPTIMIZATION: Use quasi quotes to optimize this const kValue = getUniqueKeyFromFreeText(value); + path.replaceWithSourceString(`i18n.t(k.${kValue})`); }); } @@ -76,6 +84,7 @@ module.exports = ({ types: t }) => ({ }, Identifier: { enter(path) { + console.log("> Identifier") // Only extract the value of identifiers // who are children of some JSX element if (path.findParent(p => p.isJSXElement())) { @@ -85,6 +94,7 @@ module.exports = ({ types: t }) => ({ }, TemplateLiteral: { enter(path) { + console.log("> TemplateLiteral") // Only extract the value of identifiers // who are children of some JSX element const firstJsxParent = path.findParent(p => p.isJSXElement()); @@ -96,8 +106,11 @@ module.exports = ({ types: t }) => ({ if (isBlacklistedForJsxAttribute(path)) return; const { expressions, quasis } = path.node; + expressions.forEach((expression) => { + const key = expression.name; + this.state[key] = _.merge(this.state[key], { valid: true }); }); quasis.forEach((templateElement, index) => { @@ -107,6 +120,8 @@ module.exports = ({ types: t }) => ({ const kValue = getUniqueKeyFromFreeText(coreValue); // TODO: OPTIMIZATION: Use quasi quotes to optimize this // TODO: Replace the path instead of modifying the raw + + qPath.node.value.raw = qPath.node.value.raw.replace(coreValue, `\${i18n.t(k.${kValue})}`); qPath.node.value.cooked = qPath.node.value.cooked.replace(coreValue, `\${i18n.t(k.${kValue})}`); } @@ -115,37 +130,43 @@ module.exports = ({ types: t }) => ({ }, AssignmentExpression: { enter(path) { + console.log("> AssignmentExpression") // TODO: Explore the reason behind crash const key = _.get(path, 'node.left.name', _.get(path, 'node.left.property.name')); if (!key) return; + + if (handleBlackListVariable(key)) return; extractValueAndUpdateTable(t, this.state, path.get('right'), key); }, }, ObjectProperty: { enter(path) { + console.log("> ObjectProperty") const key = _.get(path, 'node.key.name'); if (!key) return; // Check for blacklist if (isBlacklistedForJsxAttribute(path)) return; - + if(handleBlackListKey(key)) return extractValueAndUpdateTable(t, this.state, path.get('value'), key); }, }, VariableDeclarator: { enter(path) { + console.log("> VariableDeclarator") // TODO: Explore the reason behind crash const key = _.get(path, 'node.id.name'); if (!key) return; // Check for blacklist if (isBlacklistedForJsxAttribute(path)) return; - + if (handleBlackListVariable(key)) return; extractValueAndUpdateTable(t, this.state, path.get('init'), key); }, }, JSXText: { enter(path) { + console.log("> JSXText") const coreValue = _.get(path, 'node.value', '').trim(); if (!coreValue.length) return; const kValue = getUniqueKeyFromFreeText(coreValue); @@ -155,6 +176,8 @@ module.exports = ({ types: t }) => ({ }, StringLiteral: { enter(path) { + console.log("> StringLiteral") + handleConditionalExpressions(path); }, },