-
Notifications
You must be signed in to change notification settings - Fork 149
add --rename option to rename declarations and references #47
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cb7fd4b
add --rename option to rename declarations and references
frapontillo 2c189c4
Use let instead of var, fix build
frapontillo b330a25
Use let instead of var, fix build
frapontillo e1efabd
Merge branch 'rename' of github.com:frapontillo/ng-annotate into rename
frapontillo b6f2940
Merge remote-tracking branch 'upstream/master' into rename
frapontillo 98a6a0a
Add rename tests, transform remaining let to const
frapontillo e29ef7a
Add rename tests, fix test on Windows
smrq 8bed6d1
Merge remote-tracking branch 'upstream/master' into rename
frapontillo 9625520
use stringmap for rename map, separate rename tests
frapontillo a77e91c
fix: rename provider names
frapontillo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
"use strict"; | ||
|
||
const os = require("os"); | ||
const is = require("simple-is"); | ||
const fmt = require("simple-fmt"); | ||
|
||
module.exports = { | ||
init: ngInjectCommentsInit, | ||
}; | ||
|
||
function ngInjectCommentsInit(ctx) { | ||
const comments = ctx.comments; | ||
const triggers = []; | ||
for (let i = 0; i < comments.length; i++) { | ||
const comment = comments[i]; | ||
const pos = comment.value.indexOf("@ngInject"); | ||
if (pos >= 0) { | ||
triggers.push({ | ||
pos: comment.range[1], | ||
fn: visitNodeFollowingNgInjectComment, | ||
ctx: ctx, | ||
}); | ||
} | ||
} | ||
|
||
ctx.triggers.addMany(triggers); | ||
} | ||
|
||
function nestedObjectValues(node, res) { | ||
res = res || []; | ||
|
||
node.properties.forEach(function(prop) { | ||
const v = prop.value; | ||
if (is.someof(v.type, ["FunctionExpression", "ArrayExpression"])) { | ||
res.push(v); | ||
} else if (v.type === "ObjectExpression") { | ||
nestedObjectValues(v, res); | ||
} | ||
}); | ||
|
||
return res; | ||
} | ||
|
||
function visitNodeFollowingNgInjectComment(node, ctx) { | ||
// handle most common case: /*@ngInject*/ prepended to an array or function expression | ||
// (or call expression, in case of IIFE jumping) | ||
if (node.type === "ArrayExpression" || node.type === "FunctionExpression" || node.type === "CallExpression") { | ||
ctx.addModuleContextIndependentSuspect(node, ctx); | ||
return; | ||
} | ||
|
||
if (node.type === "ObjectExpression") { | ||
nestedObjectValues(node).forEach(function(n) { | ||
ctx.addModuleContextIndependentSuspect(n, ctx); | ||
}); | ||
return; | ||
} | ||
|
||
// /*@ngInject*/ var foo = function($scope) {} and | ||
// /*@ngInject*/ function foo($scope) {} | ||
let d0 = null; | ||
const nr1 = node.range[1]; | ||
if (node.type === "VariableDeclaration" && node.declarations.length === 1 && | ||
(d0 = node.declarations[0]).init && ctx.isFunctionExpressionWithArgs(d0.init)) { | ||
const isSemicolonTerminated = (ctx.src[nr1 - 1] === ";"); | ||
addRemoveInjectArray(d0.init.params, isSemicolonTerminated ? nr1 : d0.init.range[1], d0.id.name); | ||
} else if (ctx.isFunctionDeclarationWithArgs(node)) { | ||
addRemoveInjectArray(node.params, nr1, node.id.name); | ||
} else if (node.type === "ExpressionStatement" && node.expression.type === "AssignmentExpression" && | ||
ctx.isFunctionExpressionWithArgs(node.expression.right)) { | ||
const isSemicolonTerminated = (ctx.src[nr1 - 1] === ";"); | ||
const name = ctx.srcForRange(node.expression.left.range); | ||
addRemoveInjectArray(node.expression.right.params, isSemicolonTerminated ? nr1 : node.expression.right.range[1], name); | ||
} | ||
|
||
function getIndent(pos) { | ||
const src = ctx.src; | ||
const lineStart = src.lastIndexOf("\n", pos - 1) + 1; | ||
let i = lineStart; | ||
for (; src[i] === " " || src[i] === "\t"; i++) { | ||
} | ||
return src.slice(lineStart, i); | ||
} | ||
|
||
function addRemoveInjectArray(params, posAfterFunctionDeclaration, name) { | ||
const indent = getIndent(posAfterFunctionDeclaration); | ||
const str = fmt("{0}{1}{2}.$inject = {3};", os.EOL, indent, name, ctx.stringify(ctx, params, ctx.quot)); | ||
|
||
ctx.triggers.add({ | ||
pos: posAfterFunctionDeclaration, | ||
fn: visitNodeFollowingFunctionDeclaration, | ||
}); | ||
|
||
function visitNodeFollowingFunctionDeclaration(nextNode) { | ||
const assignment = nextNode.expression; | ||
let lvalue; | ||
const hasInjectArray = (nextNode.type === "ExpressionStatement" && assignment.type === "AssignmentExpression" && | ||
assignment.operator === "=" && | ||
(lvalue = assignment.left).type === "MemberExpression" && | ||
lvalue.computed === false && ctx.srcForRange(lvalue.object.range) === name && lvalue.property.name === "$inject"); | ||
|
||
if (ctx.mode === "rebuild" && hasInjectArray) { | ||
ctx.fragments.push({ | ||
start: posAfterFunctionDeclaration, | ||
end: nextNode.range[1], | ||
str: str, | ||
}); | ||
} else if (ctx.mode === "remove" && hasInjectArray) { | ||
ctx.fragments.push({ | ||
start: posAfterFunctionDeclaration, | ||
end: nextNode.range[1], | ||
str: "", | ||
}); | ||
} else if (is.someof(ctx.mode, ["add", "rebuild"]) && !hasInjectArray) { | ||
ctx.fragments.push({ | ||
start: posAfterFunctionDeclaration, | ||
end: posAfterFunctionDeclaration, | ||
str: str, | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main difference is here, where I return another target, besides the defined function, that is the given provider name.