-
Notifications
You must be signed in to change notification settings - Fork 1
/
operationalyzer.js
executable file
·140 lines (115 loc) · 3.61 KB
/
operationalyzer.js
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
// through2 is a thin wrapper around node transform streams
var through = require('through2'),
gutil = require('gulp-util'),
esprima = require('esprima'),
escodegen = require('escodegen'),
estraverse = require('estraverse'),
_ = require('lodash'),
PluginError = gutil.PluginError;
// consts
const PLUGIN_NAME = 'gulp-operationalyzer';
function isOperation( node, parent ) {
return node && parent &&
node.type !== 'ObjectExpression' &&
node.type !== 'ArrayExpression' &&
node.type !== 'Literal' &&
( node.type !== 'UnaryExpression' || node.argument.type === 'Identifier' ) &&
( parent.type === 'Property' || parent.type === 'ArrayExpression' );
}
function unique(e, i, arr) {
return arr.lastIndexOf(e) === i;
}
// plugin level function (dealing with files)
function operationalyzer() {
// prepare a generic operation object that we'll reuse to replace each operation
var operation = esprima.parse('_ = {_operation: \'\', _parameters: [], _dependencies: []}')
.body[0].expression.right.properties;
// creating a stream through which each file will pass
var stream = through.obj(function(file, enc, cb) {
var ast = esprima.parse( file.contents.toString() ),
inOperation,
inMember,
toLiteralize = [],
parameters,
dependencies,
updatedAst;
updatedAst = estraverse.replace( ast, {
enter: function( node, parent ) {
// make all object keys strings
// (otherwise in `{ y: y }`, both `y`s are undistinguishable)
if ( node.type === 'Property' && node.key.type === 'Identifier' ) {
node.key.type = 'Literal';
node.key.value = node.key.name;
node.key.raw = '\'' + node.key.name + '\'';
return node;
}
if ( !inOperation && isOperation( node, parent ) ) {
inOperation = true;
toLiteralize.push( node );
parameters = [];
dependencies = [];
}
if ( inOperation ) {
if ( inMember ) {
return;
}
if ( node.type === 'MemberExpression' ) {
inMember = node;
if ( node.object.name !== 'Math' && node.object.name !== 'Utils' ) {
dependencies.push( escodegen.generate( node ) );
}
return;
}
if ( node.type === 'Identifier' && node.name !== 'Array' && node.name !== 'Object' ) {
parameters.push( node.name );
}
}
},
leave: function( node ) {
if ( node === inMember ) {
inMember = false;
}
if ( toLiteralize.indexOf( node ) !== -1 ) {
inOperation = false;
toLiteralize.splice( toLiteralize.indexOf( node ), 1 );
var tmp = escodegen.generate( node );
node.type = 'ObjectExpression';
node.properties = _.cloneDeep( operation );
node.properties[0].value.value = tmp;
node.properties[0].value.raw = '\'' + node.properties[0].value + '\'';
node.properties[1].value.elements =
parameters
.map(function( param ) {
return {
type: 'Literal',
value: param,
raw: '\'' + param + '\''
};
})
.filter(unique);
node.properties[2].value.elements =
dependencies
.map(function( dep ) {
// transform contours[0].nodes[1].x into contours.0.nodes.1.x
// which is invalid javascript but easier to use
dep = dep.replace( /\[\s*(\d+)\s*\]/g, '.$1' );
return {
type: 'Literal',
value: dep,
raw: '\'' + dep + '\''
};
})
.filter(unique);
return node;
}
}
});
file.contents = new Buffer( escodegen.generate( updatedAst ) );
this.push(file);
return cb();
});
// returning the file stream
return stream;
}
// exporting the plugin main function
module.exports = operationalyzer;