-
-
Notifications
You must be signed in to change notification settings - Fork 26
fix: Ensure languageOptions.customSyntax is serializable #212
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -51,6 +51,34 @@ const blockCloserTokenTypes = new Map([ | |||||||||||||||||
[tokenTypes.RightSquareBracket, "["], | ||||||||||||||||||
]); | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* Recursively replaces all function values in an object with boolean true. | ||||||||||||||||||
* Used to make objects serializable for JSON output. | ||||||||||||||||||
* | ||||||||||||||||||
* @param {Record<string,any>} object The object to process. | ||||||||||||||||||
* @returns {Record<string,any>} A copy of the object with all functions replaced by true. | ||||||||||||||||||
*/ | ||||||||||||||||||
function replaceFunctions(object) { | ||||||||||||||||||
if (typeof object !== "object" || object === null) { | ||||||||||||||||||
return object; | ||||||||||||||||||
} | ||||||||||||||||||
if (Array.isArray(object)) { | ||||||||||||||||||
return object.map(replaceFunctions); | ||||||||||||||||||
} | ||||||||||||||||||
const result = {}; | ||||||||||||||||||
for (const key of Object.keys(object)) { | ||||||||||||||||||
const value = object[key]; | ||||||||||||||||||
if (typeof value === "function") { | ||||||||||||||||||
result[key] = true; | ||||||||||||||||||
} else if (typeof value === "object" && value !== null) { | ||||||||||||||||||
result[key] = replaceFunctions(value); | ||||||||||||||||||
} else { | ||||||||||||||||||
result[key] = value; | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
return result; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
//----------------------------------------------------------------------------- | ||||||||||||||||||
// Exports | ||||||||||||||||||
//----------------------------------------------------------------------------- | ||||||||||||||||||
|
@@ -101,7 +129,8 @@ export class CSSLanguage { | |||||||||||||||||
/** | ||||||||||||||||||
* Validates the language options. | ||||||||||||||||||
* @param {CSSLanguageOptions} languageOptions The language options to validate. | ||||||||||||||||||
* @throws {Error} When the language options are invalid. | ||||||||||||||||||
* @returns {void} | ||||||||||||||||||
* @throws {TypeError} When the language options are invalid. | ||||||||||||||||||
*/ | ||||||||||||||||||
validateLanguageOptions(languageOptions) { | ||||||||||||||||||
if ( | ||||||||||||||||||
|
@@ -125,6 +154,50 @@ export class CSSLanguage { | |||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* Normalizes the language options so they can be serialized. | ||||||||||||||||||
* @param {CSSLanguageOptions} languageOptions The language options to normalize. | ||||||||||||||||||
* @returns {CSSLanguageOptions} The normalized language options. | ||||||||||||||||||
*/ | ||||||||||||||||||
normalizeLanguageOptions(languageOptions) { | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] The method modifies the input object by adding a Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||
// if there's no custom syntax then no changes are necessary | ||||||||||||||||||
if (!languageOptions?.customSyntax) { | ||||||||||||||||||
return languageOptions; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
Object.defineProperty(languageOptions, "toJSON", { | ||||||||||||||||||
value() { | ||||||||||||||||||
// Shallow copy | ||||||||||||||||||
const result = { ...languageOptions }; | ||||||||||||||||||
Comment on lines
+168
to
+171
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to set the
Suggested change
We'd need to change the return value of the function as well and update some unit tests. |
||||||||||||||||||
result.customSyntax = { ...result.customSyntax }; | ||||||||||||||||||
|
||||||||||||||||||
if (result.customSyntax.node) { | ||||||||||||||||||
result.customSyntax.node = replaceFunctions( | ||||||||||||||||||
result.customSyntax.node, | ||||||||||||||||||
); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
if (result.customSyntax.scope) { | ||||||||||||||||||
result.customSyntax.scope = replaceFunctions( | ||||||||||||||||||
result.customSyntax.scope, | ||||||||||||||||||
); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
if (result.customSyntax.atrule) { | ||||||||||||||||||
result.customSyntax.atrule = replaceFunctions( | ||||||||||||||||||
result.customSyntax.atrule, | ||||||||||||||||||
); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
return result; | ||||||||||||||||||
}, | ||||||||||||||||||
enumerable: false, | ||||||||||||||||||
configurable: true, | ||||||||||||||||||
}); | ||||||||||||||||||
|
||||||||||||||||||
return languageOptions; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* Parses the given file into an AST. | ||||||||||||||||||
* @param {File} file The virtual file to parse. | ||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this function also returns arrays and non-object values depending on the type of the argument?