Skip to content

Instantly share code, notes, and snippets.

@jjbubudi
Last active January 16, 2019 14:57
Show Gist options
  • Save jjbubudi/78a3967e325dfc4f2f5acbd8253cf686 to your computer and use it in GitHub Desktop.
Save jjbubudi/78a3967e325dfc4f2f5acbd8253cf686 to your computer and use it in GitHub Desktop.
function provideToExport(file, api, options) {
const j = api.jscodeshift;
const root = j(file.source);
return root
.find(j.ExpressionStatement, {
expression: {
type: "CallExpression",
callee: {
object: {
name: "goog"
},
property: {
name: "provide"
}
}
}
})
.replaceWith((path, index) => {
const className = path.value.expression.arguments[0].value.split(".").slice(-1)[0];
const declaration = j.variableDeclaration("var", [
j.variableDeclarator(j.identifier(className), null)
]);
return j.exportNamedDeclaration(declaration, [
j.exportSpecifier(j.identifier(className), j.identifier(className))
]);;
})
.toSource();
}
function hoistMethodDefs(file, api, options) {
const j = api.jscodeshift;
return j(file.source)
.find(j.MemberExpression, {
object: {
type: "Identifier",
name: "jspb"
}
})
.replaceWith((path, index) => {
return j.identifier(path.value.property.name);
})
.toSource();
}
function googRequireToEs6Import(file, api, options) {
const j = api.jscodeshift;
const root = j(file.source);
return root
.find(j.CallExpression, {
callee: {
object: {
name: "goog"
},
property: {
name: "require"
}
}
})
.forEach(path => {
const node = path.value;
const moduleName = node.arguments[0].value.split(".").slice(-1)[0];
root
.get()
.node.program.body.unshift(
j.importDeclaration(
[j.importSpecifier(j.identifier(moduleName))],
j.literal(`./${moduleName}`)
)
);
})
.remove()
.toSource();
}
function removeAsserts(file, api, options) {
const j = api.jscodeshift;
return j(file.source)
.find(j.CallExpression, {
callee: {
object: {
object: {
name: "goog"
},
property: {
name: "asserts"
}
}
}
})
.remove()
.toSource();
}
function removeAssertImports(file, api, options) {
const j = api.jscodeshift;
return j(file.source)
.find(j.CallExpression, {
callee: {
object: {
name: "goog"
},
property: {
name: "require"
}
},
arguments: [{ value: "goog.asserts" }]
})
.remove()
.toSource();
}
function removeForwardDeclare(file, api, options) {
const j = api.jscodeshift;
return j(file.source)
.find(j.CallExpression, {
callee: {
object: {
name: "goog"
},
property: {
name: "forwardDeclare"
}
}
})
.remove()
.toSource();
}
function removeDefine(file, api, options) {
const j = api.jscodeshift;
return j(file.source)
.find(j.CallExpression, {
callee: {
object: {
name: "goog"
},
property: {
name: "define"
}
}
})
.remove()
.toSource();
}
function assertToExpect(file, api, options) {
const j = api.jscodeshift;
return j(file.source)
.find(j.CallExpression, {
callee: {
name: "assertEquals"
}
})
.replaceWith((path, index) => {
const expected = path.value.arguments[0];
const actual = path.value.arguments[1];
return j.callExpression(
j.memberExpression(
j.callExpression(j.identifier("expect"), [actual]),
j.identifier("toEqual")
),
[expected]
);
})
.toSource();
}
function assertThrowToExpect(file, api, options) {
const j = api.jscodeshift;
return j(file.source)
.find(j.CallExpression, {
callee: {
name: "assertThrows"
}
})
.replaceWith((path, index) => {
const actual = path.value.arguments[0];
return j.callExpression(
j.memberExpression(
j.callExpression(j.identifier("expect"), [actual]),
j.identifier("toThrow")
),
[]
);
})
.toSource();
}
export default function transformer(file, api, options) {
const j = api.jscodeshift;
const fixes = [
removeAssertImports,
removeAsserts,
removeForwardDeclare,
removeDefine,
googRequireToEs6Import,
provideToExport,
hoistMethodDefs,
assertToExpect,
assertThrowToExpect
];
let src = file.source;
fixes.forEach(fix => {
const nextSrc = fix({ ...file, source: src }, api, options);
if (nextSrc) {
src = nextSrc;
}
});
return src;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment