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

refactor: Use typescript for (almost) all main site javascript #489

Merged
merged 97 commits into from
Nov 23, 2024

Conversation

Mitcheljager
Copy link
Owner

I was going to implement Typescript specifically for the editor, but I decided to get it started for the main site javascript first. I did everything in one big go, which may have been a mistake. There were quite a few files and I didn't spend super long on any of them, as a result I probably took some shortcuts I shouldn't have, but at least we now have type safety.

I skipped the markdown editor scripts as they are pretty complex and I want to take care of them separately.

In the end I'm not entirely sure it's really worth it, these utils aren't very complex and a lot of the type safety here doesn't do much. I've had to put in a lot of early returns in case of missing elements, but in reality these elements will never be missing.

I don't expect anyone to review this, as it's a rather nasty PR. I will early merge it soon-ish or use it as a base PR when tackling the editor compiler. Svelte components will have to wait until the Svelte 5 upgrade.

@netux
Copy link
Contributor

netux commented Nov 9, 2024

I've had to put in a lot of early returns in case of missing elements, but in reality these elements will never be missing.

You can use the non-null assertion operation

@@ -46,25 +43,28 @@
array.forEach(rule => {
const item = document.createElement("option")

item.innerHTML = rule[0]
item.value = rule[1]
item.innerHTML = rule[0].toString()

Check warning

Code scanning / CodeQL

DOM text reinterpreted as HTML Medium

DOM text
is reinterpreted as HTML without escaping meta-characters.

Copilot Autofix AI 16 days ago

To fix the problem, we need to ensure that any text content extracted from the DOM and reinserted as HTML is properly escaped to prevent XSS attacks. The best way to do this is to use a method that sets the text content safely without interpreting it as HTML.

  • Replace the use of innerHTML with textContent to ensure that the text is treated as plain text and not HTML.
  • This change should be made on line 46 in the createRules function.
Suggested changeset 1
app/javascript/src/difference-viewer.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/app/javascript/src/difference-viewer.ts b/app/javascript/src/difference-viewer.ts
--- a/app/javascript/src/difference-viewer.ts
+++ b/app/javascript/src/difference-viewer.ts
@@ -45,3 +45,3 @@
 
-    item.innerHTML = rule[0].toString()
+    item.textContent = rule[0].toString()
     item.value = rule[1].toString()
EOF
@@ -45,3 +45,3 @@

item.innerHTML = rule[0].toString()
item.textContent = rule[0].toString()
item.value = rule[1].toString()
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
refactor: Use ts for linter and related files
refactor: Use Typescript for editor compiler
refactor: Use typescript for editor utils
@Mitcheljager Mitcheljager merged commit 2354190 into master Nov 23, 2024
7 checks passed
@Mitcheljager Mitcheljager deleted the refactor/typescript branch November 23, 2024 13:43
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.

2 participants