Skip to content

Commit

Permalink
refactor: universalize annotation injection.
Browse files Browse the repository at this point in the history
  • Loading branch information
黄健 authored and 黄健 committed Jun 22, 2017
1 parent 5cdedb0 commit 81497ea
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = function ({ types }) {
// skip when not export Function or Class
if (types.isFunctionDeclaration(path.node.declaration)) {
if (radar.determineAnnotationComment(comments)) {
injector.injectFunctionDeclare(path, types);
injector.injectFunctionDeclare(path.get('declaration'), types);
} else {
path.traverse(DeclarationVisitor, { types: types });
}
Expand All @@ -28,7 +28,7 @@ module.exports = function ({ types }) {

if (types.isClassDeclaration(path.node.declaration)) {
if (radar.determineAnnotationComment(comments)) {
injector.injectClassDeclare(path, types);
injector.injectClassDeclare(path.get('declaration'), types);
} else {
path.traverse(DeclarationVisitor, { types: types });
}
Expand Down
41 changes: 15 additions & 26 deletions src/injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,41 @@

module.exports = {
injectFunctionDeclare,
injectInlineFunctionDeclare,
injectClassDeclare,
injectInlineClassDeclare
injectClassDeclare
};

/**
* @description - inject Function DI annotation
*
* @param path
* @param types
*/
function injectFunctionDeclare(path, types) {
const { id, params } = path.node.declaration;

path.node.leadingComments = null;
path.insertBefore(types.expressionStatement(types.assignmentExpression(
'=',
types.MemberExpression(id, types.identifier('$inject')),
types.arrayExpression(params.map((identifier) => types.stringLiteral(identifier.name)))
)));
}

function injectInlineFunctionDeclare(path, types) {
const { id, params } = path.node;
const parent = path.findParent((path) => (types.isExportNamedDeclaration(path.node) || types.isExportDefaultDeclaration(path.node)));

path.node.leadingComments = null;
parent.node.leadingComments = null;
parent.insertBefore(types.expressionStatement(types.assignmentExpression(
'=',
types.MemberExpression(id, types.identifier('$inject')),
types.arrayExpression(params.map((identifier) => types.stringLiteral(identifier.name)))
)));
}

/**
* @description - inject Class DI annotation
*
* @param path
* @param types
*/
function injectClassDeclare(path, types) {
const declaration = path.node.declaration;
const params = declaration.body.body.find((ClassMethod) => ClassMethod.kind === 'constructor').params;

path.node.leadingComments = null;
path.insertBefore(types.expressionStatement(types.assignmentExpression(
'=',
types.MemberExpression(declaration.id, types.identifier('$inject')),
types.arrayExpression(params.map((identifier) => types.stringLiteral(identifier.name)))
)));
}

function injectInlineClassDeclare(path, types) {
const id = path.node.id;
const params = path.node.body.body.find((ClassMethod) => ClassMethod.kind === 'constructor').params;
const parent = path.findParent((path) => (types.isExportNamedDeclaration(path.node) || types.isExportDefaultDeclaration(path.node)));

path.node.leadingComments = null;
parent.node.leadingComments = null;
parent.insertBefore(types.expressionStatement(types.assignmentExpression(
'=',
types.MemberExpression(id, types.identifier('$inject')),
Expand Down
12 changes: 6 additions & 6 deletions src/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ const radar = require('./radar');
const injector = require('./injector');

module.exports = {
ClassDeclaration: {
FunctionDeclaration: {
enter(path) {
if (radar.determineAnnotationComment(radar.inspectClassAnnotationComment(path))) {
injector.injectInlineClassDeclare(path, this.types);
if (radar.determineAnnotationComment(radar.inspectFunctionAnnotationComment(path))) {
injector.injectFunctionDeclare(path, this.types);
}
}
},
FunctionDeclaration: {
ClassDeclaration: {
enter(path) {
if (radar.determineAnnotationComment(radar.inspectFunctionAnnotationComment(path))) {
injector.injectInlineFunctionDeclare(path, this.types);
if (radar.determineAnnotationComment(radar.inspectClassAnnotationComment(path))) {
injector.injectClassDeclare(path, this.types);
}
}
}
Expand Down

0 comments on commit 81497ea

Please sign in to comment.