Skip to content

Commit cd145e2

Browse files
committed
Improve mdx "compiler" (resolve #1471)
1 parent 844beb0 commit cd145e2

4 files changed

Lines changed: 32 additions & 4 deletions

File tree

packages/knip/fixtures/compilers/module.mdx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,29 @@ import Styles from './styles.css';
77
<Component />
88

99
<Docs />
10+
11+
Using a top-level `import type` declaration (`import type { A, B } from 'fake-module'`) ensures the entire import statement is removed during transpilation.
12+
13+
You can also use `import { something } from 'another-fake'` inline.
14+
15+
Multiple inline codes: `import x from 'x'` and `import y from 'y'` on same line.
16+
17+
Nested backticks won't happen but edge case: `import Default from "double-quotes"`.
18+
19+
```js
20+
import { fenced } from 'fenced-module';
21+
import * as ns from 'namespace-module';
22+
23+
const x = 1;
24+
```
25+
26+
```typescript
27+
import type { Type } from 'typed-module';
28+
export const y = 2;
29+
```
30+
31+
```
32+
import { noLang } from 'no-lang-module';
33+
```
34+
35+
Real import after fenced block should still work (see actual imports at top).

packages/knip/src/compilers/astro-mdx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fencedCodeBlockMatcher, importMatcher, importsWithinFrontmatter } from './compilers.js';
1+
import { fencedCodeBlockMatcher, importMatcher, importsWithinFrontmatter, inlineCodeMatcher } from './compilers.js';
22
import type { HasDependency } from './types.js';
33

44
// https://docs.astro.build/en/guides/integrations-guide/mdx/
@@ -10,7 +10,7 @@ const frontmatterImportFields = ['layout'];
1010
const condition = (hasDependency: HasDependency) => astroMDXDependencies.some(hasDependency);
1111

1212
const compiler = (text: string) => {
13-
const imports = text.replace(fencedCodeBlockMatcher, '').matchAll(importMatcher);
13+
const imports = text.replace(fencedCodeBlockMatcher, '').replace(inlineCodeMatcher, '').matchAll(importMatcher);
1414

1515
const frontmatterImports = importsWithinFrontmatter(text, frontmatterImportFields);
1616

packages/knip/src/compilers/compilers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { SyncCompilerFn } from './types.js';
22

33
export const fencedCodeBlockMatcher = /```[\s\S]*?```/g;
4+
export const inlineCodeMatcher = /`[^`]+`/g;
45

56
// Extract imports from body of <script> nodes
67
const scriptExtractor = /<script\b[^>]*>([\s\S]*?)<\/script>/gm;

packages/knip/src/compilers/mdx.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fencedCodeBlockMatcher, importMatcher } from './compilers.js';
1+
import { fencedCodeBlockMatcher, importMatcher, inlineCodeMatcher } from './compilers.js';
22
import type { HasDependency } from './types.js';
33

44
// https://mdxjs.com/packages/
@@ -16,6 +16,7 @@ const mdxDependencies = [
1616

1717
const condition = (hasDependency: HasDependency) => mdxDependencies.some(hasDependency);
1818

19-
const compiler = (text: string) => [...text.replace(fencedCodeBlockMatcher, '').matchAll(importMatcher)].join('\n');
19+
const compiler = (text: string) =>
20+
[...text.replace(fencedCodeBlockMatcher, '').replace(inlineCodeMatcher, '').matchAll(importMatcher)].join('\n');
2021

2122
export default { condition, compiler };

0 commit comments

Comments
 (0)