Skip to content

Commit

Permalink
Merge pull request #32 from johnsoncodehk/main
Browse files Browse the repository at this point in the history
Upgrade to Volar 2.0
  • Loading branch information
mizdra authored Mar 30, 2024
2 parents a414d82 + 21fcddc commit 16e4f2b
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 254 deletions.
34 changes: 16 additions & 18 deletions examples/nextjs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
"next": "13.4.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"typescript": "^4.9.4"
"typescript": "^5.3.0"
}
}
65 changes: 32 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@
"dist"
],
"dependencies": {
"@volar/language-core": "^1.10.9",
"@volar/source-map": "^1.10.9",
"@volar/typescript": "^1.10.9",
"@volar/language-core": "~2.0.2",
"@volar/typescript": "~2.0.2",
"change-case": "^4.1.2"
},
"peerDependencies": {
Expand Down
7 changes: 1 addition & 6 deletions src/dts.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { expect, it } from 'vitest';
import { getDtsFilePath, getExportedName, getDtsContent } from './dts.js';

it('getDtsFilePath', () => {
expect(getDtsFilePath('/dir/foo.png', false)).toBe('/dir/foo.png.d.ts');
expect(getDtsFilePath('/dir/foo.png', true)).toBe('/dir/foo.d.png.ts');
});
import { getExportedName, getDtsContent } from './dts.js';

it('getExportedName', () => {
expect(getExportedName('/dir/foo-bar.png', 'constantCase', 'I_')).toBe('I_FOO_BAR');
Expand Down
9 changes: 0 additions & 9 deletions src/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@ import path from 'node:path';
import { ExportedNameCase } from './option.js';
import { changeCase } from './util';

export function getDtsFilePath(filePath: string, arbitraryExtensions: boolean): string {
const { dir, name, ext } = path.parse(filePath);
if (arbitraryExtensions) {
return path.join(dir, `${name}.d${ext}.ts`);
} else {
return path.join(dir, `${name + ext}.d.ts`);
}
}

export function getExportedName(
filePath: string,
exportedNameCase: ExportedNameCase,
Expand Down
54 changes: 10 additions & 44 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,16 @@
import { createVirtualFiles } from '@volar/language-core';
import { decorateLanguageService } from '@volar/typescript';
import type * as ts from 'typescript/lib/tsserverlibrary';
import { createLanguageServicePlugin } from '@volar/typescript/lib/quickstart/createLanguageServicePlugin.js';
import { createAssetLanguage } from './language-service/language.js';
import {
AssetLanguageServiceHost,
createAssetLanguageServiceHost,
decorateLanguageServiceHost,
} from './language-service-host.js';
import { getAssetPluginOptions } from './option.js';

const projectAssetLSHost = new WeakMap<ts.server.Project, AssetLanguageServiceHost>();
export = createLanguageServicePlugin((ts, info) => {
const assetPluginOptions = getAssetPluginOptions(info);

const init: ts.server.PluginModuleFactory = (modules) => {
const { typescript: ts } = modules;
const pluginModule: ts.server.PluginModule = {
create(info) {
const assetPluginOptions = getAssetPluginOptions(info);
if (!info.project.fileExists(assetPluginOptions.tsConfigPath)) {
// project name not a tsconfig path, this is a inferred project
return [];
}

if (!info.project.fileExists(assetPluginOptions.tsConfigPath)) {
// project name not a tsconfig path, this is a inferred project
return info.languageService;
}
const assetLanguage = createAssetLanguage(ts.sys, assetPluginOptions);

const assetTSLSHost = createAssetLanguageServiceHost(ts.sys, info, assetPluginOptions);
const assetLanguage = createAssetLanguage(assetTSLSHost, assetPluginOptions);
const virtualFiles = createVirtualFiles([assetLanguage]);

projectAssetLSHost.set(info.project, assetTSLSHost);

decorateLanguageService(virtualFiles, info.languageService, true);
decorateLanguageServiceHost(
assetTSLSHost,
info.project,
virtualFiles,
info.languageServiceHost,
ts,
assetPluginOptions.extensions,
);

return info.languageService;
},
getExternalFiles(project) {
return projectAssetLSHost.get(project)?.getAssetFileNames() ?? [];
},
};
return pluginModule;
};

export = init;
return [assetLanguage];
});
Loading

0 comments on commit 16e4f2b

Please sign in to comment.