-
Notifications
You must be signed in to change notification settings - Fork 80
chore(linting): automate tracking of custom Sass functions for stylelint #10313
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
jcfranco
merged 8 commits into
dev
from
jcfranco/10188-dynamically-track-custom-sass-functions-for-linting
Sep 16, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8e32514
refactor(stylelint): change config to module format to enable more dy…
jcfranco 832d9c6
roll back allowJs changes
jcfranco 72e0b2f
chore(linting): automate tracking of custom Sass functions for stylelint
jcfranco 72abe66
wire up to pre-commit hook
jcfranco f16a9b6
improve Windows compat
jcfranco df9e76b
use recursive option
jcfranco 5a07572
change shebang to bash
jcfranco 4f99c54
merge dev
jcfranco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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) { | ||
jcfranco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.