Skip to content

Commit

Permalink
Merge pull request #29111 from webpro/chore/add-knip
Browse files Browse the repository at this point in the history
Build: Use Knip for project linting
  • Loading branch information
JReinhold authored Sep 27, 2024
2 parents f529029 + 84236d8 commit f2363de
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 4 deletions.
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[]) ?? [];
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

0 comments on commit f2363de

Please sign in to comment.