@@ -64,6 +64,7 @@ function EagerEvaluator( repl, rli, enabled ) {
6464 if ( ! ( this instanceof EagerEvaluator ) ) {
6565 return new EagerEvaluator ( repl , rli , enabled ) ;
6666 }
67+ debug ( 'Creating a new eager-evaluator...' ) ;
6768
6869 // Cache a reference to the provided REPL instance:
6970 this . _repl = repl ;
@@ -84,119 +85,28 @@ function EagerEvaluator( repl, rli, enabled ) {
8485}
8586
8687/**
87- * Callback for handling a "keypress" event.
88- *
89- * @name onKeyPress
90- * @memberof EagerEvaluator.prototype
91- * @type {Function }
92- * @param {string } data - input data
93- * @param {(Object|void) } key - key object
94- * @returns {void }
88+ * Checks if the code is side-effect-free.
9589*
96- */
97- setNonEnumerableReadOnly ( EagerEvaluator . prototype , 'onKeypress' , function onKeypress ( ) {
98- var cursorPosition ;
99- var executable ;
100- var nlInd ;
101- var code ;
102- var opts ;
103- var pre ;
104- var res ;
105- var tmp ;
106-
107- if ( ! this . _enabled || this . _rli . line === '' || this . _repl . _completerEngine . isNavigating ( ) ) {
108- return ;
109- }
110-
111- code = this . _cmd . join ( '\n' ) + this . _rli . line ;
112- opts = {
113- 'timeout' : this . _repl . _timeout ,
114- 'displayErrors' : false ,
115- 'breakOnSigint' : true // Node.js >=6.3.0
116- } ;
117- if ( ! this . isSideEffectFree ( code ) ) {
118- debug ( 'code have side effect' ) ;
119- return ;
120- }
121- debug ( 'try to process command' ) ;
122- tmp = processCommand ( code ) ;
123- if ( tmp instanceof Error ) {
124- debug ( 'getting error %s' , tmp . message ) ;
125- return ;
126- }
127- debug ( 'try to compile command' ) ;
128- executable = compileCommand ( tmp ) ;
129- if ( executable instanceof Error ) {
130- debug ( 'getting error %s' , executable . message ) ;
131- return ;
132- }
133- try {
134- if ( this . _repl . _sandbox ) {
135- res = executable . compiled . runInContext ( this . _repl . _context , opts ) ;
136- } else {
137- res = executable . compiled . runInThisContext ( opts ) ;
138- }
139- } catch ( err ) {
140- debug ( 'getting error when executing the command %s' , err . message ) ;
141- return ;
142- }
143-
144- res = inspect ( res ) ;
145- nlInd = res . indexOf ( '\n' ) ;
146- if ( nlInd !== - 1 ) {
147- res = res . slice ( 0 , nlInd ) + '...' ;
148- }
149- cursorPosition = this . _rli . cursor ;
150- pre = replace ( this . _repl . _outputPrompt , '%d' , ( this . _repl . _count + 1 ) . toString ( ) ) ;
151- this . _repl . _ostream . write ( '\n' + ANSI_GRAY + pre + res + ANSI_RESET ) ;
152- readline . moveCursor ( this . _repl . _ostream , 0 , - 1 ) ;
153- readline . cursorTo ( this . _repl . _ostream , cursorPosition + this . _repl . promptLength ( ) ) ;
154- this . _isPreview = true ;
155- debug ( 'sucess' ) ;
156- } ) ;
157-
158- /**
159- * Callback which should be invoked**before** a "keypress" event.
160- *
161- * @name beforeKeyPress
162- * @memberof EagerEvaluator.prototype
163- * @type {Function }
164- * @param {string } data - input data
165- * @param {(Object|void) } key - key object
166- * @returns {void }
167- *
168- */
169- setNonEnumerableReadOnly ( EagerEvaluator . prototype , 'beforeKeypress' , function beforeKeypress ( ) {
170- if ( ! this . _isPreview ) {
171- return ;
172- }
173- if ( this . _isPreview ) {
174- this . clear ( ) ;
175- }
176- } ) ;
177-
178- /**
179- * Function to determine if code is side-effect-free.
180- *
181- * @name isSideEffectFree
90+ * @private
91+ * @name _isSideEffectFree
18292* @memberof EagerEvaluator.prototype
18393* @type {Function }
18494* @param {string } code - input code
185- * @returns {boolean } - boolean indicate weather code is side effect free or not.
95+ * @returns {boolean } - boolean indicating whether the code is side- effect- free or not
18696*
18797*/
188- setNonEnumerableReadOnly ( EagerEvaluator . prototype , 'isSideEffectFree ' , function isSideEffectFree ( code ) {
98+ setNonEnumerableReadOnly ( EagerEvaluator . prototype , '_isSideEffectFree ' , function isSideEffectFree ( code ) {
18999 var ast ;
190100 var i ;
191101
192102 try {
193103 ast = parse ( code , AOPTS ) ;
194104 }
195105 catch ( err ) {
196- debug ( 'getting error when generating AST %S ' , err ) ;
106+ debug ( 'Encountered an error when generating AST: %s ' , err ) ;
197107 return false ;
198108 }
199- // Iterate from each node in body
109+ // Iterate from each node in the body:
200110 for ( i = 0 ; i < ast . body . length ; i ++ ) {
201111 if ( ! traverse ( ast . body [ i ] ) ) {
202112 return false ;
@@ -205,11 +115,11 @@ setNonEnumerableReadOnly( EagerEvaluator.prototype, 'isSideEffectFree', function
205115 return true ;
206116
207117 /**
208- * Function which recursivly traverse from the node and tells weather node is side effect free or not.
118+ * Recursively traverses the node to determine whether the node is side- effect- free or not.
209119 *
210120 * @private
211121 * @param {Object } node - ast node
212- * @returns {Boolean } - boolean indicating whether the node is side effect free or not
122+ * @returns {Boolean } - boolean indicating whether the node is side- effect= free or not
213123 */
214124 function traverse ( node ) {
215125 var fname ;
@@ -233,7 +143,7 @@ setNonEnumerableReadOnly( EagerEvaluator.prototype, 'isSideEffectFree', function
233143 else if ( node . type === 'CallExpression' ) {
234144 fname = getFunctionName ( node . callee ) ;
235145 if ( tempDB [ fname ] && tempDB [ fname ] . isPure ) {
236- // Iterating through arguments
146+ // Iterating through arguments:
237147 for ( i = 0 ; i < node . arguments . length ; i ++ ) {
238148 if ( ! traverse ( node . arguments [ i ] ) ) {
239149 return false ;
@@ -246,11 +156,11 @@ setNonEnumerableReadOnly( EagerEvaluator.prototype, 'isSideEffectFree', function
246156 }
247157
248158 /**
249- * Get the underscore seprate function name for the member function call .
159+ * Gets a function name representing identifier and member expression nodes .
250160 *
251161 * @private
252162 * @param {Object } node - ast node
253- * @returns {string } - underscore seprated function name for the member function call
163+ * @returns {string } - function name representing the node
254164 */
255165 function getFunctionName ( node ) {
256166 if ( ! node ) {
@@ -267,15 +177,16 @@ setNonEnumerableReadOnly( EagerEvaluator.prototype, 'isSideEffectFree', function
267177} ) ;
268178
269179/**
270- * Method to clear the eagerly evaluated text .
180+ * Clears eagerly- evaluated output .
271181*
182+ * @private
272183* @name clear
273184* @memberof EagerEvaluator.prototype
274185* @type {Function }
275186* @returns {void }
276187*
277188*/
278- setNonEnumerableReadOnly ( EagerEvaluator . prototype , 'clear' , function clear ( ) {
189+ setNonEnumerableReadOnly ( EagerEvaluator . prototype , 'clear' , function clear ( ) {
279190 var cursorPosition ;
280191
281192 cursorPosition = this . _rli . cursor ;
@@ -286,6 +197,100 @@ setNonEnumerableReadOnly( EagerEvaluator.prototype, 'clear', function clear( ) {
286197 this . _isPreview = false ;
287198} ) ;
288199
200+ /**
201+ * Callback which should be invoked **before** a "keypress" event.
202+ *
203+ * @private
204+ * @name beforeKeypress
205+ * @memberof EagerEvaluator.prototype
206+ * @type {Function }
207+ * @param {string } data - input data
208+ * @param {(Object|void) } key - key object
209+ * @returns {void }
210+ *
211+ */
212+ setNonEnumerableReadOnly ( EagerEvaluator . prototype , 'beforeKeypress' , function beforeKeypress ( ) {
213+ if ( ! this . _isPreview ) {
214+ return ;
215+ }
216+ if ( this . _isPreview ) {
217+ this . clear ( ) ;
218+ }
219+ } ) ;
220+
221+ /**
222+ * Callback for handling a "keypress" event.
223+ *
224+ * @private
225+ * @name onKeypress
226+ * @memberof EagerEvaluator.prototype
227+ * @type {Function }
228+ * @param {string } data - input data
229+ * @param {(Object|void) } key - key object
230+ * @returns {void }
231+ *
232+ */
233+ setNonEnumerableReadOnly ( EagerEvaluator . prototype , 'onKeypress' , function onKeypress ( ) {
234+ var cursorPosition ;
235+ var executable ;
236+ var nlInd ;
237+ var code ;
238+ var opts ;
239+ var pre ;
240+ var res ;
241+ var tmp ;
242+
243+ if ( ! this . _enabled || this . _rli . line === '' ) {
244+ return ;
245+ }
246+
247+ code = this . _cmd . join ( '\n' ) + this . _rli . line ;
248+ opts = {
249+ 'timeout' : this . _repl . _timeout ,
250+ 'displayErrors' : false ,
251+ 'breakOnSigint' : true // Node.js >=6.3.0
252+ } ;
253+ if ( ! this . _isSideEffectFree ( code ) ) {
254+ debug ( 'code have side effect' ) ;
255+ return ;
256+ }
257+ debug ( 'try to process command' ) ;
258+ tmp = processCommand ( code ) ;
259+ if ( tmp instanceof Error ) {
260+ debug ( 'getting error %s' , tmp . message ) ;
261+ return ;
262+ }
263+ debug ( 'try to compile command' ) ;
264+ executable = compileCommand ( tmp ) ;
265+ if ( executable instanceof Error ) {
266+ debug ( 'getting error %s' , executable . message ) ;
267+ return ;
268+ }
269+ try {
270+ if ( this . _repl . _sandbox ) {
271+ res = executable . compiled . runInContext ( this . _repl . _context , opts ) ;
272+ } else {
273+ res = executable . compiled . runInThisContext ( opts ) ;
274+ }
275+ } catch ( err ) {
276+ debug ( 'getting error when executing the command %s' , err . message ) ;
277+ return ;
278+ }
279+
280+ res = inspect ( res ) ;
281+ nlInd = res . indexOf ( '\n' ) ;
282+ if ( nlInd !== - 1 ) {
283+ res = res . slice ( 0 , nlInd ) + '...' ;
284+ }
285+ cursorPosition = this . _rli . cursor ;
286+ pre = replace ( this . _repl . _outputPrompt , '%d' , ( this . _repl . _count + 1 ) . toString ( ) ) ;
287+ this . _repl . _ostream . write ( '\n' + ANSI_GRAY + pre + res + ANSI_RESET ) ;
288+ readline . moveCursor ( this . _repl . _ostream , 0 , - 1 ) ;
289+ readline . cursorTo ( this . _repl . _ostream , cursorPosition + this . _repl . promptLength ( ) ) ;
290+ this . _isPreview = true ;
291+ debug ( 'sucess' ) ;
292+ } ) ;
293+
289294
290295// EXPORTS //
291296
0 commit comments