-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(linting): automate tracking of custom Sass functions for stylel…
…int (#10313) **Related Issue:** #10188 ## Summary This adds a script that tracks all custom Sass functions and updates the metadata for Stylelint’s [`function-no-unknown`](https://github.com/stylelint-scss/stylelint-scss/tree/master/src/rules/function-no-unknown) rule, addressing workflow improvements as discussed in #10188 (comment). Our pre-commit hook will run this script if there is a staged Sass file and will add any changes (if any). **Note**: this depends on #10311. --------- Co-authored-by: Ben Elan <no-reply@benelan.dev>
- Loading branch information
Showing
4 changed files
with
90 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* This script updates a variable from the stylelint config file with a list of custom Sass functions found in the project. | ||
* This helps stylelint flag unknown functions that may be unintentionally used. | ||
*/ | ||
|
||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
console.info('Scanning custom functions for Stylelint config update.'); | ||
|
||
const rootDirectory = path.join(__dirname, '..'); | ||
|
||
function collectSassFiles(dir: string): string[] { | ||
const sassFiles: string[] = []; | ||
|
||
try { | ||
fs.readdirSync(dir, { recursive: true, withFileTypes: true }).forEach(dirent => { | ||
const fullPath = path.join(dir, dirent.name); | ||
|
||
if (dirent.isFile() && fullPath.endsWith('.scss')) { | ||
sassFiles.push(fullPath); | ||
} | ||
}); | ||
} catch (err) { | ||
console.error(`Error reading directory: ${dir}`, err); | ||
} | ||
|
||
return sassFiles; | ||
} | ||
|
||
const customFunctionPattern = /@function\s+([a-zA-Z0-9_-]+)/g; | ||
const customFunctions = new Set<string>(); | ||
const sassFiles = collectSassFiles(rootDirectory); | ||
|
||
sassFiles.forEach(filePath => { | ||
try { | ||
const content = fs.readFileSync(filePath, 'utf8'); | ||
let match: RegExpExecArray | null; | ||
|
||
while ((match = customFunctionPattern.exec(content)) !== null) { | ||
customFunctions.add(match[1]); | ||
} | ||
} catch (err) { | ||
console.error(`Error reading file: ${filePath}`, err); | ||
} | ||
}); | ||
|
||
const stylelintConfigPath = path.join(__dirname, "..", "packages", "calcite-components", ".stylelintrc.cjs"); | ||
|
||
try { | ||
const stylelintConfigContent = fs.readFileSync(stylelintConfigPath, 'utf8'); | ||
const customFunctionsPattern = /const customFunctions = \[[\s\S]*?\];/; | ||
|
||
const updatedConfigContent = stylelintConfigContent.replace( | ||
customFunctionsPattern, | ||
`const customFunctions = ${JSON.stringify(Array.from(customFunctions).sort(), null, 2)};` | ||
); | ||
|
||
fs.writeFileSync(stylelintConfigPath, updatedConfigContent); | ||
console.info('Stylelint configuration updated successfully'); | ||
} catch (err) { | ||
console.error(`Error updating Stylelint configuration: ${stylelintConfigPath}`, err); | ||
} |