-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: report leaks, improve cli output
- Loading branch information
1 parent
11af4b0
commit 44e698a
Showing
36 changed files
with
569 additions
and
165 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.husky | ||
.prettierignore | ||
.prettierignore | ||
*.snap |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
3 changes: 2 additions & 1 deletion
3
src/extract-classes-from-file.ts → src/functions/extract-classes-from-file.ts
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,20 @@ | ||
import type { FileSelector } from "../models/FileSelector.js" | ||
import type { SelectorsReport } from "../models/SelectorsReport.js" | ||
|
||
import { replenishSetList } from "../utils/list.js" | ||
|
||
export const findDuplicates = (fileSelectors: FileSelector[]): SelectorsReport => { | ||
const selectorsReport: SelectorsReport = {} | ||
|
||
fileSelectors.forEach(fileSelector => { | ||
fileSelector.selectors | ||
.flatMap(selector => selector.split(".")) | ||
.filter(selector => selector.includes("--")) | ||
.forEach(selector => { | ||
replenishSetList(selectorsReport, selector, fileSelector.filePath) | ||
}) | ||
}) | ||
|
||
const duplicateEntries = Object.entries(selectorsReport).filter(([, files]) => files.size > 1) | ||
return Object.fromEntries(duplicateEntries) | ||
} |
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,57 @@ | ||
import type { FileSelector } from "../models/FileSelector.js" | ||
import type { SelectorsReport } from "../models/SelectorsReport.js" | ||
|
||
import { replenishSetList } from "../utils/list.js" | ||
|
||
export const findLeaks = (fileSelectors: FileSelector[]): SelectorsReport => { | ||
const globalSelectorsReport: SelectorsReport = {} | ||
const modifierSelectorsReport: SelectorsReport = {} | ||
|
||
fileSelectors.forEach(fileSelector => { | ||
fileSelector.selectors.forEach(selector => { | ||
const classSelectors = selector.split(".") | ||
|
||
// case 1: vkcn element with modifiers | ||
if (classSelectors[1]?.includes("--")) { | ||
const [, , ...modifierSelectors] = classSelectors | ||
|
||
modifierSelectors.forEach(modifier => { | ||
replenishSetList(modifierSelectorsReport, modifier, fileSelector.filePath) | ||
}) | ||
} | ||
|
||
// case 2: nested modifiers | ||
else if (classSelectors[0] === "&") { | ||
const [, ...modifierSelectors] = classSelectors | ||
|
||
modifierSelectors.forEach(modifier => { | ||
replenishSetList(modifierSelectorsReport, modifier, fileSelector.filePath) | ||
}) | ||
} | ||
|
||
// case 3: global class selector | ||
else if (classSelectors[0] === "") { | ||
classSelectors.slice(1).forEach(classSelector => { | ||
replenishSetList(globalSelectorsReport, classSelector, fileSelector.filePath) | ||
}) | ||
} | ||
|
||
// case X: unhandled | ||
else throw new Error("UNHANDLED CASE! PLEASE REPORT A NEW ISSUE.") | ||
}) | ||
}) | ||
|
||
const leakEntries = Object.entries(modifierSelectorsReport) | ||
.map(([selector, files]) => { | ||
const globalSelectors = globalSelectorsReport[selector] | ||
|
||
if (globalSelectors) { | ||
globalSelectors.forEach(gs => files.add(gs)) | ||
} | ||
|
||
return [selector, files] as const | ||
}) | ||
.filter(([, files]) => files.size > 1) | ||
|
||
return Object.fromEntries(leakEntries) | ||
} |
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,52 @@ | ||
import pc from "picocolors" | ||
|
||
import type { SelectorsReport } from "../models/SelectorsReport.js" | ||
|
||
const renderReport = (lines: string[]): string => lines.join("\n") | ||
|
||
const getReport = ( | ||
selectorFiles: SelectorsReport, | ||
messages: { | ||
none: string | ||
title: (amount: number) => string | ||
line: (selector: string, amount: number) => string | ||
}, | ||
): [string, number] => { | ||
const lines: string[] = [] | ||
|
||
const duplicateClassSelectors = Object.keys(selectorFiles) | ||
if (duplicateClassSelectors.length === 0) { | ||
lines.push(messages.none) | ||
return [renderReport(lines), 0] | ||
} | ||
|
||
lines.push(messages.title(duplicateClassSelectors.length)) | ||
lines.push("") | ||
|
||
duplicateClassSelectors.forEach(classSelector => { | ||
const files = selectorFiles[classSelector]! | ||
|
||
lines.push(messages.line(classSelector, files.size)) | ||
for (const file of files) lines.push(` - ${file}`) | ||
lines.push("") | ||
}) | ||
|
||
return [renderReport(lines), 1] | ||
} | ||
|
||
export const getDuplicatesReport = (selectorFiles: SelectorsReport): [string, number] => { | ||
return getReport(selectorFiles, { | ||
none: pc.green("👍 No duplicate class selectors found"), | ||
title: duplicatesAmount => `❌ Found ${pc.bold(duplicatesAmount)} ${pc.red("duplicated")} class selectors`, | ||
line: (classSelector, filesAmount) => | ||
`Class selector ".${pc.yellow(classSelector)}" defined in ${pc.blue(filesAmount)} files`, | ||
}) | ||
} | ||
export const getLeaksReport = (selectorFiles: SelectorsReport): [string, number] => { | ||
return getReport(selectorFiles, { | ||
none: pc.green("👍 No potentially leaking class selectors found"), | ||
title: leaksAmount => `❌ Found ${pc.bold(leaksAmount)} ${pc.red("potentially leaking")} class selectors`, | ||
line: (classSelector, filesAmount) => | ||
`Class selector ".${pc.yellow(classSelector)}" can potentially leak in ${pc.blue(filesAmount)} files`, | ||
}) | ||
} |
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,4 @@ | ||
export type FileSelector = { | ||
filePath: string | ||
selectors: string[] | ||
} |
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,2 @@ | ||
/** Object with key as selector class and value as set of file paths */ | ||
export type SelectorsReport = Record<string, Set<string>> |
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 was deleted.
Oops, something went wrong.
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 @@ | ||
export const uniqArray = (list: string[]): string[] => Array.from(new Set<string>(list)) |
Oops, something went wrong.