Skip to content

Commit

Permalink
Use entire body of <script lang=ts> in vue compiler (fixes #740)
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Nov 14, 2024
1 parent db9da71 commit 249794a
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 13 deletions.
4 changes: 4 additions & 0 deletions packages/knip/fixtures/compilers/Component.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script setup lang="ts">
import { Enum } from './enum';
Enum.Member;
</script>
3 changes: 3 additions & 0 deletions packages/knip/fixtures/compilers/enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum Enum {
Member = 'member',
}
4 changes: 4 additions & 0 deletions packages/knip/fixtures/compilers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
import identifier from './module.mdx';
import Component from './Component.vue';
import { createApp } from 'vue';

identifier;
createApp(Component);
3 changes: 2 additions & 1 deletion packages/knip/fixtures/compilers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "compilers",
"devDependencies": {
"@mdx-js/mdx": "*"
"@mdx-js/mdx": "*",
"vue": "*"
}
}
3 changes: 2 additions & 1 deletion packages/knip/fixtures/compilers/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"target": "ES2015"
"target": "ES2015",
"jsx": "preserve"
}
}
17 changes: 15 additions & 2 deletions packages/knip/src/compilers/compilers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { SyncCompilerFn } from './types.js';

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

// Extract imports from body of <script> nodes
const scriptExtractor = /<script\b[^>]*>([\s\S]*?)<\/script>/gm;
export const importMatcher = /import[^'"]+['"]([^'"]+)['"]/g;
export const importsWithinScripts: SyncCompilerFn = (text: string) => {
const scripts = [];
let scriptMatch: RegExpExecArray | null;
Expand All @@ -15,3 +16,15 @@ export const importsWithinScripts: SyncCompilerFn = (text: string) => {
}
return scripts.join(';\n');
};

// Extract body of <script lang="ts"> nodes
const tsScriptExtractor = /<script\b[^>]*lang="ts"[^>]*>(?<body>[\s\S]*?)<\/script>/gm;
export const tsScriptBodies: SyncCompilerFn = (text: string) => {
const scripts = [];
let scriptMatch: RegExpExecArray | null;
// biome-ignore lint/suspicious/noAssignInExpressions: ignore
while ((scriptMatch = tsScriptExtractor.exec(text))) {
if (scriptMatch.groups?.body) scripts.push(scriptMatch.groups.body);
}
return scripts.join(';\n');
};
4 changes: 2 additions & 2 deletions packages/knip/src/compilers/vue.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { importsWithinScripts } from './compilers.js';
import { tsScriptBodies } from './compilers.js';
import type { HasDependency } from './types.js';

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

const compiler = importsWithinScripts;
const compiler = tsScriptBodies;

export default { condition, compiler };
8 changes: 3 additions & 5 deletions packages/knip/src/typescript/get-imports-and-exports.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isBuiltin } from 'node:module';
import ts from 'typescript';
import { ALIAS_TAG, ANONYMOUS, DEFAULT_EXTENSIONS, IMPORT_STAR, PROTOCOL_VIRTUAL } from '../constants.js';
import { ALIAS_TAG, ANONYMOUS, IMPORT_STAR, PROTOCOL_VIRTUAL } from '../constants.js';
import type { GetImportsAndExportsOptions } from '../types/config.js';
import type { ExportMap, ExportMember, ImportDetails, ImportMap, UnresolvedImport } from '../types/dependency-graph.js';
import type { ExportNode, ExportNodeMember } from '../types/exports.js';
Expand All @@ -9,7 +9,7 @@ import type { IssueSymbol } from '../types/issues.js';
import { timerify } from '../util/Performance.js';
import { addNsValue, addValue, createImports } from '../util/dependency-graph.js';
import { getPackageNameFromFilePath, isStartsLikePackageName, sanitizeSpecifier } from '../util/modules.js';
import { extname, isInNodeModules } from '../util/path.js';
import { isInNodeModules } from '../util/path.js';
import { shouldIgnore } from '../util/tag.js';
import type { BoundSourceFile } from './SourceFile.js';
import {
Expand Down Expand Up @@ -155,9 +155,7 @@ const getImportsAndExports = (
addValue(imports.imported, identifier, sourceFile.fileName);
}

if (symbol && DEFAULT_EXTENSIONS.includes(extname(sourceFile.fileName))) {
importedInternalSymbols.set(symbol, filePath);
}
if (symbol) importedInternalSymbols.set(symbol, filePath);
}
};

Expand Down
4 changes: 2 additions & 2 deletions packages/knip/test/compilers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test('Support compiler functions in config', async () => {
assert.deepEqual(counters, {
...baseCounters,
files: 2,
processed: 9,
total: 9,
processed: 11,
total: 11,
});
});

0 comments on commit 249794a

Please sign in to comment.