Skip to content

Commit 8d53df5

Browse files
committedNov 15, 2024
Use entire body of <script lang=ts> in vue compiler (fixes #740)
1 parent 1cf68c8 commit 8d53df5

File tree

9 files changed

+37
-13
lines changed

9 files changed

+37
-13
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<script setup lang="ts">
2+
import { Enum } from './enum';
3+
Enum.Member;
4+
</script>
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export enum Enum {
2+
Member = 'member',
3+
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
import identifier from './module.mdx';
2+
import Component from './Component.vue';
3+
import { createApp } from 'vue';
4+
25
identifier;
6+
createApp(Component);
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "compilers",
33
"devDependencies": {
4-
"@mdx-js/mdx": "*"
4+
"@mdx-js/mdx": "*",
5+
"vue": "*"
56
}
67
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "ES2015"
3+
"target": "ES2015",
4+
"jsx": "preserve"
45
}
56
}

‎packages/knip/src/compilers/compilers.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type { SyncCompilerFn } from './types.js';
22

3-
const scriptExtractor = /<script\b[^>]*>([\s\S]*?)<\/script>/gm;
4-
export const importMatcher = /import[^'"]+['"]([^'"]+)['"]/g;
53
export const fencedCodeBlockMatcher = /```[\s\S]*?```/g;
64

5+
// Extract imports from body of <script> nodes
6+
const scriptExtractor = /<script\b[^>]*>([\s\S]*?)<\/script>/gm;
7+
export const importMatcher = /import[^'"]+['"]([^'"]+)['"]/g;
78
export const importsWithinScripts: SyncCompilerFn = (text: string) => {
89
const scripts = [];
910
let scriptMatch: RegExpExecArray | null;
@@ -15,3 +16,15 @@ export const importsWithinScripts: SyncCompilerFn = (text: string) => {
1516
}
1617
return scripts.join(';\n');
1718
};
19+
20+
// Extract body of <script lang="ts"> nodes
21+
const tsScriptExtractor = /<script\b[^>]*lang="ts"[^>]*>(?<body>[\s\S]*?)<\/script>/gm;
22+
export const tsScriptBodies: SyncCompilerFn = (text: string) => {
23+
const scripts = [];
24+
let scriptMatch: RegExpExecArray | null;
25+
// biome-ignore lint/suspicious/noAssignInExpressions: ignore
26+
while ((scriptMatch = tsScriptExtractor.exec(text))) {
27+
if (scriptMatch.groups?.body) scripts.push(scriptMatch.groups.body);
28+
}
29+
return scripts.join(';\n');
30+
};

‎packages/knip/src/compilers/vue.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { importsWithinScripts } from './compilers.js';
1+
import { tsScriptBodies } from './compilers.js';
22
import type { HasDependency } from './types.js';
33

44
const condition = (hasDependency: HasDependency) => hasDependency('vue') || hasDependency('nuxt');
55

6-
const compiler = importsWithinScripts;
6+
const compiler = tsScriptBodies;
77

88
export default { condition, compiler };

‎packages/knip/src/typescript/get-imports-and-exports.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { isBuiltin } from 'node:module';
22
import ts from 'typescript';
3-
import { ALIAS_TAG, ANONYMOUS, DEFAULT_EXTENSIONS, IMPORT_STAR, PROTOCOL_VIRTUAL } from '../constants.js';
3+
import { ALIAS_TAG, ANONYMOUS, IMPORT_STAR, PROTOCOL_VIRTUAL } from '../constants.js';
44
import type { GetImportsAndExportsOptions } from '../types/config.js';
55
import type { ExportMap, ExportMember, ImportDetails, ImportMap, UnresolvedImport } from '../types/dependency-graph.js';
66
import type { ExportNode, ExportNodeMember } from '../types/exports.js';
@@ -9,7 +9,7 @@ import type { IssueSymbol } from '../types/issues.js';
99
import { timerify } from '../util/Performance.js';
1010
import { addNsValue, addValue, createImports } from '../util/dependency-graph.js';
1111
import { getPackageNameFromFilePath, isStartsLikePackageName, sanitizeSpecifier } from '../util/modules.js';
12-
import { extname, isInNodeModules } from '../util/path.js';
12+
import { isInNodeModules } from '../util/path.js';
1313
import { shouldIgnore } from '../util/tag.js';
1414
import type { BoundSourceFile } from './SourceFile.js';
1515
import {
@@ -155,9 +155,7 @@ const getImportsAndExports = (
155155
addValue(imports.imported, identifier, sourceFile.fileName);
156156
}
157157

158-
if (symbol && DEFAULT_EXTENSIONS.includes(extname(sourceFile.fileName))) {
159-
importedInternalSymbols.set(symbol, filePath);
160-
}
158+
if (symbol) importedInternalSymbols.set(symbol, filePath);
161159
}
162160
};
163161

‎packages/knip/test/compilers.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ test('Support compiler functions in config', async () => {
1919
assert.deepEqual(counters, {
2020
...baseCounters,
2121
files: 2,
22-
processed: 9,
23-
total: 9,
22+
processed: 11,
23+
total: 11,
2424
});
2525
});

0 commit comments

Comments
 (0)
Please sign in to comment.