Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'js-slang' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
test('with error in the code, should return correct line number in error', () => {
code = '// Prepend\n error';
state = generateDefaultState(workspaceLocation, { editorPrepend: '// Prepend' });
runInContext(code, context, { scheduler: 'preemptive', originalMaxExecTime: 1000 }).then(
result => (context = (result as Finished).context)
);
const errors = context.errors.map((error: SourceError) => {
const newError = cloneDeep(error);
newError.location.start.line = newError.location.start.line - 1;
newError.location.end.line = newError.location.end.line - 1;
return newError;
});
return expectSaga(evalCode, code, context, execTime, workspaceLocation, actionType)
.withState(state)
.call(runInContext, code, context, {
scheduler: 'preemptive',
originalMaxExecTime: execTime
})
import { mount, shallow } from 'enzyme';
import * as React from 'react';
import { ErrorSeverity, ErrorType, SourceError } from 'js-slang/dist/types';
import {
AutogradingResult,
ITestcase,
TestcaseTypes
} from '../../../../components/assessment/assessmentShape';
import Autograder, { AutograderProps } from '../Autograder';
const mockErrors: SourceError[] = [
{
type: ErrorType.RUNTIME,
severity: ErrorSeverity.ERROR,
location: { start: { line: 3, column: 11 }, end: { line: 3, column: 11 } },
explain() {
return `Name a not declared.`;
},
elaborate() {
return `Name a not declared.`;
}
}
];
// The five testcases have statuses: correct, (none), correct, incorrect and error
const mockPublicTestcases: ITestcase[] = [
{ program: `"string";`, score: 0, answer: `"string"`, result: `string` },
{ program: `fibonacci(2);`, score: 1, answer: `2` },
{ program: `fibonacci(3);`, score: 1, answer: `2`, result: 2 },
{ program: `fibonacci(4);`, score: 2, answer: `3`, result: 4 },
import { mount, shallow } from 'enzyme';
import * as React from 'react';
import { ErrorSeverity, ErrorType, SourceError } from 'js-slang/dist/types';
import {
AutogradingResult,
ITestcase,
TestcaseTypes
} from '../../../../components/assessment/assessmentShape';
import Autograder, { AutograderProps } from '../Autograder';
const mockErrors: SourceError[] = [
{
type: ErrorType.RUNTIME,
severity: ErrorSeverity.ERROR,
location: { start: { line: 3, column: 11 }, end: { line: 3, column: 11 } },
explain() {
return `Name a not declared.`;
},
elaborate() {
return `Name a not declared.`;
}
}
];
// The five testcases have statuses: correct, (none), correct, incorrect and error
const mockPublicTestcases: ITestcase[] = [
{ program: `"string";`, score: 0, answer: `"string"`, result: `string` },
{ program: `fibonacci(2);`, score: 1, answer: `2` },
{ program: `fibonacci(3);`, score: 1, answer: `2`, result: 2 },
yield put(actions.resetTestcase(workspaceLocation, index));
const { result, interrupted } = yield race({
result: call(runInContext, code, context, {
scheduler: 'preemptive',
originalMaxExecTime: execTime
}),
/**
* A BEGIN_INTERRUPT_EXECUTION signals the beginning of an interruption,
* i.e the trigger for the interpreter to interrupt execution.
*/
interrupted: take(actionTypes.BEGIN_INTERRUPT_EXECUTION)
});
if (interrupted) {
interrupt(context);
// Redundancy, added ensure that interruption results in an error.
context.errors.push(new InterruptedError(context.runtime.nodes[0]));
yield put(actions.endInterruptExecution(workspaceLocation));
yield call(showWarningMessage, `Execution of testcase ${index} aborted`, 750);
return;
}
/** result.status here is either 'error' or 'finished'; 'suspended' is not possible
* since debugger is presently disabled in assessment and grading environments
*/
if (result.status === 'error') {
yield put(actions.evalInterpreterError(context.errors, workspaceLocation));
yield put(actions.evalTestcaseFailure(context.errors, workspaceLocation, index));
} else if (result.status === 'finished') {
// Execution of the testcase is successful, i.e. no errors were raised
yield put(actions.evalInterpreterSuccess(result.value, workspaceLocation));
? call(resume, lastDebuggerResult)
: call(runInContext, code, context, {
scheduler: 'preemptive',
originalMaxExecTime: execTime,
useSubst: substActiveAndCorrectChapter
}),
/**
* A BEGIN_INTERRUPT_EXECUTION signals the beginning of an interruption,
* i.e the trigger for the interpreter to interrupt execution.
*/
interrupted: take(actionTypes.BEGIN_INTERRUPT_EXECUTION),
paused: take(actionTypes.BEGIN_DEBUG_PAUSE)
});
if (interrupted) {
interrupt(context);
/* Redundancy, added ensure that interruption results in an error. */
context.errors.push(new InterruptedError(context.runtime.nodes[0]));
yield put(actions.debuggerReset(workspaceLocation));
yield put(actions.endInterruptExecution(workspaceLocation));
yield call(showWarningMessage, 'Execution aborted', 750);
return;
}
if (paused) {
yield put(actions.endDebuggerPause(workspaceLocation));
lastDebuggerResult = manualToggleDebugger(context);
yield updateInspector(workspaceLocation);
yield call(showWarningMessage, 'Execution paused', 750);
return;
}
const { result, interrupted } = yield race({
result: call(runInContext, code, context, {
scheduler: 'preemptive',
originalMaxExecTime: execTime
}),
/**
* A BEGIN_INTERRUPT_EXECUTION signals the beginning of an interruption,
* i.e the trigger for the interpreter to interrupt execution.
*/
interrupted: take(actionTypes.BEGIN_INTERRUPT_EXECUTION)
});
if (interrupted) {
interrupt(context);
// Redundancy, added ensure that interruption results in an error.
context.errors.push(new InterruptedError(context.runtime.nodes[0]));
yield put(actions.endInterruptExecution(workspaceLocation));
yield call(showWarningMessage, `Execution of testcase ${index} aborted`, 750);
return;
}
/** result.status here is either 'error' or 'finished'; 'suspended' is not possible
* since debugger is presently disabled in assessment and grading environments
*/
if (result.status === 'error') {
yield put(actions.evalInterpreterError(context.errors, workspaceLocation));
yield put(actions.evalTestcaseFailure(context.errors, workspaceLocation, index));
} else if (result.status === 'finished') {
// Execution of the testcase is successful, i.e. no errors were raised
yield put(actions.evalInterpreterSuccess(result.value, workspaceLocation));
yield put(actions.evalTestcaseSuccess(result.value, workspaceLocation, index));
}
scheduler: 'preemptive',
originalMaxExecTime: execTime,
useSubst: substActiveAndCorrectChapter
}),
/**
* A BEGIN_INTERRUPT_EXECUTION signals the beginning of an interruption,
* i.e the trigger for the interpreter to interrupt execution.
*/
interrupted: take(actionTypes.BEGIN_INTERRUPT_EXECUTION),
paused: take(actionTypes.BEGIN_DEBUG_PAUSE)
});
if (interrupted) {
interrupt(context);
/* Redundancy, added ensure that interruption results in an error. */
context.errors.push(new InterruptedError(context.runtime.nodes[0]));
yield put(actions.debuggerReset(workspaceLocation));
yield put(actions.endInterruptExecution(workspaceLocation));
yield call(showWarningMessage, 'Execution aborted', 750);
return;
}
if (paused) {
yield put(actions.endDebuggerPause(workspaceLocation));
lastDebuggerResult = manualToggleDebugger(context);
yield updateInspector(workspaceLocation);
yield call(showWarningMessage, 'Execution paused', 750);
return;
}
if (actionType === actionTypes.EVAL_EDITOR) {
lastDebuggerResult = result;
paused: take(actionTypes.BEGIN_DEBUG_PAUSE)
});
if (interrupted) {
interrupt(context);
/* Redundancy, added ensure that interruption results in an error. */
context.errors.push(new InterruptedError(context.runtime.nodes[0]));
yield put(actions.debuggerReset(workspaceLocation));
yield put(actions.endInterruptExecution(workspaceLocation));
yield call(showWarningMessage, 'Execution aborted', 750);
return;
}
if (paused) {
yield put(actions.endDebuggerPause(workspaceLocation));
lastDebuggerResult = manualToggleDebugger(context);
yield updateInspector(workspaceLocation);
yield call(showWarningMessage, 'Execution paused', 750);
return;
}
if (actionType === actionTypes.EVAL_EDITOR) {
lastDebuggerResult = result;
}
yield updateInspector(workspaceLocation);
if (result.status !== 'suspended' && result.status !== 'finished') {
yield put(actions.evalInterpreterError(context.errors, workspaceLocation));
return;
} else if (result.status === 'suspended') {
yield put(actions.endDebuggerPause(workspaceLocation));
yield put(actions.evalInterpreterSuccess('Breakpoint hit!', workspaceLocation));
export function mockTypeError(): TypeError {
// Typecast to Node to fix estree-acorn compatability.
return new TypeError(parse('') as Node, '', '', '');
}
const renderResult = (value: any) => {
/** A class which is the output of the show() function */
const ShapeDrawn = (window as any).ShapeDrawn;
if (typeof ShapeDrawn !== 'undefined' && value instanceof ShapeDrawn) {
return ;
} else {
return stringify(value);
}
};