Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(language-core): add checkUnknownDirectives option #5141

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions packages/language-core/lib/codegen/codeFeatures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import type { VueCodeInformation } from '../types';

export const codeFeatures = {
all: {
verification: true,
completion: true,
semantic: true,
navigation: true,
} as VueCodeInformation,
none: {} as VueCodeInformation,

verification: {
verification: true,
} as VueCodeInformation,

completion: {
completion: true,
} as VueCodeInformation,
additionalCompletion: {
completion: { isAdditional: true },
} as VueCodeInformation,

withoutCompletion: {
verification: true,
semantic: true,
navigation: true,
} as VueCodeInformation,

navigation: {
navigation: true,
} as VueCodeInformation,
navigationWithoutRename: {
navigation: { shouldRename: () => false },
} as VueCodeInformation,
navigationAndCompletion: {
navigation: true,
completion: true,
} as VueCodeInformation,
navigationAndAdditionalCompletion: {
navigation: true,
completion: { isAdditional: true },
} as VueCodeInformation,

withoutNavigation: {
verification: true,
completion: true,
semantic: true,
} as VueCodeInformation,

withoutHighlight: {
semantic: { shouldHighlight: () => false },
verification: true,
navigation: true,
} as VueCodeInformation,
withoutHighlightAndCompletion: {
semantic: { shouldHighlight: () => false },
verification: true,
navigation: true,
} as VueCodeInformation,
withoutHighlightAndNavigation: {
semantic: { shouldHighlight: () => false },
verification: true,
completion: true,
} as VueCodeInformation,
withoutHighlightAndCompletionAndNavigation: {
semantic: { shouldHighlight: () => false },
verification: true,
} as VueCodeInformation,
};
3 changes: 2 additions & 1 deletion packages/language-core/lib/codegen/script/component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';
import type { Code, Sfc } from '../../types';
import { codeFeatures } from '../codeFeatures';
import { endOfLine, generateSfcBlockSection, newLine } from '../utils';
import type { ScriptCodegenContext } from './context';
import { ScriptCodegenOptions, codeFeatures } from './index';
import type { ScriptCodegenOptions } from './index';

