Skip to content

Commit abc23ee

Browse files
committed
refactor: move _cmd and run logic
Signed-off-by: Snehil Shah <[email protected]> --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- Signed-off-by: Snehil Shah <[email protected]>
1 parent 9cefae1 commit abc23ee

File tree

5 files changed

+147
-10
lines changed

5 files changed

+147
-10
lines changed

lib/node_modules/@stdlib/repl/base/ctor/README.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,116 @@ repl.close();
9494

9595
### Methods
9696

97+
#### REPL.prototype.isMultilineInput( input )
98+
99+
Checks if the given input is a possible multi-line command.
100+
101+
```javascript
102+
// Create a new REPL:
103+
var repl = new REPL();
104+
105+
// Check if an input is a possible multi-line command:
106+
var input = 'for( var i = 0; i < 10; i++ ) {';
107+
var out = repl.isMultilineInput( input );
108+
109+
// Close the REPL:
110+
repl.close();
111+
```
112+
113+
#### REPL.prototype.processCommand( code )
114+
115+
Processes a "raw" input command.
116+
117+
```javascript
118+
// Create a new REPL:
119+
var repl = new REPL();
120+
121+
// Process raw command:
122+
var code = 'var y = 10;';
123+
var out = repl.processCommand( code );
124+
125+
// Close the REPL:
126+
repl.close();
127+
```
128+
129+
#### REPL.prototype.compileCommand( cmd )
130+
131+
Compiles a command.
132+
133+
```javascript
134+
// Create a new REPL:
135+
var repl = new REPL();
136+
137+
// Process raw command:
138+
var code = 'var y = 10;';
139+
var cmd = repl.processCommand( code );
140+
141+
// Compile processed command:
142+
var out = repl.compileCommand( cmd );
143+
144+
// Close the REPL:
145+
repl.close();
146+
```
147+
148+
#### REPL.prototype.runCommand( exe, opts )
149+
150+
Runs a compiled command.
151+
152+
```javascript
153+
// Create a new REPL:
154+
var repl = new REPL();
155+
156+
// Process and compile a raw command:
157+
var cmd = '1 + 1';
158+
var code = repl.processCommand( cmd );
159+
var exe = repl.compileCommand( code );
160+
161+
// Run the compiled command:
162+
var out = repl.runCommand( exe, {} );
163+
164+
// Close the REPL:
165+
repl.close();
166+
```
167+
168+
#### REPL.prototype.completer( line, clbk )
169+
170+
Generates completions for a given line.
171+
172+
```javascript
173+
// Create a new REPL:
174+
var repl = new REPL();
175+
176+
// Generate completions for a line:
177+
var line = 'var x = M';
178+
repl.completer( line, clbk );
179+
180+
function clbk( error, completions ) {
181+
if ( error ) {
182+
throw error;
183+
}
184+
console.log( completions );
185+
}
186+
187+
// Close the REPL:
188+
repl.close();
189+
```
190+
191+
#### REPL.prototype.tokenizer( line )
192+
193+
Tokenizes the input line based on ECMAScript specification.
194+
195+
```javascript
196+
// Create a new REPL:
197+
var repl = new REPL();
198+
199+
// Tokenize an input line:
200+
var line = 'var x = 5;';
201+
var out = repl.tokenizer( line );
202+
203+
// Close the REPL:
204+
repl.close();
205+
```
206+
97207
#### REPL.prototype.createContext()
98208

99209
Returns a REPL context.

lib/node_modules/@stdlib/repl/base/ctor/lib/completer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ function completer( repl ) {
147147
return clbk( null, [ res, line ] );
148148
}
149149
// Sanity check that we are attempting to complete something which is completable:
150-
if ( reservedCharsRegExp().test( line[ repl._rli.cursor-1 ] ) ) {
150+
if ( reservedCharsRegExp().test( line[ line.length-1 ] ) ) {
151151
debug( 'Detected attempt to trigger completion after a special character.' );
152152
debug( 'Results: %s', res.join( ', ' ) );
153153
return clbk( null, [ res, line ] );

lib/node_modules/@stdlib/repl/base/ctor/lib/main.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,6 @@ function REPL( options ) {
158158
// Initialize a strided internal buffer for storing the command history:
159159
setNonEnumerableReadOnly( this, '_history', [] );
160160

161-
// Initialize an internal buffer for storing the current command:
162-
setNonEnumerableReadOnly( this, '_cmd', [] );
163-
164161
// Initialize a executed command counter:
165162
setNonEnumerable( this, '_count', -1 );
166163

@@ -356,7 +353,6 @@ setNonEnumerableReadOnly( REPL.prototype, 'processCommand', function process( co
356353
/**
357354
* Compiles a command.
358355
*
359-
* @private
360356
* @name compileCommand
361357
* @memberof REPL.prototype
362358
* @type {Function}
@@ -384,6 +380,38 @@ setNonEnumerableReadOnly( REPL.prototype, 'compileCommand', function compile( cm
384380
return compileCommand( cmd );
385381
});
386382

383+
/**
384+
* Runs a compiled command.
385+
*
386+
* @name runCommand
387+
* @memberof REPL.prototype
388+
* @type {Function}
389+
* @param {Object} exe - compiled executable
390+
* @param {Object} opts - run options
391+
* @returns {*} command result
392+
*
393+
* @example
394+
* // Create a new REPL:
395+
* var repl = new REPL();
396+
*
397+
* // Process and compile a raw command:
398+
* var cmd = '1 + 1';
399+
* var code = repl.processCommand( cmd );
400+
* var exe = repl.compileCommand( code );
401+
*
402+
* // Run the compiled command:
403+
* var out = repl.runCommand( exe, {} );
404+
*
405+
* // Close the REPL:
406+
* repl.close();
407+
*/
408+
setNonEnumerableReadOnly( REPL.prototype, 'runCommand', function run( exe, opts ) {
409+
if ( this._sandbox ) {
410+
return exe.compiled.runInContext( this._context, opts );
411+
}
412+
return exe.compiled.runInThisContext( opts );
413+
});
414+
387415
/**
388416
* Generates completions for a given line.
389417
*

lib/node_modules/@stdlib/repl/lib/eager_evaluator.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,7 @@ setNonEnumerableReadOnly( EagerEvaluator.prototype, 'onKeypress', function onKey
311311
return;
312312
}
313313
try {
314-
if ( this._repl._sandbox ) {
315-
res = executable.compiled.runInContext( this._repl._context, ROPTS );
316-
} else {
317-
res = executable.compiled.runInThisContext( ROPTS );
318-
}
314+
res = this._repl.runCommand( executable, ROPTS );
319315
} catch ( err ) {
320316
debug( 'Encountered an error when executing the command: %s', err.message );
321317
return;

lib/node_modules/@stdlib/repl/lib/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ function REPL( options ) {
206206
setNonEnumerableReadOnly( this._internal.presentation, 'cache', {} );
207207
setNonEnumerable( this._internal.presentation, 'counter', 0 );
208208

209+
// Initialize an internal buffer for storing the current command:
210+
setNonEnumerableReadOnly( this, '_cmd', [] );
211+
209212
// Initialize a strided internal buffer for storing user-defined documentation:
210213
setNonEnumerableReadOnly( this, '_userdocs', [] );
211214

0 commit comments

Comments
 (0)