Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
so1ve committed May 8, 2024
1 parent fe72955 commit 09d076c
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 36 deletions.
8 changes: 7 additions & 1 deletion packages/language-core/lib/codegen/template/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as CompilerDOM from '@vue/compiler-dom';
import { camelize, capitalize } from '@vue/shared';
import type { Code, VueCodeInformation } from '../../types';
import { hyphenateTag } from '../../utils/shared';
import { BindingTypes } from '../../utils/parseBindings';
import { collectVars, createTsAst, endOfLine, newLine, variableNameRegex, wrapWith } from '../common';
import { generateCamelized } from './camelized';
import type { TemplateCodegenContext } from './context';
Expand Down Expand Up @@ -30,7 +31,12 @@ export function* generateComponent(
: [startTagOffset];
const propsFailedExps: CompilerDOM.SimpleExpressionNode[] = [];
const possibleOriginalNames = getPossibleOriginalComponentNames(node.tag, true);
const matchImportName = possibleOriginalNames.find(name => options.scriptSetupImportComponentNames.has(name));
const matchImportName = possibleOriginalNames.find(name => {
const bindingType = ctx.bindingTypes?.get(name);
if (bindingType) {
return bindingType & BindingTypes.Component;
}
});
const var_originalComponent = matchImportName ?? ctx.getInternalVariable();
const var_functionalComponent = ctx.getInternalVariable();
const var_componentInstance = ctx.getInternalVariable();
Expand Down
1 change: 0 additions & 1 deletion packages/language-core/lib/codegen/template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export interface TemplateCodegenOptions {
template: NonNullable<Sfc['template']>;
shouldGenerateScopedClasses?: boolean;
stylesScopedClasses: Set<string>;
scriptSetupImportComponentNames: Set<string>;
hasDefineSlots?: boolean;
slotsAssignName?: string;
propsAssignName?: string;
Expand Down
13 changes: 0 additions & 13 deletions packages/language-core/lib/parsers/scriptSetupRanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export function parseScriptSetupRanges(
const bindings = parseBindings(ts, ast, vueCompilerOptions);
const text = ast.text;
const leadingCommentEndOffset = ts.getLeadingCommentRanges(text, 0)?.reverse()[0].end ?? 0;
const importComponentNames = new Set<string>();

ts.forEachChild(ast, node => {
const isTypeExport = (ts.isTypeAliasDeclaration(node) || ts.isInterfaceDeclaration(node)) && node.modifiers?.some(mod => mod.kind === ts.SyntaxKind.ExportKeyword);
Expand All @@ -72,25 +71,13 @@ export function parseScriptSetupRanges(
}
foundNonImportExportNode = true;
}

if (
ts.isImportDeclaration(node)
&& node.importClause?.name
&& !node.importClause.isTypeOnly
) {
const moduleName = getNodeText(ts, node.moduleSpecifier, ast).slice(1, -1);
if (vueCompilerOptions.extensions.some(ext => moduleName.endsWith(ext))) {
importComponentNames.add(getNodeText(ts, node.importClause.name, ast));
}
}
});
ts.forEachChild(ast, child => visitNode(child, [ast]));

return {
leadingCommentEndOffset,
importSectionEndOffset,
bindings,
importComponentNames,
props,
slots,
emits,
Expand Down
20 changes: 0 additions & 20 deletions packages/language-core/lib/plugins/vue-tsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ function createTsx(
template: _sfc.template,
shouldGenerateScopedClasses: shouldGenerateScopedClasses(),
stylesScopedClasses: stylesScopedClasses(),
scriptSetupImportComponentNames: scriptSetupImportComponentNames(),
hasDefineSlots: hasDefineSlots(),
slotsAssignName: slotsAssignName(),
propsAssignName: propsAssignName(),
Expand All @@ -140,13 +139,6 @@ function createTsx(
};
});
const hasDefineSlots = computed(() => !!scriptSetupRanges()?.slots.define);
const scriptSetupImportComponentNames = computed<Set<string>>(oldNames => {
const newNames = scriptSetupRanges()?.importComponentNames ?? new Set();
if (newNames && oldNames && twoSetsEqual(newNames, oldNames)) {
return oldNames;
}
return newNames;
});
const slotsAssignName = computed(() => scriptSetupRanges()?.slots.name);
const propsAssignName = computed(() => scriptSetupRanges()?.props.name);
const bindingTypes = computed(() => scriptSetupRanges()?.bindings.bindingTypes ?? scriptRanges()?.bindings.bindingTypes);
Expand Down Expand Up @@ -192,15 +184,3 @@ function createTsx(
generatedTemplate,
};
}

function twoSetsEqual(a: Set<string>, b: Set<string>) {
if (a.size !== b.size) {
return false;
}
for (const file of a) {
if (!b.has(file)) {
return false;
}
}
return true;
}
6 changes: 5 additions & 1 deletion packages/language-core/lib/utils/parseBindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ export function parseBindings(
if (node.importClause.name) {
const name = _getNodeText(node.importClause.name);
bindingRanges.push(_getStartEnd(node.importClause.name));
if (ts.isStringLiteral(node.moduleSpecifier) && vueCompilerOptions?.extensions.some(ext => _getNodeText(node.moduleSpecifier).endsWith(ext))) {
if (
ts.isStringLiteral(node.moduleSpecifier)
&& !node.importClause.isTypeOnly
&& vueCompilerOptions?.extensions.some(ext => _getNodeText(node.moduleSpecifier).slice(1, -1).endsWith(ext))
) {
bindingTypes.set(name, BindingTypes.NoUnref | BindingTypes.Component);
}
else {
Expand Down

0 comments on commit 09d076c

Please sign in to comment.