Skip to content
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
merged 6 commits into from
Sep 27, 2024
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
25 changes: 25 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,22 @@ jobs:
yarn lint
- report-workflow-on-failure
- cancel-workflow-on-failure
knip:
executor:
class: large
name: sb_node_22_classic
steps:
- git-shallow-clone/checkout_advanced:
clone_options: "--depth 1 --verbose"
- attach_workspace:
at: .
- run:
name: Knip
command: |
cd code
yarn knip --no-exit-code
- report-workflow-on-failure
- cancel-workflow-on-failure
check:
executor:
class: xlarge
Expand Down Expand Up @@ -688,6 +704,9 @@ workflows:
- lint:
requires:
- build
- knip:
requires:
- build
- check:
requires:
- build
Expand Down Expand Up @@ -754,6 +773,9 @@ workflows:
- lint:
requires:
- build
- knip:
requires:
- build
- check:
requires:
- build
Expand Down Expand Up @@ -821,6 +843,9 @@ workflows:
- lint:
requires:
- build
- knip:
requires:
- build
- check:
requires:
- build
Expand Down
1 change: 1 addition & 0 deletions code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"generate-sandboxes": "yarn --cwd ../scripts generate-sandboxes",
"github-release": "github-release-from-changelog",
"i": "yarn --cwd .. i",
"knip": "VITE_CJS_IGNORE_WARNING=1 ../scripts/node_modules/.bin/knip --config ../scripts/knip.config.ts",
"lint": "yarn lint:js && yarn lint:md",
"lint:ejs": "ejslint **/*.ejs",
"lint:js": "yarn lint:js:cmd . --quiet",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"get-report-message": "cd scripts; yarn get-report-message",
"get-template": "cd scripts; yarn get-template",
"i": "yarn --cwd scripts && yarn --cwd code",
"knip": "cd code; yarn knip",
"lint": "cd code; yarn lint",
"nx": "cd code; yarn nx",
"pretty-docs": "cd scripts; yarn install >/dev/null; yarn docs:prettier:write",
Expand Down
93 changes: 93 additions & 0 deletions scripts/knip.config.ts
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[]) ?? [];
Copy link
Contributor

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.

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);
1 change: 1 addition & 0 deletions scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
"husky": "^4.3.7",
"json5": "^2.2.3",
"junit-xml": "^1.2.0",
"knip": "^5.30.1",
"lint-staged": "^15.2.7",
"memoizerific": "^1.11.3",
"node-gyp": "^9.3.1",
Expand Down
Loading