Skip to content
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

Framework: Add i18n-translate codemod #13597

Merged
merged 9 commits into from
May 10, 2017
Merged
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
42 changes: 42 additions & 0 deletions bin/codemods/i18n-mixin
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env node

/*
This codemod converts `this.translate` to `this.props.translate`, and wraps the corresponding
React.createClass instance with a `localize()` higher-order component.

How to use:
./bin/codemods/i18n-mixin path-to-transform/
*/

/**
* External dependencies
*/
const path = require( 'path' );
const child_process = require( 'child_process' );

/**
* Internal dependencies
*/
const config = require( './config' );
const helpers = require( './helpers' );

const args = process.argv.slice( 2 );
if ( args.length === 0 ) {
process.stdout.write( 'No files to transform\n' );
process.exit( 0 );
}

const binArgs = [
// jscodeshift options
'--transform=bin/codemods/i18n-mixin-to-localize.js',
...config.jscodeshiftArgs,

// Recast options via 5to6
...config.recastArgs,

// Transform target
args[ 0 ],
];
const binPath = path.join( '.', 'node_modules', '.bin', 'jscodeshift' );
const jscodeshift = child_process.spawn( binPath, binArgs );
helpers.bindEvents( jscodeshift );
143 changes: 143 additions & 0 deletions bin/codemods/i18n-mixin-to-localize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
export default function transformer(file, api) {
const j = api.jscodeshift;
const ReactUtils = require('react-codemod/transforms/utils/ReactUtils')(j);
const root = j(file.source);
let foundThisTranslate = false;

const createClassesInstances = ReactUtils.findAllReactCreateClassCalls( root );

// Find the declaration to wrap with the localize HOC. It can be the React.createClass
// itself, or an 'export default' or 'module.exports =' declaration, if present.
function findDeclarationsToWrap( createClassInstance ) {
// Is the created class being assigned to a variable?
const parentNode = createClassInstance.parentPath.value;
if (parentNode.type !== 'VariableDeclarator' || parentNode.id.type !== 'Identifier') {
return j(createClassInstance);
}

// AST matcher for the class identifier
const classIdentifier = {
type: 'Identifier',
name: parentNode.id.name,
};

// AST matcher for the connected class identifier
const connectedClassIdentifier = {
type: 'CallExpression',
callee: {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'connect',
}
},
arguments: [ classIdentifier ]
};

// AST matcher for the module.exports expression
const moduleExportsExpression = {
type: 'MemberExpression',
object: {
type: 'Identifier',
name: 'module'
},
property: {
type: 'Identifier',
name: 'exports',
}
};

// Is the variable later exported with 'export default'?
const exportDefaultDeclarations = root.find(j.ExportDefaultDeclaration, {
declaration: classIdentifier
});
if (exportDefaultDeclarations.size()) {
return exportDefaultDeclarations.map(d => d.get('declaration'));
}

// Is the variable later exported with 'export default connect()'?
const exportDefaultConnectDeclarations = root.find(j.ExportDefaultDeclaration, {
declaration: connectedClassIdentifier
});
if (exportDefaultConnectDeclarations.size()) {
return exportDefaultConnectDeclarations.map(d => d.get('declaration').get('arguments', 0));
}

// Is the variable later exported with 'module.exports ='?
const moduleExportsDeclarations = root.find(j.AssignmentExpression, {
left: moduleExportsExpression,
right: classIdentifier
});
if (moduleExportsDeclarations.size()) {
return moduleExportsDeclarations.map(d => d.get('right'));
}

// Is the variable later exported with 'module.exports = connect()'?
const moduleExportsConnectDeclarations = root.find(j.AssignmentExpression, {
left: moduleExportsExpression,
right: connectedClassIdentifier
});
if (moduleExportsConnectDeclarations.size()) {
return moduleExportsConnectDeclarations.map(d => d.get('right').get('arguments', 0));
}

return j(createClassInstance);
}

createClassesInstances.forEach( createClassInstance => {
const thisTranslateInstances = j(createClassInstance).find(j.MemberExpression, {
object: { type: 'ThisExpression'},
property: {
type: 'Identifier',
name: 'translate'
}
});
thisTranslateInstances.replaceWith( () => (
j.memberExpression(
j.memberExpression(
j.thisExpression(),
j.identifier('props')
),
j.identifier('translate')
)
) );
if (thisTranslateInstances.size()) {
foundThisTranslate = true;

const declarationsToWrap = findDeclarationsToWrap(createClassInstance);
declarationsToWrap.replaceWith( decl => {
return j.callExpression(
j.identifier('localize'),
[ decl.value ]
)
});
}
} );

if ( foundThisTranslate ) {
const i18nCalypsoImports = root.find(j.ImportDeclaration, {
source: { value: 'i18n-calypso' }
})
if ( i18nCalypsoImports.size() ) {
const i18nCalypsoImport = i18nCalypsoImports.get();
const localizeImport = j(i18nCalypsoImport).find(j.ImportSpecifier, {
local: {
type: 'Identifier',
name: 'localize'
}
});
if ( ! localizeImport.size() ) {
i18nCalypsoImport.value.specifiers.push( j.importSpecifier(
j.identifier('localize')
));
}
} else {
root.find(j.ImportDeclaration).at(0).insertAfter('import { localize } from \'i18n-calypso\';');
}
}

return root
.toSource({
useTabs: true
});
}
Loading