Skip to content

[heft-sass] Ensure valid d.ts output for non-identifier classnames #5233

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

Merged
merged 1 commit into from
May 14, 2025
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions build-tests/heft-sass-test/src/ExampleApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class ExampleApp extends React.Component {
<li className={stylesUseAltSyntax.exampleListItem2}>2nd</li>
<li className={stylesUseAltSyntax.exampleListItem3}>3rd</li>
</ul>
<p className={altSyntaxStyles['style-with-dashes']}>This element has a complex class name.</p>
</div>
</div>
);
Expand Down
4 changes: 4 additions & 0 deletions build-tests/heft-sass-test/src/stylesAltSyntax.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ $marginValue: '[theme:normalMargin, default: 20px]';
.label {
margin-bottom: $marginValue;
}

.style-with-dashes {
margin-top: $marginValue;
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ exports[`SASS CJS Shims stylesAltSyntax.module.scss: stylesAltSyntax.module.css
*/
.label {
margin-bottom: var(--normalMargin, 20px);
}

.style-with-dashes {
margin-top: var(--normalMargin, 20px);
}"
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ exports[`SASS No Shims stylesAltSyntax.module.scss: stylesAltSyntax.module.css 1
*/
.label {
margin-bottom: var(--normalMargin, 20px);
}

.style-with-dashes {
margin-top: var(--normalMargin, 20px);
}"
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,17 @@ exports[`SASS ESM Shims stylesAltSyntax.module.scss: stylesAltSyntax.module.css
*/
.label {
margin-bottom: var(--normalMargin, 20px);
}

.style-with-dashes {
margin-top: var(--normalMargin, 20px);
}"
`;

exports[`SASS ESM Shims stylesAltSyntax.module.scss: stylesAltSyntax.module.scss.d.ts 1`] = `
"declare interface IStyles {
label: string;
\\"style-with-dashes\\": string;
}
declare const styles: IStyles;
export default styles;"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Array [
exports[`SASS Typings stylesAltSyntax.module.scss: stylesAltSyntax.module.scss.d.ts 1`] = `
"declare interface IStyles {
label: string;
\\"style-with-dashes\\": string;
}
declare const styles: IStyles;
export default styles;"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/heft-sass-plugin",
"comment": "Quote classnames in .d.ts files to handle non-identifier characters.",
"type": "patch"
}
],
"packageName": "@rushstack/heft-sass-plugin"
}
13 changes: 12 additions & 1 deletion heft-plugins/heft-sass-plugin/src/SassProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import {
Sort
} from '@rushstack/node-core-library';

const SIMPLE_IDENTIFIER_REGEX: RegExp = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;

/**
* @public
*/
Expand Down Expand Up @@ -817,13 +819,22 @@ export class SassProcessor {
if (this._options.exportAsDefault) {
source.push(`declare interface IStyles {`);
for (const className of Object.keys(moduleMap)) {
source.push(` ${className}: string;`);
const safeClassName: string = SIMPLE_IDENTIFIER_REGEX.test(className)
? className
: JSON.stringify(className);
// Quote and escape class names as needed.
source.push(` ${safeClassName}: string;`);
}
source.push(`}`);
source.push(`declare const styles: IStyles;`);
source.push(`export default styles;`);
} else {
for (const className of Object.keys(moduleMap)) {
if (!SIMPLE_IDENTIFIER_REGEX.test(className)) {
throw new Error(
`Class name "${className}" is not a valid identifier and may only be exported using "exportAsDefault: true"`
);
}
source.push(`export const ${className}: string;`);
}
}
Expand Down
Loading