export function* generateComponent(
options: ScriptCodegenOptions,
Expand Down
3 changes: 2 additions & 1 deletion packages/language-core/lib/codegen/script/componentSelf.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as path from 'path-browserify';
import type { Code } from '../../types';
import { codeFeatures } from '../codeFeatures';
import type { TemplateCodegenContext } from '../template/context';
import { endOfLine, generateSfcBlockSection, newLine } from '../utils';
import { generateComponentSetupReturns, generateEmitsOption, generatePropsOption } from './component';
import type { ScriptCodegenContext } from './context';
import { codeFeatures, type ScriptCodegenOptions } from './index';
import type { ScriptCodegenOptions } from './index';
import { getTemplateUsageVars } from './template';

export function* generateComponentSelf(
Expand Down
26 changes: 2 additions & 24 deletions packages/language-core/lib/codegen/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import * as path from 'path-browserify';
import type * as ts from 'typescript';
import type { ScriptRanges } from '../../parsers/scriptRanges';
import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';
import type { Code, Sfc, VueCodeInformation, VueCompilerOptions } from '../../types';
import type { Code, Sfc, VueCompilerOptions } from '../../types';
import { codeFeatures } from '../codeFeatures';
import { generateGlobalTypes, getGlobalTypesFileName } from '../globalTypes';
import type { TemplateCodegenContext } from '../template/context';
import { endOfLine, generateSfcBlockSection, newLine } from '../utils';
Expand All @@ -14,29 +15,6 @@ import { generateSrc } from './src';
import { generateStyleModulesType } from './styleModulesType';
import { generateTemplate } from './template';

export const codeFeatures = {
all: {
verification: true,
completion: true,
semantic: true,
navigation: true,
} as VueCodeInformation,
none: {} as VueCodeInformation,
verification: {
verification: true,
} as VueCodeInformation,
navigation: {
navigation: true,
} as VueCodeInformation,
navigationWithoutRename: {
navigation: {
shouldRename() {
return false;
},
},
} as VueCodeInformation,
};

export interface ScriptCodegenOptions {
ts: typeof ts;
compilerOptions: ts.CompilerOptions;
Expand Down
3 changes: 2 additions & 1 deletion packages/language-core/lib/codegen/script/scriptSetup.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';
import type { Code, Sfc, TextRange } from '../../types';
import { codeFeatures } from '../codeFeatures';
import { combineLastMapping, endOfLine, generateSfcBlockSection, newLine } from '../utils';
import { generateComponent, generateEmitsOption } from './component';
import { generateComponentSelf } from './componentSelf';
import type { ScriptCodegenContext } from './context';
import { ScriptCodegenOptions, codeFeatures, generateScriptSectionPartiallyEnding } from './index';
import { type ScriptCodegenOptions, generateScriptSectionPartiallyEnding } from './index';
import { generateTemplate } from './template';

export function* generateScriptSetupImports(
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/codegen/script/src.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Code, Sfc } from '../../types';
import { codeFeatures } from '../codeFeatures';
import { endOfLine } from '../utils';
import { codeFeatures } from './index';

export function* generateSrc(
script: NonNullable<Sfc['script']>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Code } from '../../types';
import { codeFeatures } from '../codeFeatures';
import { endOfLine, newLine } from '../utils';
import type { ScriptCodegenContext } from './context';
import { ScriptCodegenOptions, codeFeatures } from './index';
import type { ScriptCodegenOptions } from './index';
import { generateCssClassProperty } from './template';

export function* generateStyleModulesType(
Expand Down
3 changes: 2 additions & 1 deletion packages/language-core/lib/codegen/script/template.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { Code } from '../../types';
import { hyphenateTag } from '../../utils/shared';
import { codeFeatures } from '../codeFeatures';
import { TemplateCodegenContext, createTemplateCodegenContext } from '../template/context';
import { generateInterpolation } from '../template/interpolation';
import { generateStyleScopedClassReferences } from '../template/styleScopedClasses';
import { endOfLine, newLine } from '../utils';
import type { ScriptCodegenContext } from './context';
import { codeFeatures, type ScriptCodegenOptions } from './index';
import type { ScriptCodegenOptions } from './index';

export function* generateTemplate(
options: ScriptCodegenOptions,
Expand Down
115 changes: 31 additions & 84 deletions packages/language-core/lib/codegen/template/context.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,10 @@
import type * as CompilerDOM from '@vue/compiler-dom';
import type { Code, VueCodeInformation } from '../../types';
import { codeFeatures } from '../codeFeatures';
import { InlayHintInfo } from '../inlayHints';
import { endOfLine, newLine, wrapWith } from '../utils';
import type { TemplateCodegenOptions } from './index';

const _codeFeatures = {
all: {
verification: true,
completion: true,
semantic: true,
navigation: true,
} as VueCodeInformation,
verification: {
verification: true,
} as VueCodeInformation,
completion: {
completion: true,
} as VueCodeInformation,
additionalCompletion: {
completion: { isAdditional: true },
} as VueCodeInformation,
navigation: {
navigation: true,
} as VueCodeInformation,
navigationWithoutRename: {
navigation: {
shouldRename() {
return false;
},
},
} as VueCodeInformation,
navigationAndCompletion: {
navigation: true,
completion: true,
} as VueCodeInformation,
navigationAndAdditionalCompletion: {
navigation: true,
completion: { isAdditional: true },
} as VueCodeInformation,
withoutNavigation: {
verification: true,
completion: true,
semantic: true,
} as VueCodeInformation,
withoutHighlight: {
semantic: { shouldHighlight: () => false },
verification: true,
navigation: true,
completion: true,
} as VueCodeInformation,
withoutHighlightAndCompletion: {
semantic: { shouldHighlight: () => false },
verification: true,
navigation: true,
} as VueCodeInformation,
withoutHighlightAndCompletionAndNavigation: {
semantic: { shouldHighlight: () => false },
verification: true,
} as VueCodeInformation,
};

export type TemplateCodegenContext = ReturnType<typeof createTemplateCodegenContext>;

export function createTemplateCodegenContext(options: Pick<TemplateCodegenOptions, 'scriptSetupBindingNames' | 'edited'>) {
Expand All @@ -74,34 +19,30 @@ export function createTemplateCodegenContext(options: Pick<TemplateCodegenOption
} | undefined;
let variableId = 0;

const codeFeatures = new Proxy(_codeFeatures, {
get(target, key: keyof typeof _codeFeatures) {
const data = target[key];
if (data.verification) {
if (ignoredError) {
return {
...data,
verification: false,
};
}
if (expectErrorToken) {
const token = expectErrorToken;
if (typeof data.verification !== 'object' || !data.verification.shouldReport) {
return {
...data,
verification: {
shouldReport: () => {
token.errors++;
return false;
},
},
};
}
}
function resolveCodeFeatures(features: VueCodeInformation) {
if (features.verification) {
if (ignoredError) {
return {
...features,
verification: false,
};
}
return data;
},
});
if (expectErrorToken) {
const token = expectErrorToken;
return {
...features,
verification: {
shouldReport: () => {
token.errors++;
return false;
},
},
};
}
}
return features;
}

const localVars = new Map<string, number>();
const specialVars = new Set<string>();
const accessExternalVariables = new Map<string, Set<number>>();
Expand Down Expand Up @@ -129,9 +70,15 @@ export function createTemplateCodegenContext(options: Pick<TemplateCodegenOption
const templateRefs = new Map<string, [varName: string, offset: number]>();

return {
codeFeatures: new Proxy(codeFeatures, {
get(target, key: keyof typeof codeFeatures) {
const data = target[key];
return resolveCodeFeatures(data);
},
}),
resolveCodeFeatures,
slots,
dynamicSlots,
codeFeatures,
specialVars,
accessExternalVariables,
lastGenericComment,
Expand Down
9 changes: 3 additions & 6 deletions packages/language-core/lib/codegen/template/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,7 @@ export function* generateComponent(
options,
ctx,
'template',
{
...ctx.codeFeatures.all,
completion: false,
},
ctx.codeFeatures.withoutCompletion,
dynamicTagInfo.tag,
dynamicTagInfo.offsets[1],
dynamicTagInfo.astHolder,
Expand Down Expand Up @@ -227,13 +224,13 @@ export function* generateComponent(
yield* wrapWith(
node.loc.start.offset,
node.loc.end.offset,
{
ctx.resolveCodeFeatures({
verification: {
shouldReport(_source, code) {
return String(code) !== '6133';
},
}
},
}),
var_componentInstance
);
yield ` = ${var_functionalComponent}`;
Expand Down
Loading
Loading