-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
181 lines (181 loc) · 7.06 KB
/
index.js
File metadata and controls
181 lines (181 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/// <reference path="./typings/tsd.d.ts"/>
'use strict';
var through = require('through2');
var falafel = require('falafel');
function setDefaultValue(opts) {
if (typeof opts.ignore === 'undefined') {
opts.ignore = /^\$/;
}
if (typeof opts.moduleName === 'undefined') {
opts.moduleName = '<Must set moduleName>';
}
if (typeof opts.firstLowerCase === 'undefined') {
opts.firstLowerCase = true;
}
if (typeof opts.patterns === 'undefined') {
opts.patterns = [
{ pattern: /Controller$/, type: 'controller', firstLowerCase: false },
{ pattern: /Ctrl$/, type: 'controller', firstLowerCase: false },
{ pattern: /Service$/, type: 'service' },
{ pattern: /Manager$/, type: 'service' },
{ pattern: /ServiceDelegate$/, type: 'service' },
{ pattern: /Provider$/, type: 'provider', removePattern: true },
{ pattern: /Directive$/, type: 'directive', removePattern: true, firstLowerCase: true }
];
}
if (typeof opts.decoratorPatterns === 'undefined') {
opts.decoratorPatterns = [
{ pattern: /Controller$/, func: 'Controller', firstLowerCase: false },
{ pattern: /Service$/, func: 'Service' },
{ pattern: /Provider$/, func: 'Provider', removePattern: true },
{ pattern: /Directive$/, func: 'Directive', removePattern: true, firstLowerCase: true }
];
}
}
module.exports = function angularify(opts) {
opts = opts || {};
setDefaultValue(opts);
// creating a stream through which each file will pass
var stream = through.obj(function (file, enc, cb) {
if (file.isNull()) {
}
if (file.isBuffer()) {
var contents = file.contents.toString();
contents = transform(contents, opts);
file.contents = new Buffer(contents.toString());
}
this.push(file);
return cb();
});
// returning the file stream
return stream;
};
function transform(contents, opts) {
return falafel(contents, { tolerant: true }, function (node) {
findClassDeclaration(node, opts);
}).toString();
}
function findClassDeclaration(node, opts) {
var decls, decl;
if (node.type === 'VariableDeclaration' &&
(decls = node.declarations) && decls.length === 1 &&
(decl = decls[0]) && decl.init && decl.init.type === 'CallExpression') {
if (opts.ignore && decl.id.name.match(opts.ignore)) {
return;
}
if (!decl.init.callee.body) {
return;
}
// Check if this is an ES6 module pattern (not wrapped in internal module IIFE)
// ES6 modules have the constructor as the first statement in the body
var isES6Module = isES6ModulePattern(decl);
if (opts.decorator) {
if (isES6Module) {
// Skip the plugin's internal module decorator processing for ES6 modules
// The decorator pattern in this plugin is designed for internal modules
return;
}
var decorators = findDecorator(decl);
decorators.forEach(function (decorator) {
opts.decoratorPatterns.forEach(function (decoratorPattern) {
if (decorator === opts.decoratorModuleName + '.' + decoratorPattern.func) {
addAngularModule(node, decl, opts, decoratorPattern);
}
});
});
}
else {
opts.patterns.forEach(function (pattern) {
if (decl.id.name.match(pattern.pattern)) {
addAngularModule(node, decl, opts, pattern);
}
});
}
}
}
function isES6ModulePattern(decl) {
// ES6 modules have the class constructor directly in the body
// Internal modules wrap the class in another IIFE
var body = decl.init.callee.body;
if (!body || !body.body || body.body.length < 2) {
return false;
}
// In ES6 modules, the first statement is the constructor FunctionDeclaration
// and it has the same name as the variable
var firstStatement = body.body[0];
return firstStatement.type === 'FunctionDeclaration' &&
firstStatement.id &&
firstStatement.id.name &&
decl.id &&
decl.id.name &&
firstStatement.id.name === decl.id.name;
}
function findDecorator(decl) {
var body = decl.init.callee.body;
var decoratorBlock = body.body[body.body.length - 2];
var decorators = decoratorBlock.expression.right;
return decorators.arguments[0].elements.map(function (element) {
return element.source();
});
}
function addAngularModule(node, decl, opts, ptn) {
var moduleName = opts.moduleName, type = ptn.type, pattern = ptn.pattern, removePattern = ptn.removePattern;
var firstLowerCase = ptn.firstLowerCase;
if (typeof firstLowerCase === 'undefined') {
firstLowerCase = opts.firstLowerCase;
}
var constructor = decl.init.callee.body.body[0].type === 'FunctionDeclaration' ? decl.init.callee.body.body[0] : decl.init.callee.body.body[1];
var constructorParams = constructor.params.map(function (param) {
return '\'' + param.name + '\'';
});
var className = decl.id.name;
var conponentName = className;
if (removePattern) {
conponentName = decl.id.name.replace(pattern, '');
}
if (firstLowerCase) {
conponentName = conponentName.toLowerCase()[0] + conponentName.substring(1);
}
add$inject(decl.init.callee.body, className, conponentName, constructor, constructorParams);
function add$inject(body, className, componentName, constructor, constructorParams) {
if (!constructorParams) {
return;
}
var source = '/*<auto_generate>*/';
source += className + ".$inject = [" + constructorParams.join('\,') + "]; ";
source += className + ".$componentName = '" + componentName + "'";
source += '/*</auto_generate>*/';
constructor.update(constructor.source() + source);
}
if (opts.decoratorModuleName) {
return;
}
var source = '/*<auto_generate>*/';
if (type === 'directive') {
source += createModule();
}
else if (type === 'value') {
source += createModule();
}
else if (type === 'constant') {
source += createModule();
}
else {
source += functionModule();
}
source += '/*</auto_generate>*/';
node.update(node.source() + source);
function functionModule() {
var source = '';
source += "angular.module('" + moduleName + "')";
source += "." + type + "('" + conponentName + "'," + className + ");";
return source;
}
function createModule() {
constructorParams.push("function(){return new (Function.prototype.bind.apply(" + className + ",[null].concat(Array.prototype.slice.call(arguments))));}");
var source = '';
source += "angular.module('" + moduleName + "')";
source += "." + type + "('" + conponentName + "',[" + constructorParams.join('\,') + "]);";
return source;
}
}