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

imp: improved types and utils #1014

Merged
merged 6 commits into from
Nov 9, 2024
Merged

imp: improved types and utils #1014

merged 6 commits into from
Nov 9, 2024

Conversation

ssi02014
Copy link
Contributor

@ssi02014 ssi02014 commented Nov 7, 2024

Summary

@cure53 👋
I'm very happy to announce that DOMPurify now supports direct types.
I've improved some of the types and added const assertion for tighter control.

I made additional improvements to ts-doc.

*/
const _removeAttribute = function (name, node) {
const _removeAttribute = function (name: string, element: Element): void {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I set the Node type to Element because it gives a type error, and rename it accordingly.

src/utils.ts Outdated
Comment on lines 61 to 62
function unapply<T>(func: (thisArg: any, ...args: any[]) => T) {
return (thisArg: any, ...args: any[]): T => apply(func, thisArg, args);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's much clearer than Function!

@@ -137,7 +133,7 @@ function cleanArray(array: any[]): any[] {
* @param object - The object to be cloned.
* @returns A new object that copies the original.
*/
function clone<T extends object>(object: T): T {
function clone<T extends Record<string, any>>(object: T): T {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I unified the object types to the Record type.

@ssi02014 ssi02014 force-pushed the refactor/types branch 2 times, most recently from d748150 to 757d02c Compare November 7, 2024 09:31
@cure53
Copy link
Owner

cure53 commented Nov 8, 2024

Thanks a ton 🙂

@reduckted and @aloisklink who have worked on the initial PRs - do you see any blockers or all good? We will likely release DOMPurify 3.2.0 soon in case all is fine.

@ssi02014
Copy link
Contributor Author

ssi02014 commented Nov 8, 2024

Let me know if you find anything wrong or if I missed anything 😃

Copy link
Contributor

@reduckted reduckted left a comment

Choose a reason for hiding this comment

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

Looks OK. Just a lot of redundant JSDoc tags that are unnecessarily specifying the type.

src/utils.ts Outdated
* @param object - The object to look up the getter function in its prototype chain.
* @param prop - The property name for which to find the getter function.
* @returns The getter function found in the prototype chain or a fallback function.
* @param {T} object - The object to look up the getter function in its prototype chain.
Copy link
Contributor

Choose a reason for hiding this comment

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

The types do not need to be specified in the JSDoc comments. The type annotations define the type.

src/utils.ts Outdated
@@ -134,10 +134,10 @@ function cleanArray(array: any[]): any[] {
/**
* Shallow clone an object
*
* @param object - The object to be cloned.
* @returns A new object that copies the original.
* @param {T} object - The object to be cloned.
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for the types in the JSDoc comments here.

src/utils.ts Outdated
@@ -116,10 +116,10 @@ function addToSet(
/**
* Clean up an array to harden against CSPP
*
* @param array - The array to be cleaned.
* @returns The cleaned version of the array
* @param {T[]} array - The array to be cleaned.
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for the types in the JSDoc comments here.

src/utils.ts Outdated
* @param array - The array containing elements to be added to the set.
* @param transformCaseFunc - An optional function to transform the case of each element before adding to the set.
* @returns The modified set with added elements.
* @param {Record<string, any>} set - The set to which elements will be added.
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for the types in the JSDoc comments here.

src/utils.ts Outdated
}

/**
* Creates a new function that constructs an instance of the given constructor function with the provided arguments.
*
* @param func - The constructor function to be wrapped and called.
* @returns A new function that constructs an instance of the given constructor function with the provided arguments.
* @param {(...args: any[]) => T} func - The constructor function to be wrapped and called.
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for the types in the JSDoc comments here.

src/purify.ts Outdated
@@ -1810,17 +1827,17 @@ interface DOMPurify {
* Remove a DOMPurify hook at a given entryPoint
* (pops it from the stack of hooks if more are present)
*
* @param entryPoint entry point for the hook to remove
* @return removed(popped) hook
* @param {BasicHookName} entryPoint entry point for the hook to remove
Copy link
Contributor

Choose a reason for hiding this comment

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

The types can be removed from the JSDoc comments here.

src/purify.ts Outdated
*/
removeHook(entryPoint: BasicHookName): Hook | undefined;

/**
* Remove a DOMPurify hook at a given entryPoint
* (pops it from the stack of hooks if more are present)
*
* @param entryPoint entry point for the hook to remove
* @return removed(popped) hook
* @param {'uponSanitizeElement'} entryPoint entry point for the hook to remove
Copy link
Contributor

Choose a reason for hiding this comment

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

The types can be removed from the JSDoc comments here.

src/purify.ts Outdated
@@ -1830,8 +1847,8 @@ interface DOMPurify {
* Remove a DOMPurify hook at a given entryPoint
* (pops it from the stack of hooks if more are present)
*
* @param entryPoint entry point for the hook to remove
* @return removed(popped) hook
* @param {'uponSanitizeAttribute'} entryPoint entry point for the hook to remove
Copy link
Contributor

Choose a reason for hiding this comment

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

The types can be removed from the JSDoc comments here.

src/purify.ts Outdated
@@ -1840,12 +1857,15 @@ interface DOMPurify {
/**
* Removes all DOMPurify hooks at a given entryPoint
*
* @param entryPoint entry point for the hooks to remove
* @param {HookName} entryPoint entry point for the hooks to remove
Copy link
Contributor

Choose a reason for hiding this comment

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

The types can be removed from the JSDoc comments here.

src/purify.ts Outdated
*/
removeHooks(entryPoint: HookName): void;

/**
* Removes all DOMPurify hooks.
*
* @returns {void}
Copy link
Contributor

Choose a reason for hiding this comment

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

This JSDoc comment should be redundant since the return type annotation indicates the method returns void. @cure53 , do you want all functions to have an @returns JSDoc tag, even if they don't return a value?

@ssi02014
Copy link
Contributor Author

ssi02014 commented Nov 9, 2024

@reduckted @cure53 Yes, let's remove the unnecessarily specified type of JSDoc again!

I agree that after supporting typescript, we no longer need to specify types in the Jsdoc.

@ssi02014
Copy link
Contributor Author

ssi02014 commented Nov 9, 2024

I fixed it!

@cure53
Copy link
Owner

cure53 commented Nov 9, 2024

Thanks a lot everyone 🙏

@cure53 cure53 merged commit 0e54785 into cure53:main Nov 9, 2024
8 checks passed
@ssi02014 ssi02014 deleted the refactor/types branch November 9, 2024 11:22
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.

3 participants