From 0893e4a51f12a0ab3cddb515e77585b8ef2f6cf2 Mon Sep 17 00:00:00 2001 From: Guillaume Guine Date: Tue, 23 Aug 2022 14:35:01 +0200 Subject: [PATCH 1/5] fork 1 --- src/plugin-helpers.js | 56 +++++++++++++++++++++++++++++++++++++++++++ src/plugin.js | 16 ++++++++++--- 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/src/plugin-helpers.js b/src/plugin-helpers.js index 4db0a91..bb5f943 100644 --- a/src/plugin-helpers.js +++ b/src/plugin-helpers.js @@ -14,6 +14,14 @@ 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' + ]; const jsxAttributeParent = path.findParent(p => p.isJSXAttribute()); if (!jsxAttributeParent) return false; @@ -22,6 +30,45 @@ 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' + + ]; + + if (blacklistValue.includes(key)) return true; +} + +const handleBlackListValue = (key) => { + const blacklistValue = [ + '/app/measure?show=ghg-protocol-upload' + + ]; + + if (blacklistValue.includes(key)) return true; +} + + + +const handleBlackListVariable= (key) => { + const blacklistValues = [ + 'padding2Px', 'borderRadiusPx','maxWidth', 'columnWidths', 'dispatch' + ]; + + if (blacklistValues.includes(key)) return true; + if (blacklistValues.includes(key.name)) return true; +} const handleConditionalExpressions = (path) => { // For ternary operators @@ -48,7 +95,16 @@ const handleConditionalExpressions = (path) => { } }; +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..6bf8cdf 100644 --- a/src/plugin.js +++ b/src/plugin.js @@ -13,16 +13,24 @@ const { const { isBlacklistedForJsxAttribute, handleConditionalExpressions, + handleBlacklistedNode, + handleBlackListVariable, + handleBlackListKey, + 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(handleBlackListVariable(path)) return + if(handleBlackListValue(value)) return + table[key].pairs.push({ path, value }); }; const extractValueAndUpdateTable = (t, table, path, key) => { + if(handleBlackListKey(key)) return if (t.isStringLiteral(path.node)) { handleStringLiteral(path, table, key); } else if (t.isArrayExpression(path.node)) { @@ -43,7 +51,7 @@ module.exports = ({ types: t }) => ({ LutManager.resetGetUniqueKeyFromFreeTextNumCalls(); }, exit(programPath) { - Object.keys(this.state).forEach((key) => { + Object.keys(this.state).forEach((key) => {w if (this.state[key].valid && this.state[key].pairs) { this.state[key].pairs.forEach(({ path, value }) => { // TODO: OPTIMIZATION: Use quasi quotes to optimize this @@ -118,6 +126,7 @@ module.exports = ({ types: t }) => ({ // 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); }, }, @@ -137,10 +146,10 @@ module.exports = ({ types: t }) => ({ // TODO: Explore the reason behind crash const key = _.get(path, 'node.id.name'); if (!key) return; - + //console.log() // Check for blacklist if (isBlacklistedForJsxAttribute(path)) return; - + if (handleBlackListVariable(key)) return; extractValueAndUpdateTable(t, this.state, path.get('init'), key); }, }, @@ -156,6 +165,7 @@ module.exports = ({ types: t }) => ({ StringLiteral: { enter(path) { handleConditionalExpressions(path); + handleBlacklistedNode(path) }, }, }, From b0b47dd3e30f2f32994afad5c180f4df7d0d5782 Mon Sep 17 00:00:00 2001 From: Guillaume Guine Date: Tue, 23 Aug 2022 17:10:14 +0200 Subject: [PATCH 2/5] progress --- .DS_Store | Bin 0 -> 6148 bytes src/plugin-helpers.js | 30 +++++++++++++++++++++++------- src/plugin.js | 24 ++++++++++++++---------- 3 files changed, 37 insertions(+), 17 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..e3bd5481b4d9f341c085a93babed4dd0230e78f5 GIT binary patch literal 6148 zcmeHK%}T>S5Z<-brW7Fug&r5Y7OYw;#Y>3w1&ruHr6#6mFlI}VnnNk%tS{t~_&m<+ zZop#BB6bFLzxmzGevtiPjB$4!9x!Gz#w=)v9F-bDcWr3HBqMShBbx=W48ZybW+wL6 z0l&S$GM2K3p!oj%ag^o#{wLpPG&i?eR?F&GcixjMyxh;{nd_%FXkAJf2bJyzSJAYX z*gI!3$^9sqrm7$cr;u`c9VMYGTscp|Ox1ciV70CG#O^K@!{JF!9FGReo>+_y`#mu@ z8ZMV@Yj)WEN4hGg;{tWaa}1UmaTN5c QazMHWC_< { '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' + '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' ]; const jsxAttributeParent = path.findParent(p => p.isJSXAttribute()); @@ -43,27 +48,30 @@ const handleBlacklistedNode = (path) => { const handleBlackListKey = (key) => { const blacklistValue = [ - 'kind' - + 'kind', 'hasPermission', 'InitiativesTableColumnSizes' ]; if (blacklistValue.includes(key)) return true; } const handleBlackListValue = (key) => { - const blacklistValue = [ - '/app/measure?show=ghg-protocol-upload' + const blacklistValues = [ + '/app/measure?show=ghg-protocol-upload', 'canEditTree', 'canEditOwnApiKey', 'canEditSegment', 'market_based', + '+99', 'edit-track', 'all-tracks', '45%', '12.5%', '12.5%', '17.5%', '12.5%', '0%', ]; - if (blacklistValue.includes(key)) return true; + if (blacklistValues.includes(key)) return true; } const handleBlackListVariable= (key) => { const blacklistValues = [ - 'padding2Px', 'borderRadiusPx','maxWidth', 'columnWidths', 'dispatch' + 'padding2Px', 'borderRadiusPx','maxWidth', 'columnWidths', 'dispatch', 'emoji', 'scopesColWidth', + 'emissionsColWidth', 'tco2eColWidth', 'totalColWidth', 'columnSizes', 'roleColumnSizes', 'RulesListTableColumnSizes', + 'editImportsUrl', 'columnReportSizes', 'columnsWidth', 'InitiativesTableColumnSizes', 'ContributionTableColumnSizes', + 'SwSweepnitorAssessmentsList', ]; if (blacklistValues.includes(key)) return true; @@ -81,21 +89,29 @@ 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 } diff --git a/src/plugin.js b/src/plugin.js index 6bf8cdf..32943d4 100644 --- a/src/plugin.js +++ b/src/plugin.js @@ -13,9 +13,9 @@ const { const { isBlacklistedForJsxAttribute, handleConditionalExpressions, - handleBlacklistedNode, - handleBlackListVariable, + handleURLInlitterals, handleBlackListKey, + handleBlackListVariable, handleBlackListValue, } = require('./plugin-helpers'); @@ -23,14 +23,13 @@ const handleStringLiteral = (path, table, key) => { const { value } = path.node; if (!table[key]) table[key] = {}; if (!table[key].pairs) table[key].pairs = []; - if(handleBlackListVariable(path)) return + if (handleURLInlitterals(value)) return + if (handleBlackListKey(key)); if(handleBlackListValue(value)) return - table[key].pairs.push({ path, value }); }; const extractValueAndUpdateTable = (t, table, path, key) => { - if(handleBlackListKey(key)) return if (t.isStringLiteral(path.node)) { handleStringLiteral(path, table, key); } else if (t.isArrayExpression(path.node)) { @@ -51,11 +50,14 @@ module.exports = ({ types: t }) => ({ LutManager.resetGetUniqueKeyFromFreeTextNumCalls(); }, exit(programPath) { - Object.keys(this.state).forEach((key) => {w + Object.keys(this.state).forEach((key) => { if (this.state[key].valid && this.state[key].pairs) { this.state[key].pairs.forEach(({ path, value }) => { // TODO: OPTIMIZATION: Use quasi quotes to optimize this const kValue = getUniqueKeyFromFreeText(value); + if (kValue ==='CANEDITTREE'){ + console.log(">>>>>>>>2") + } path.replaceWithSourceString(`i18n.t(k.${kValue})`); }); } @@ -104,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) => { @@ -115,6 +120,7 @@ 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})}`); } @@ -126,7 +132,6 @@ module.exports = ({ types: t }) => ({ // 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); }, }, @@ -137,7 +142,7 @@ module.exports = ({ types: t }) => ({ // Check for blacklist if (isBlacklistedForJsxAttribute(path)) return; - + if(handleBlackListKey(key)) return extractValueAndUpdateTable(t, this.state, path.get('value'), key); }, }, @@ -146,7 +151,7 @@ module.exports = ({ types: t }) => ({ // TODO: Explore the reason behind crash const key = _.get(path, 'node.id.name'); if (!key) return; - //console.log() + // Check for blacklist if (isBlacklistedForJsxAttribute(path)) return; if (handleBlackListVariable(key)) return; @@ -165,7 +170,6 @@ module.exports = ({ types: t }) => ({ StringLiteral: { enter(path) { handleConditionalExpressions(path); - handleBlacklistedNode(path) }, }, }, From dc3c1ab5995ad1099f88f0d31e7125b1e94a4c4e Mon Sep 17 00:00:00 2001 From: Guillaume Guine Date: Wed, 24 Aug 2022 10:20:48 +0200 Subject: [PATCH 3/5] more rules --- src/plugin-helpers.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/plugin-helpers.js b/src/plugin-helpers.js index af87a96..238fa5f 100644 --- a/src/plugin-helpers.js +++ b/src/plugin-helpers.js @@ -25,7 +25,9 @@ const isBlacklistedForJsxAttribute = (path) => { 'offsetforonboardingsurvey', 'mobile', 'headerpresent', 'footerpresent', 'nohoverunderlined', 'edition', 'underlined', 'keyDataTestId', 'margin', 'maxHeight', 'top', 'position', 'placement', 'align', 'textAlign', 'rootMargin', 'setAnswer', 'onValueChange', 'onChange', 'editImportsUrl','InputProps', - 'selectedCategory', 'setSelectedCategory', 'boxSizing' + 'selectedCategory', 'setSelectedCategory', 'boxSizing', 'p', 'size', 'mt', 'keyTestId', 'panelTitleVariant', + 'cursor', 'storageKey', 'emoj', 'selectedEmissionFactor', 'track', 'color', 'justifyContent', 'inputVariant', 'updatedData', + 'dataTestIdRedirection', '$height', ]; const jsxAttributeParent = path.findParent(p => p.isJSXAttribute()); @@ -56,8 +58,8 @@ const handleBlackListKey = (key) => { const handleBlackListValue = (key) => { const blacklistValues = [ - '/app/measure?show=ghg-protocol-upload', 'canEditTree', 'canEditOwnApiKey', 'canEditSegment', 'market_based', - '+99', 'edit-track', 'all-tracks', '45%', '12.5%', '12.5%', '17.5%', '12.5%', '0%', + '/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', ]; @@ -71,7 +73,7 @@ const handleBlackListVariable= (key) => { 'padding2Px', 'borderRadiusPx','maxWidth', 'columnWidths', 'dispatch', 'emoji', 'scopesColWidth', 'emissionsColWidth', 'tco2eColWidth', 'totalColWidth', 'columnSizes', 'roleColumnSizes', 'RulesListTableColumnSizes', 'editImportsUrl', 'columnReportSizes', 'columnsWidth', 'InitiativesTableColumnSizes', 'ContributionTableColumnSizes', - 'SwSweepnitorAssessmentsList', + 'SwSweepnitorAssessmentsList', 'color' ]; if (blacklistValues.includes(key)) return true; From c2d6f19c7c4d8568b92f65d30d529f147e37db12 Mon Sep 17 00:00:00 2001 From: Guillaume Guine Date: Fri, 26 Aug 2022 17:36:43 +0200 Subject: [PATCH 4/5] all --- src/plugin-helpers.js | 2 +- src/plugin.js | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/plugin-helpers.js b/src/plugin-helpers.js index 238fa5f..ee00369 100644 --- a/src/plugin-helpers.js +++ b/src/plugin-helpers.js @@ -27,7 +27,7 @@ const isBlacklistedForJsxAttribute = (path) => { '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', + 'dataTestIdRedirection', '$height', 'active' ]; const jsxAttributeParent = path.findParent(p => p.isJSXAttribute()); diff --git a/src/plugin.js b/src/plugin.js index 32943d4..7c7991b 100644 --- a/src/plugin.js +++ b/src/plugin.js @@ -24,9 +24,11 @@ const handleStringLiteral = (path, table, key) => { if (!table[key]) table[key] = {}; if (!table[key].pairs) table[key].pairs = []; if (handleURLInlitterals(value)) return - if (handleBlackListKey(key)); + if (handleBlackListKey(key)) return; if(handleBlackListValue(value)) return + console.log(table[key].pairs.length) table[key].pairs.push({ path, value }); + console.log(table[key].pairs.length) }; const extractValueAndUpdateTable = (t, table, path, key) => { From c5a719f9d2df13ceaa77a4736ed84cf6d37cd7eb Mon Sep 17 00:00:00 2001 From: Guillaume Guine Date: Mon, 29 Aug 2022 13:03:39 +0200 Subject: [PATCH 5/5] less fake --- src/plugin-helpers.js | 4 ++-- src/plugin.js | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/plugin-helpers.js b/src/plugin-helpers.js index ee00369..c5239d6 100644 --- a/src/plugin-helpers.js +++ b/src/plugin-helpers.js @@ -27,7 +27,7 @@ const isBlacklistedForJsxAttribute = (path) => { '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' + 'dataTestIdRedirection', '$height', 'active', 'bgColor', 'pl', 'direction', 'target', 'getProps' ]; const jsxAttributeParent = path.findParent(p => p.isJSXAttribute()); @@ -73,7 +73,7 @@ const handleBlackListVariable= (key) => { 'padding2Px', 'borderRadiusPx','maxWidth', 'columnWidths', 'dispatch', 'emoji', 'scopesColWidth', 'emissionsColWidth', 'tco2eColWidth', 'totalColWidth', 'columnSizes', 'roleColumnSizes', 'RulesListTableColumnSizes', 'editImportsUrl', 'columnReportSizes', 'columnsWidth', 'InitiativesTableColumnSizes', 'ContributionTableColumnSizes', - 'SwSweepnitorAssessmentsList', 'color' + 'SwSweepnitorAssessmentsList', 'color', 'dataTestIdRedirection' ]; if (blacklistValues.includes(key)) return true; diff --git a/src/plugin.js b/src/plugin.js index 7c7991b..2142fff 100644 --- a/src/plugin.js +++ b/src/plugin.js @@ -26,9 +26,7 @@ const handleStringLiteral = (path, table, key) => { if (handleURLInlitterals(value)) return if (handleBlackListKey(key)) return; if(handleBlackListValue(value)) return - console.log(table[key].pairs.length) table[key].pairs.push({ path, value }); - console.log(table[key].pairs.length) }; const extractValueAndUpdateTable = (t, table, path, key) => { @@ -57,9 +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); - if (kValue ==='CANEDITTREE'){ - console.log(">>>>>>>>2") - } + path.replaceWithSourceString(`i18n.t(k.${kValue})`); }); } @@ -88,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())) { @@ -97,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()); @@ -122,6 +120,7 @@ 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})}`); @@ -131,14 +130,18 @@ 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; @@ -150,6 +153,7 @@ module.exports = ({ types: t }) => ({ }, VariableDeclarator: { enter(path) { + console.log("> VariableDeclarator") // TODO: Explore the reason behind crash const key = _.get(path, 'node.id.name'); if (!key) return; @@ -162,6 +166,7 @@ module.exports = ({ types: t }) => ({ }, JSXText: { enter(path) { + console.log("> JSXText") const coreValue = _.get(path, 'node.value', '').trim(); if (!coreValue.length) return; const kValue = getUniqueKeyFromFreeText(coreValue); @@ -171,6 +176,8 @@ module.exports = ({ types: t }) => ({ }, StringLiteral: { enter(path) { + console.log("> StringLiteral") + handleConditionalExpressions(path); }, },