From 80796163a3f1849d58302bb9604be9ffd89a8353 Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Mon, 26 Feb 2024 04:51:49 +0800 Subject: [PATCH] chore: deprecate `volar-service-tsconfig` --- packages/tsconfig/LICENSE | 21 --- packages/tsconfig/README.md | 26 ---- packages/tsconfig/index.ts | 220 -------------------------------- packages/tsconfig/package.json | 42 ------ packages/tsconfig/tsconfig.json | 4 - readme.md | 1 - 6 files changed, 314 deletions(-) delete mode 100644 packages/tsconfig/LICENSE delete mode 100644 packages/tsconfig/README.md delete mode 100644 packages/tsconfig/index.ts delete mode 100644 packages/tsconfig/package.json delete mode 100644 packages/tsconfig/tsconfig.json diff --git a/packages/tsconfig/LICENSE b/packages/tsconfig/LICENSE deleted file mode 100644 index e2ac49ce..00000000 --- a/packages/tsconfig/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2022-present Johnson Chu - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/packages/tsconfig/README.md b/packages/tsconfig/README.md deleted file mode 100644 index 2193fe09..00000000 --- a/packages/tsconfig/README.md +++ /dev/null @@ -1,26 +0,0 @@ -# volar-service-tsconfig - -Volar plugin for [`tsconfig.json`](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html). - -## Installation - -```sh -npm install volar-service-tsconfig -``` - -## Usage - -`volar.config.js` - -```js -module.exports = { - services: [ - require('volar-service-tsconfig').create(), - ], -}; -``` - - -## License - -[MIT](LICENSE) © [Johnson Chu](https://github.com/johnsoncodehk) diff --git a/packages/tsconfig/index.ts b/packages/tsconfig/index.ts deleted file mode 100644 index 2b903bec..00000000 --- a/packages/tsconfig/index.ts +++ /dev/null @@ -1,220 +0,0 @@ -import type { DocumentLink, FileType, ServicePluginInstance, ServicePlugin } from '@volar/language-service'; -import * as jsonc from 'jsonc-parser'; -import { minimatch } from 'minimatch'; -import type { TextDocument } from 'vscode-languageserver-textdocument'; -import { URI, Utils } from 'vscode-uri'; - -interface OpenExtendsLinkCommandArgs { - resourceUri: string; - extendsValue: string; -} - -function mapChildren(node: jsonc.Node | undefined, f: (x: jsonc.Node) => R): R[] { - return node && node.type === 'array' && node.children - ? node.children.map(f) - : []; -} - -export function create(): ServicePlugin { - return { - name: 'tsconfig', - create(context): ServicePluginInstance { - - const patterns = [ - '**/[jt]sconfig.json', - '**/[jt]sconfig.*.json', - ]; - const languages = ['json', 'jsonc']; - - return { - - /** - * Reference https://github.com/microsoft/vscode/blob/main/extensions/typescript-language-features/src/languageFeatures/tsconfig.ts - */ - - provideDocumentLinks(document) { - - const match = languages.includes(document.languageId) && patterns.some(pattern => minimatch(document.uri, pattern)); - if (!match) { - return []; - } - - const root = jsonc.parseTree(document.getText()); - if (!root) { - return []; - } - - const links = [ - getExtendsLink(document, root), - ...getFilesLinks(document, root), - ...getReferencesLinks(document, root) - ]; - - return links.filter(link => !!link) as DocumentLink[]; - }, - - async resolveDocumentLink(link) { - - const data: OpenExtendsLinkCommandArgs = link.data; - if (data) { - const tsconfigPath = await getTsconfigPath(Utils.dirname(URI.parse(data.resourceUri)), data.extendsValue); - if (tsconfigPath === undefined) { - // console.error(vscode.l10n.t("Failed to resolve {0} as module", data.extendsValue)); - } - link.target = tsconfigPath?.toString(); - } - return link; - }, - }; - - function getExtendsLink(document: TextDocument, root: jsonc.Node): DocumentLink | undefined { - const extendsNode = jsonc.findNodeAtLocation(root, ['extends']); - if (!isPathValue(extendsNode)) { - return undefined; - } - - const extendsValue: string = extendsNode.value; - if (extendsValue.startsWith('/')) { - return undefined; - } - - const args: OpenExtendsLinkCommandArgs = { - resourceUri: document.uri, - extendsValue: extendsValue - }; - - const link: DocumentLink = { - range: getRange(document, extendsNode), - data: args, - }; - // link.tooltip = vscode.l10n.t("Follow link"); - link.tooltip = "Follow link"; - return link; - } - - function getFilesLinks(document: TextDocument, root: jsonc.Node) { - return mapChildren( - jsonc.findNodeAtLocation(root, ['files']), - child => pathNodeToLink(document, child)); - } - - function getReferencesLinks(document: TextDocument, root: jsonc.Node) { - return mapChildren( - jsonc.findNodeAtLocation(root, ['references']), - child => { - const pathNode = jsonc.findNodeAtLocation(child, ['path']); - if (!isPathValue(pathNode)) { - return undefined; - } - - const link: DocumentLink = { - range: getRange(document, pathNode), - target: pathNode.value.endsWith('.json') - ? getFileTarget(document, pathNode) - : getFolderTarget(document, pathNode) - }; - return link; - }); - } - - function pathNodeToLink( - document: TextDocument, - node: jsonc.Node | undefined - ): DocumentLink | undefined { - return isPathValue(node) - ? { range: getRange(document, node), target: getFileTarget(document, node) } - : undefined; - } - - function isPathValue(extendsNode: jsonc.Node | undefined): extendsNode is jsonc.Node { - return extendsNode - && extendsNode.type === 'string' - && extendsNode.value - && !(extendsNode.value as string).includes('*'); // don't treat globs as links. - } - - function getFileTarget(document: TextDocument, node: jsonc.Node): string { - return Utils.joinPath(Utils.dirname(URI.parse(document.uri)), node.value).toString(); - } - - function getFolderTarget(document: TextDocument, node: jsonc.Node): string { - return Utils.joinPath(Utils.dirname(URI.parse(document.uri)), node.value, 'tsconfig.json').toString(); - } - - function getRange(document: TextDocument, node: jsonc.Node) { - const offset = node.offset; - const start = document.positionAt(offset + 1); - const end = document.positionAt(offset + (node.length - 1)); - return { start, end }; - } - - async function resolveNodeModulesPath(baseDirUri: URI, pathCandidates: string[]): Promise { - let currentUri = baseDirUri; - const baseCandidate = pathCandidates[0]; - const sepIndex = baseCandidate.startsWith('@') ? 2 : 1; - const moduleBasePath = baseCandidate.split('/').slice(0, sepIndex).join('/'); - while (true) { - const moduleAbsoluteUrl = Utils.joinPath(currentUri, 'node_modules', moduleBasePath); - const moduleStat = await context.env.fs?.stat(moduleAbsoluteUrl.toString()); - - if (moduleStat && moduleStat.type === 2 satisfies FileType.Directory) { - for (const uriCandidate of pathCandidates - .map((relativePath) => relativePath.split('/').slice(sepIndex).join('/')) - // skip empty paths within module - .filter(Boolean) - .map((relativeModulePath) => Utils.joinPath(moduleAbsoluteUrl, relativeModulePath)) - ) { - if (await exists(uriCandidate)) { - return uriCandidate; - } - } - // Continue to looking for potentially another version - } - - const oldUri = currentUri; - currentUri = Utils.joinPath(currentUri, '..'); - - // Can't go next. Reached the system root - if (oldUri.path === currentUri.path) { - return; - } - } - } - - // Reference https://github.com/microsoft/TypeScript/blob/febfd442cdba343771f478cf433b0892f213ad2f/src/compiler/commandLineParser.ts#L3005 - /** - * @returns Returns undefined in case of lack of result while trying to resolve from node_modules - */ - async function getTsconfigPath(baseDirUri: URI, extendsValue: string): Promise { - // Don't take into account a case, where tsconfig might be resolved from the root (see the reference) - // e.g. C:/projects/shared-tsconfig/tsconfig.json (note that C: prefix is optional) - - const isRelativePath = ['./', '../'].some(str => extendsValue.startsWith(str)); - if (isRelativePath) { - const absolutePath = Utils.joinPath(baseDirUri, extendsValue); - if (await exists(absolutePath) || absolutePath.path.endsWith('.json')) { - return absolutePath; - } - return absolutePath.with({ - path: `${absolutePath.path}.json` - }); - } - - // Otherwise resolve like a module - return resolveNodeModulesPath(baseDirUri, [ - extendsValue, - ...extendsValue.endsWith('.json') ? [] : [ - `${extendsValue}.json`, - `${extendsValue}/tsconfig.json`, - ] - ]); - } - - async function exists(resource: URI): Promise { - const stat = await context.env.fs?.stat(resource.toString()); - // stat.type is an enum flag - return stat?.type === 1 satisfies FileType.File; - } - }, - }; -} diff --git a/packages/tsconfig/package.json b/packages/tsconfig/package.json deleted file mode 100644 index 35a82bc7..00000000 --- a/packages/tsconfig/package.json +++ /dev/null @@ -1,42 +0,0 @@ -{ - "name": "volar-service-tsconfig", - "version": "0.0.30", - "description": "Integrate tsconfig into Volar", - "homepage": "https://github.com/volarjs/services/tree/master/packages/tsconfig", - "bugs": "https://github.com/volarjs/services/issues", - "sideEffects": false, - "keywords": [ - "volar-service" - ], - "license": "MIT", - "files": [ - "**/*.js", - "**/*.d.ts" - ], - "repository": { - "type": "git", - "url": "https://github.com/volarjs/services.git", - "directory": "packages/tsconfig" - }, - "author": { - "name": "Johnson Chu", - "email": "johnsoncodehk@gmail.com", - "url": "https://github.com/johnsoncodehk" - }, - "devDependencies": { - "vscode-languageserver-textdocument": "^1.0.11" - }, - "dependencies": { - "jsonc-parser": "^3.2.0", - "minimatch": "^9.0.3", - "vscode-uri": "^3.0.8" - }, - "peerDependencies": { - "@volar/language-service": "~2.1.0" - }, - "peerDependenciesMeta": { - "@volar/language-service": { - "optional": true - } - } -} diff --git a/packages/tsconfig/tsconfig.json b/packages/tsconfig/tsconfig.json deleted file mode 100644 index 45b22f96..00000000 --- a/packages/tsconfig/tsconfig.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "extends": "../../tsconfig.base.json", - "include": [ "*", "lib/**/*" ], -} diff --git a/readme.md b/readme.md index 8ec917b7..ccea2ef3 100644 --- a/readme.md +++ b/readme.md @@ -12,7 +12,6 @@ | pug | | ✅ | ✅ | ✅ | | | pug-beautify | [pug-beautify](https://github.com/vingorius/pug-beautify) | | | | ✅ | | sass-formatter | [sass-formatter](https://github.com/TheRealSyler/sass-formatter) | | | | ✅ | -| tsconfig | [vscode/typescript-language-features](https://github.com/microsoft/vscode/blob/main/extensions/typescript-language-features/src/languageFeatures/tsconfig.ts) | | ✅ | | | | typescript | [TypeScript](https://github.com/microsoft/TypeScript) | ✅ | ✅ | ✅ | ✅ | | typescript-twoslash-queries | [vscode-twoslash-queries](https://github.com/orta/vscode-twoslash-queries) | | ✅ | | | | vetur | [vls](https://github.com/vuejs/vetur) | | ✅ | | |