Skip to content

Commit 46cb338

Browse files
committed
Treat Props as used when Astro.props is referenced (resolve #1629)
1 parent 1a99048 commit 46cb338

3 files changed

Lines changed: 9 additions & 2 deletions

File tree

packages/knip/fixtures/plugins/astro/knip.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export default {
22
ignore: '.astro/types.d.ts',
3+
ignoreExportsUsedInFile: true,
34
compilers: {
45
css: (text: string) => [...text.matchAll(/(?<=@)import[^;]+/g)].join('\n'),
56
},

packages/knip/fixtures/plugins/astro/src/components/FormattedDate.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
interface Props {
2+
export interface Props {
33
date: Date;
44
}
55

packages/knip/src/plugins/astro/compiler.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import { frontmatterMatcher, scriptBodies } from '../../compilers/compilers.ts';
22

3+
const propsDeclMatcher = /(?:^|[\s;])(?:interface|type)\s+Props\b/;
4+
35
const compiler = (text: string, path: string) => {
46
const scripts = [];
57

68
const frontmatter = text.match(frontmatterMatcher);
7-
if (frontmatter?.[1]) scripts.push(frontmatter[1]);
9+
if (frontmatter?.[1]) {
10+
let fm = frontmatter[1];
11+
if (propsDeclMatcher.test(fm) && text.includes('Astro.props')) fm += '\ntype __knip_astro_props = Props;';
12+
scripts.push(fm);
13+
}
814

915
const scriptContent = scriptBodies(text, path);
1016
if (scriptContent) scripts.push(scriptContent);

0 commit comments

Comments
 (0)