Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 22 additions & 39 deletions javascript/processingFunctions/processSnippetJs.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,52 +83,35 @@ export const processSnippetJs = (node, writeTo, fileFormat) => {
jsRunSnippet = jsSnippet;
}
}

const codeArr_run = [];
recursiveProcessPureText(jsRunSnippet.firstChild, codeArr_run);
const codeStr_run = codeArr_run.join("").trim();

let reqStr = "";
let reqArr = [];
const reqSet = new Set();
const snippetName = node.getElementsByTagName("NAME")[0];
let nameStr;
if (snippetName) {
nameStr = snippetName.firstChild.nodeValue;
const reqSet = new Set();
recursiveGetRequires(nameStr, reqSet);
const examples = node.getElementsByTagName("EXAMPLE");
for (let i = 0; examples[i]; i++) {
const exampleString = examples[i].firstChild.nodeValue;
const exampleNode = snippetStore[exampleString];
if (exampleNode) {
const exampleRequires = exampleNode.requireNames;
for (let j = 0; exampleRequires[j]; j++) {
recursiveGetRequires(exampleRequires[j], reqSet);
}
}
}
for (const reqName of reqSet) {
const snippetEntry = snippetStore[reqName];
if (snippetEntry && reqName !== nameStr) {
reqArr.push(snippetEntry.codeStr);
reqArr.push("\n");
}
}
reqStr = reqArr.join("");
} else {
const requirements = node.getElementsByTagName("REQUIRES");
for (let i = 0; requirements[i]; i++) {
const required = requirements[i].firstChild.nodeValue;
if (snippetStore[required]) {
reqArr.push(snippetStore[required].codeStr);
reqArr.push("\n");
} else {
missingRequireWarning(required);
}
}
reqStr = reqArr.join("");
recursiveGetRequires(snippetName.firstChild.nodeValue, reqSet);
}
const requirements = node.getElementsByTagName("REQUIRES");
for (let i = 0; requirements[i]; i++) {
recursiveGetRequires(requirements[i].firstChild.nodeValue, reqSet);
}
const examples = node.getElementsByTagName("EXAMPLE");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The const variable examples is declared here, but it is re-declared on line 115. This will cause a SyntaxError: Identifier 'examples' has already been declared'. Please declare this variable only once and reuse it for both loops that iterate over the examples.

for (let i = 0; examples[i]; i++) {
const exampleName = examples[i].firstChild.nodeValue;
recursiveGetRequires(exampleName, reqSet);
}
Comment on lines +95 to 103
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

These C-style loops can be simplified by using for...of loops, which are more idiomatic in modern JavaScript and improve readability.

    const requirements = node.getElementsByTagName("REQUIRES");
    for (const requirement of requirements) {
      recursiveGetRequires(requirement.firstChild.nodeValue, reqSet);
    }
    const examples = node.getElementsByTagName("EXAMPLE");
    for (const example of examples) {
      recursiveGetRequires(example.firstChild.nodeValue, reqSet);
    }


const reqArr = [];
for (const reqName of reqSet) {
const snippetEntry = snippetStore[reqName];
if (snippetEntry) {
reqArr.push(snippetEntry.codeStr);
reqArr.push("
");
}
}
const reqStr = reqArr.join("");
const examples = node.getElementsByTagName("EXAMPLE");
const exampleArr = [];
for (let i = 0; examples[i]; i++) {
Expand Down Expand Up @@ -168,4 +151,4 @@ export const processSnippetJs = (node, writeTo, fileFormat) => {
}
};

export default processSnippetJs;
export default processSnippetJs;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It's a common convention and a good practice to have a newline at the end of a file. Many tools and version control systems expect it. Please consider adding it back.

export default processSnippetJs;