Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Separate solution logic from comments.js #378

Merged
merged 3 commits into from
Mar 24, 2024

Conversation

yammesicka
Copy link
Member

No description provided.

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @yammesicka - I've reviewed your changes and they look great!

General suggestions:

  • Consider exploring more declarative approaches or DOM parsing libraries to optimize readability and performance in updateOpenedSpans.
  • Investigate safer methods for HTML manipulation to mitigate potential security risks associated with direct innerHTML manipulation.
  • Reevaluate the necessity of the 'load' event listener for script initialization to potentially enhance script execution time.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟡 Security: 1 issue found
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Docstrings: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

Comment on lines 1 to 25
function updateOpenedSpans(currentSpans, line) {
/* Because we have each line wrapped in it's own span, we must close
* all the opened spans in this specific line and re-open them in the next
* line. This function help us to manage the state of open span tags.
*/
let isCatching = false;
let phrase = '';
for (let i = 0; i < line.length; i += 1) {
const c = line[i];
if (c === '>') {
isCatching = false;
phrase = `<${phrase}>`;
if (phrase === '</span>') {
currentSpans.pop();
} else if (phrase.startsWith('<span')) {
currentSpans.push(phrase);
}
phrase = '';
} else if (c === '<') {
isCatching = true;
} else if (isCatching) {
phrase += c;
}
}
}
Copy link

Choose a reason for hiding this comment

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

suggestion (code_refinement): Consider optimizing the updateOpenedSpans function for readability and performance.

The current implementation uses string concatenation and checks within a loop, which might be optimized for better readability and possibly performance by using a more declarative approach or leveraging existing DOM parsing libraries.

phrase += c;
}
}
}
Copy link

Choose a reason for hiding this comment

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

🚨 issue (security): Ensure proper handling of special characters in HTML.

The direct manipulation of innerHTML could lead to issues with special characters or potential security vulnerabilities. Consider using textContent and createElement for safer and more controlled manipulation.

Comment on lines +45 to +47
window.addEventListener('load', () => {
addLineSpansToPre(document.getElementsByTagName('code'));
});
Copy link

Choose a reason for hiding this comment

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

suggestion (performance): Review the necessity of the 'load' event listener for initializing the script.

If the script tag is placed at the end of the body, the DOM content would already be loaded, making the 'load' event listener redundant. This could improve the script's execution time.

Comment on lines +37 to +38
const wrappedLine = `<div class="line-container" data-line="${i + 1}"><span class="line-number" style="width: ${digits}em">${i + 1}</span> <span data-line="${i + 1}" class="line">${lineContent}</span></div>`;
return wrappedLine;
Copy link

Choose a reason for hiding this comment

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

suggestion (code-quality): Inline variable that is immediately returned (inline-immediately-returned-variable)

Suggested change
const wrappedLine = `<div class="line-container" data-line="${i + 1}"><span class="line-number" style="width: ${digits}em">${i + 1}</span> <span data-line="${i + 1}" class="line">${lineContent}</span></div>`;
return wrappedLine;
return `<div class="line-container" data-line="${i + 1}"><span class="line-number" style="width: ${digits}em">${i + 1}</span> <span data-line="${i + 1}" class="line">${lineContent}</span></div>`;


ExplanationSomething that we often see in people's code is assigning to a result variable
and then immediately returning it.

Returning the result directly shortens the code and removes an unnecessary
variable, reducing the mental load of reading the function.

Where intermediate variables can be useful is if they then get used as a
parameter or a condition, and the name can act like a comment on what the
variable represents. In the case where you're returning it from a function, the
function name is there to tell you what the result is, so the variable name
is unnecessary.

Copy link

codecov bot commented Mar 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.48%. Comparing base (d092cd9) to head (f276b16).

❗ Current head f276b16 differs from pull request most recent head 81221f4. Consider uploading reports for the commit 81221f4 to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #378   +/-   ##
=======================================
  Coverage   80.48%   80.48%           
=======================================
  Files          65       65           
  Lines        3049     3049           
=======================================
  Hits         2454     2454           
  Misses        595      595           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@yammesicka yammesicka merged commit f87079a into master Mar 24, 2024
3 checks passed
@yammesicka yammesicka deleted the fix-shared-solution-2 branch March 24, 2024 22:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant