Skip to content

Commit

Permalink
refactor(tsc): rework based on first-party TS API (#3795)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Dec 12, 2023
1 parent 7b47792 commit fbabb96
Show file tree
Hide file tree
Showing 29 changed files with 294 additions and 549 deletions.
2 changes: 1 addition & 1 deletion extensions/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@
"devDependencies": {
"@types/semver": "^7.5.3",
"@types/vscode": "^1.82.0",
"@volar/vscode": "2.0.0-alpha.3",
"@volar/vscode": "2.0.0-alpha.4",
"@vue/language-core": "1.8.25",
"@vue/language-server": "1.8.25",
"esbuild": "latest",
Expand Down
10 changes: 1 addition & 9 deletions extensions/vscode/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,6 @@ async function doActivate(context: vscode.ExtensionContext, createLc: CreateLang
// doctor.register(context, client);
// componentMeta.register(context, client);

const supportedLanguages: Record<string, boolean> = {
vue: true,
markdown: true,
javascript: true,
typescript: true,
javascriptreact: true,
typescriptreact: true,
};
const selectors: vscode.DocumentFilter[] = [{ language: 'vue' }];

if (config.server.petiteVue.supportHtmlFile) {
Expand All @@ -87,7 +79,7 @@ async function doActivate(context: vscode.ExtensionContext, createLc: CreateLang
selectors.push({ language: 'markdown' });
}

activateAutoInsertion([client], document => supportedLanguages[document.languageId]); // TODO: implement auto insert .value
activateAutoInsertion(selectors, client); // TODO: implement auto insert .value
activateDocumentDropEdit(selectors, client);
activateWriteVirtualFiles('volar.action.writeVirtualFiles', client);

Expand Down
7 changes: 3 additions & 4 deletions extensions/vscode/src/nodeClientMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import * as fs from 'fs';

export async function activate(context: vscode.ExtensionContext) {

const languageClients: lsp.LanguageClient[] = [];

let languageClient: lsp.LanguageClient;
let serverPathStatusItem: vscode.StatusBarItem | undefined;

await commonActivate(context, (
Expand Down Expand Up @@ -110,7 +109,7 @@ export async function activate(context: vscode.ExtensionContext) {
);
client.start();

languageClients.push(client);
languageClient = client;

updateProviders(client);

Expand Down Expand Up @@ -149,7 +148,7 @@ export async function activate(context: vscode.ExtensionContext) {
volarLabs: {
version: supportLabsVersion,
codegenStackSupport: true,
languageClients,
languageClient: languageClient!,
languageServerProtocol: serverLib,
},
} satisfies ExportsInfoForLabs;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"devDependencies": {
"@lerna-lite/cli": "latest",
"@lerna-lite/publish": "latest",
"@volar/language-service": "2.0.0-alpha.3",
"@volar/language-service": "2.0.0-alpha.4",
"typescript": "latest",
"vite": "latest",
"vitest": "latest"
Expand Down
2 changes: 1 addition & 1 deletion packages/component-meta/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"directory": "packages/component-meta"
},
"dependencies": {
"@volar/typescript": "2.0.0-alpha.3",
"@volar/typescript": "2.0.0-alpha.4",
"@vue/language-core": "1.8.25",
"path-browserify": "^1.0.1",
"vue-component-type-helpers": "1.8.25"
Expand Down
4 changes: 1 addition & 3 deletions packages/component-meta/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type * as ts from 'typescript/lib/tsserverlibrary';
import * as path from 'path-browserify';
import { code as typeHelpersCode } from 'vue-component-type-helpers';
import { code as vue2TypeHelpersCode } from 'vue-component-type-helpers/vue2';
import { createLanguage, decorateLanguageService } from '@volar/typescript';
import { createLanguage } from '@volar/typescript';

import type {
MetaCheckerOptions,
Expand Down Expand Up @@ -164,8 +164,6 @@ export function baseCreate(
const { languageServiceHost } = language.typescript!;
const tsLs = ts.createLanguageService(languageServiceHost);

decorateLanguageService(language.files, tsLs, false);

if (checkerOptions.forceUseTs) {
const getScriptKind = languageServiceHost.getScriptKind?.bind(languageServiceHost);
languageServiceHost.getScriptKind = (fileName) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"directory": "packages/language-core"
},
"dependencies": {
"@volar/language-core": "2.0.0-alpha.3",
"@volar/language-core": "2.0.0-alpha.4",
"@vue/compiler-dom": "^3.3.0",
"@vue/shared": "^3.3.0",
"computeds": "^0.0.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/language-core/src/generators/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function* generate(
generic: undefined,
genericOffset: 0,
attrs: {},
ast: ts.createSourceFile('', '', ts.ScriptTarget.Latest, false, ts.ScriptKind.TS),
ast: ts.createSourceFile('', '', 99 satisfies ts.ScriptTarget.Latest, false, ts.ScriptKind.TS),
};
scriptSetupRanges = {
bindings: [],
Expand Down Expand Up @@ -1013,7 +1013,7 @@ type __VLS_NormalizeEmits<T> = __VLS_Prettify<
for (const [segment, offset, onlyError] of eachInterpolationSegment(
ts,
cssBind.text,
ts.createSourceFile('/a.txt', cssBind.text, ts.ScriptTarget.ESNext),
ts.createSourceFile('/a.txt', cssBind.text, 99 satisfies ts.ScriptTarget.ESNext),
emptyLocalVars,
cssIds,
vueCompilerOptions,
Expand Down
12 changes: 6 additions & 6 deletions packages/language-core/src/generators/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ export function* generate(
if (leftExpressionRange && leftExpressionText) {

const collectAst = createTsAst(node.parseResult, `const [${leftExpressionText}]`);
collectVars(ts, collectAst, forBlockVars);
collectVars(ts, collectAst, collectAst, forBlockVars);

for (const varName of forBlockVars)
localVars.set(varName, (localVars.get(varName) ?? 0) + 1);
Expand Down Expand Up @@ -871,7 +871,7 @@ export function* generate(
);

const slotAst = createTsAst(slotDir, `(${slotDir.exp.content}) => {}`);
collectVars(ts, slotAst, slotBlockVars);
collectVars(ts, slotAst, slotAst, slotBlockVars);
hasProps = true;
if (slotDir.exp.content.indexOf(':') === -1) {
yield _ts('const [');
Expand Down Expand Up @@ -1093,10 +1093,10 @@ export function* generate(
const ast = createTsAst(prop.exp, prop.exp.content);
let isCompoundExpression = true;

if (ast.getChildCount() === 2) { // with EOF
ast.forEachChild(child_1 => {
if (ast.statements.length === 1) {
ts.forEachChild(ast, child_1 => {
if (ts.isExpressionStatement(child_1)) {
child_1.forEachChild(child_2 => {
ts.forEachChild(child_1, child_2 => {
if (ts.isArrowFunction(child_2)) {
isCompoundExpression = false;
}
Expand Down Expand Up @@ -1902,7 +1902,7 @@ export function* generate(
function createTsAst(astHolder: any, text: string) {
if (astHolder.__volar_ast_text !== text) {
astHolder.__volar_ast_text = text;
astHolder.__volar_ast = ts.createSourceFile('/a.ts', text, ts.ScriptTarget.ESNext);
astHolder.__volar_ast = ts.createSourceFile('/a.ts', text, 99 satisfies ts.ScriptTarget.ESNext);
}
return astHolder.__volar_ast as ts.SourceFile;
}
Expand Down
20 changes: 13 additions & 7 deletions packages/language-core/src/parsers/scriptRanges.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TextRange } from '../types';
import type * as ts from 'typescript/lib/tsserverlibrary';
import { getStartEnd, parseBindingRanges } from './scriptSetupRanges';
import { getNodeText, getStartEnd, parseBindingRanges } from './scriptSetupRanges';

export interface ScriptRanges extends ReturnType<typeof parseScriptRanges> { }

Expand All @@ -17,12 +17,12 @@ export function parseScriptRanges(ts: typeof import('typescript/lib/tsserverlibr

const bindings = hasScriptSetup ? parseBindingRanges(ts, ast) : [];

ast.forEachChild(raw => {
ts.forEachChild(ast, raw => {

if (ts.isExportAssignment(raw)) {

let node: ts.AsExpression | ts.ExportAssignment | ts.ParenthesizedExpression = raw;
while (ts.isAsExpression(node.expression) || ts.isParenthesizedExpression(node.expression)) { // fix https://github.com/vuejs/language-tools/issues/1882
while (isAsExpression(node.expression) || ts.isParenthesizedExpression(node.expression)) { // fix https://github.com/vuejs/language-tools/issues/1882
node = node.expression;
}

Expand All @@ -39,12 +39,13 @@ export function parseScriptRanges(ts: typeof import('typescript/lib/tsserverlibr
if (obj) {
let componentsOptionNode: ts.ObjectLiteralExpression | undefined;
let nameOptionNode: ts.Expression | undefined;
obj.forEachChild(node => {
ts.forEachChild(obj, node => {
if (ts.isPropertyAssignment(node) && ts.isIdentifier(node.name)) {
if (node.name.escapedText === 'components' && ts.isObjectLiteralExpression(node.initializer)) {
const name = getNodeText(ts, node.name, ast);
if (name === 'components' && ts.isObjectLiteralExpression(node.initializer)) {
componentsOptionNode = node.initializer;
}
if (node.name.escapedText === 'name') {
if (name === 'name') {
nameOptionNode = node.initializer;
}
}
Expand All @@ -68,6 +69,11 @@ export function parseScriptRanges(ts: typeof import('typescript/lib/tsserverlibr
};

function _getStartEnd(node: ts.Node) {
return getStartEnd(node, ast);
return getStartEnd(ts, node, ast);
}

// isAsExpression is missing in tsc
function isAsExpression(node: ts.Node): node is ts.AsExpression {
return node.kind === ts.SyntaxKind.AsExpression;
}
}

0 comments on commit fbabb96

Please sign in to comment.