Skip to content

fork 1 #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
74 changes: 74 additions & 0 deletions src/plugin-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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,
};
27 changes: 25 additions & 2 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
};

Expand Down Expand Up @@ -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})`);
});
}
Expand Down Expand Up @@ -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())) {
Expand All @@ -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());
Expand All @@ -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) => {
Expand All @@ -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})}`);
}
Expand All @@ -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);
Expand All @@ -155,6 +176,8 @@ module.exports = ({ types: t }) => ({
},
StringLiteral: {
enter(path) {
console.log("> StringLiteral")

handleConditionalExpressions(path);
},
},
Expand Down