@@ -12,7 +12,7 @@ const config = JSON.parse(readFileSync(path.join(root, "knip.json"), "utf8"))
1212
1313// Exercise the installed analyzer and the repository's extension graph, rather
1414// than asserting that a configuration flag merely appears in a JSON file.
15- function fixture ( files , run ) {
15+ function fixture ( files , run , includeWebview = false ) {
1616 const directory = mkdtempSync ( path . join ( tmpdir ( ) , "zoo-knip-policy-" ) )
1717 try {
1818 const write = ( name , contents ) => {
@@ -21,7 +21,7 @@ function fixture(files, run) {
2121 writeFileSync ( filename , contents )
2222 }
2323 write ( "package.json" , JSON . stringify ( { name : "knip-policy-fixture" , private : true } ) )
24- write ( "pnpm-workspace.yaml" , " packages:\n - src\n" )
24+ write ( "pnpm-workspace.yaml" , ` packages:\n - src\n${ includeWebview ? " - webview-ui\n" : "" } ` )
2525 write (
2626 "src/package.json" ,
2727 JSON . stringify ( {
@@ -30,7 +30,20 @@ function fixture(files, run) {
3030 devDependencies : { vitest : "4.1.11" } ,
3131 } ) ,
3232 )
33- write ( "knip.json" , JSON . stringify ( { ...config , workspaces : { src : config . workspaces . src } } ) )
33+ write (
34+ "knip.json" ,
35+ JSON . stringify ( {
36+ ...config ,
37+ workspaces : {
38+ src : config . workspaces . src ,
39+ ...( includeWebview ? { "webview-ui" : config . workspaces [ "webview-ui" ] } : { } ) ,
40+ } ,
41+ } ) ,
42+ )
43+ if ( includeWebview ) {
44+ write ( "webview-ui/package.json" , JSON . stringify ( { name : "fixture-webview" , private : true } ) )
45+ write ( "webview-ui/src/index.tsx" , 'console.log("application entry")\n' )
46+ }
3447 write ( "src/extension.ts" , 'import { live } from "./helper"\nconsole.log(live)\n' )
3548 write ( "src/helper.ts" , "export const live = 1\n" )
3649 write (
@@ -156,3 +169,25 @@ test("unlisted runtime dependencies remain errors", () => {
156169 } ,
157170 )
158171} )
172+
173+ test ( "visual entries cannot hide helpers unused by the production application" , ( ) => {
174+ fixture (
175+ {
176+ "webview-ui/src/scene.visual.tsx" : 'import { sceneOnly } from "./scene-helper"\nconsole.log(sceneOnly)\n' ,
177+ "webview-ui/src/scene-helper.ts" : 'export const sceneOnly = "scene"\n' ,
178+ "webview-ui/src/scene.visual.fixture.tsx" :
179+ 'import { fixtureOnly } from "./fixture-helper"\nconsole.log(fixtureOnly)\n' ,
180+ "webview-ui/src/fixture-helper.ts" : 'export const fixtureOnly = "fixture"\n' ,
181+ } ,
182+ ( knip ) => {
183+ const development = knip ( )
184+ assert . equal ( development . status , 0 , JSON . stringify ( development . report ) )
185+ const production = knip ( "--production" )
186+ assert . equal ( production . status , 1 )
187+ assert . ok ( production . report . files . includes ( "webview-ui/src/scene-helper.ts" ) )
188+ assert . ok ( production . report . files . includes ( "webview-ui/src/fixture-helper.ts" ) )
189+ assert . ok ( ! production . report . files . some ( ( file ) => file . includes ( ".visual." ) ) )
190+ } ,
191+ true ,
192+ )
193+ } )
0 commit comments