Skip to content

Commit

Permalink
Use for-of in place of .forEach() iteration.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjeffburke committed Jan 1, 2020
1 parent e7a0589 commit 77e297c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
5 changes: 3 additions & 2 deletions lib/Snippets.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class Snippets {
getTests() {
var tests = [];
var evaluatedExampleIndex;
this.items.forEach(function(snippet, index) {

for (const [index, snippet] of this.items.entries()) {
var flags = snippet.flags;

switch (snippet.lang) {
Expand All @@ -36,7 +37,7 @@ class Snippets {
}
break;
}
});
}

return tests;
}
Expand Down
22 changes: 13 additions & 9 deletions lib/convertMarkdownToMocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function findCodeBlocks(mdSrc) {
function compileCodeBlocksToAst(codeBlocks, fileName) {
var top = [];
var cursor = top;
codeBlocks.forEach(function(codeBlock, i) {
for (const [i, codeBlock] of codeBlocks.entries()) {
var previousExampleNumber = i + 1;
var topLevelStatements = esprima.parse(
`${new Array(codeBlock.lineNumber).join('\n')}(function () {\n${
Expand All @@ -150,7 +150,10 @@ function compileCodeBlocksToAst(codeBlocks, fileName) {
{ loc: true, source: pathModule.resolve(fileName) }
).body[0].expression.callee.body.body;

topLevelStatements.forEach(function(topLevelStatement, i) {
for (const [
statementIndex,
topLevelStatement
] of topLevelStatements.entries()) {
switch (topLevelStatement.type) {
case 'VariableDeclaration':
topLevelStatement.kind = 'var';
Expand All @@ -169,10 +172,10 @@ function compileCodeBlocksToAst(codeBlocks, fileName) {
],
kind: 'var'
};
topLevelStatements[i] = newStatement;
topLevelStatements[statementIndex] = newStatement;
break;
}
});
}

if (codeBlock.flags.freshExpect) {
Array.prototype.push.apply(
Expand Down Expand Up @@ -242,7 +245,7 @@ function compileCodeBlocksToAst(codeBlocks, fileName) {
}
Array.prototype.push.apply(cursor, check);
}
});
}
return top;
}

Expand Down Expand Up @@ -297,8 +300,9 @@ module.exports = function(mdSrc, fileName) {
);

preamble = esprima.parse(transpiledBlocks[0]);
const remainingBlocks = transpiledBlocks.slice(1);

transpiledBlocks.slice(1).forEach(function(transpiledBlock, i) {
for (const [i, transpiledBlock] of remainingBlocks.entries()) {
if (codeBlocks[i].flags.async) {
codeBlocks[i].code = transpiledBlock
.replace(/^\(function unexpectedMarkdownScope\(\) \{\n/, '')
Expand All @@ -307,10 +311,10 @@ module.exports = function(mdSrc, fileName) {
} else {
codeBlocks[i].code = transpiledBlock;
}
});
}
}

codeBlocks.forEach(function(codeBlock, i) {
for (const [i, codeBlock] of codeBlocks.entries()) {
var exampleNumber = i + 1;
/* eslint-disable */
var itExpressionStatement = parseFunctionBody(function f() {
Expand All @@ -336,6 +340,6 @@ module.exports = function(mdSrc, fileName) {
itExpressionStatement.expression.arguments[1].body.body,
compileCodeBlocksToAst(codeBlocks.slice(0, i + 1), fileName)
);
});
}
return escodegen.generate(ast, { sourceMap: true, sourceMapWithCode: true });
};
14 changes: 8 additions & 6 deletions lib/evaluateSnippets.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ try {
function extend(target) {
for (var i = 1; i < arguments.length; i += 1) {
var source = arguments[i];
Object.keys(source).forEach(function(key) {
for (const key of Object.keys(source)) {
target[key] = source[key];
});
}
}
return target;
}
Expand Down Expand Up @@ -96,11 +96,13 @@ module.exports = async function(snippets, options) {
);

var preamble = transpiledSnippets[0];
const remainingSnippets = transpiledSnippets.slice(1);

vm.runInThisContext(preamble);

transpiledSnippets.slice(1).forEach(function(transpiledSnippet, i) {
for (const [i, transpiledSnippet] of remainingSnippets.entries()) {
exampleSnippets[i].code = transpiledSnippet;
});
}
}
}

Expand Down Expand Up @@ -141,11 +143,11 @@ module.exports = async function(snippets, options) {
}
}

Object.keys(global).forEach(function(key) {
for (const key of Object.keys(global)) {
if (!(key in oldGlobal)) {
delete global[key];
}
});
}

// local function used for compatibility with node 8
extend(global, oldGlobal);
Expand Down
4 changes: 2 additions & 2 deletions lib/extractSnippets.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ var snippetRegexp = require('./snippetRegexp');

function parseFlags(flagsString) {
var flags = {};
flagsString.split(/,/).forEach(function(flagString) {
for (const flagString of flagsString.split(/,/)) {
var m = /(\w+)\s*:\s*(\w+)/.exec(flagString.trim());
flags[m[1]] = m[2] === 'true';
});
}
return flags;
}

Expand Down

0 comments on commit 77e297c

Please sign in to comment.