-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
Build: Use Knip for project linting #29111
Merged
+259
−4
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c55b7b5
Add Knip with initial custom config
webpro 9f15da5
Move script/config
webpro 397bb94
Add knip job to circle ci
webpro 01cca67
Fix TS issues (use matcher pkg that we have types for)
webpro 79966f3
Merge branch 'next' into chore/add-knip
webpro 84236d8
Merge branch 'next' into chore/add-knip
ndelangen 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 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,93 @@ | ||
import { join, relative } from 'node:path'; | ||
|
||
import fg from 'fast-glob'; | ||
import type { KnipConfig } from 'knip'; | ||
import { match } from 'minimatch'; | ||
|
||
// Files we want to exclude from analysis should be negated project patterns, not `ignores` | ||
// docs: https://knip.dev/guides/configuring-project-files | ||
const project = [ | ||
'src/**/*.{js,jsx,ts,tsx}', | ||
'!**/__search-files-tests__/**', | ||
'!**/__testfixtures__/**', | ||
'!**/__mocks-ng-workspace__/**', | ||
'!**/__mockdata__/**', | ||
]; | ||
|
||
// Adding an explicit MDX "compiler", as the dependency knip looks for isn't listed (@mdx-js/mdx or astro) | ||
// Alternatively, we could ignore a few false positives | ||
// docs: https://knip.dev/features/compilers | ||
const importMatcher = /import[^'"]+['"]([^'"]+)['"]/g; | ||
const fencedCodeBlockMatcher = /```[\s\S]*?```/g; | ||
const mdx = (text: string) => | ||
[...text.replace(fencedCodeBlockMatcher, '').matchAll(importMatcher)].join('\n'); | ||
|
||
const baseConfig = { | ||
ignoreWorkspaces: ['renderers/svelte'], // ignored: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in code/node_modules/@sveltejs/vite-plugin-svelte/package.json | ||
|
||
// storybook itself configured (only) in root | ||
storybook: { entry: ['**/*.@(mdx|stories.@(mdx|js|jsx|mjs|ts|tsx))'] }, | ||
|
||
workspaces: { | ||
'.': { | ||
project, | ||
}, | ||
'addons/*': { | ||
project, | ||
}, | ||
'builders/*': { | ||
project, | ||
}, | ||
core: { | ||
entry: ['src/index.ts', 'src/cli/bin/index.ts', 'src/*/{globals*,index,runtime}.ts'], | ||
project, | ||
}, | ||
'frameworks/*': { | ||
entry: [ | ||
// these extra entries we only need for frameworks/angular and frameworks/ember it seems | ||
'src/index.ts', | ||
'src/builders/{build,start}-storybook/index.ts', | ||
'src/**/docs/{index,config}.{js,ts}', | ||
], | ||
project, | ||
}, | ||
'lib/*': { | ||
project, | ||
}, | ||
'presets/*': { | ||
project, | ||
}, | ||
'renderers/*': { | ||
project, | ||
}, | ||
}, | ||
compilers: { | ||
mdx, | ||
}, | ||
} satisfies KnipConfig; | ||
|
||
// Adds package.json#bundler.entries etc. to each workspace config `entry: []` | ||
export const addBundlerEntries = async (config: KnipConfig) => { | ||
const baseDir = join(__dirname, '../code'); | ||
const rootManifest = await import(join(baseDir, 'package.json')); | ||
const workspaceDirs = await fg(rootManifest.workspaces.packages, { cwd: baseDir, onlyDirectories: true }); | ||
const workspaceDirectories = workspaceDirs.map((dir) => relative(baseDir, join(baseDir, dir))); | ||
for (const wsDir of workspaceDirectories) { | ||
for (const configKey of Object.keys(baseConfig.workspaces)) { | ||
if (match([wsDir], configKey)) { | ||
const manifest = await import(join(baseDir, wsDir, 'package.json')); | ||
const configEntries = (config.workspaces[configKey].entry as string[]) ?? []; | ||
const bundler = manifest?.bundler; | ||
for (const value of Object.values(bundler ?? {})) { | ||
if (Array.isArray(value)) { | ||
configEntries.push(...value); | ||
} | ||
} | ||
config.workspaces[configKey].entry = Array.from(new Set(configEntries)); | ||
} | ||
} | ||
} | ||
return config; | ||
}; | ||
|
||
export default addBundlerEntries(baseConfig); |
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
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.
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.
logic: This line assumes config.workspaces[configKey].entry is always defined. Add a check to ensure it exists before casting.