From 78acea703b3112243d8a73eb72835709332cb9e2 Mon Sep 17 00:00:00 2001 From: Brody McKee Date: Sun, 19 Feb 2023 14:21:51 +1100 Subject: [PATCH] WIP --- package.json | 2 +- pnpm-lock.yaml | 60 +++++++++++++++++------------------ src/helpers/getDtsSnapshot.ts | 2 +- src/helpers/logger.ts | 4 ++- src/index.ts | 17 ++++++---- 5 files changed, 46 insertions(+), 39 deletions(-) diff --git a/package.json b/package.json index 278d7b8..e184779 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,7 @@ "prettier": "^2.8.4", "sass-svg": "^1.2.0", "ts-jest": "^29.0.5", - "typescript": "^4.9.5" + "typescript": "5.0.0-beta" }, "peerDependencies": { "typescript": ">=3.9.0" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9bcd6d0..bd3da99 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -39,7 +39,7 @@ specifiers: stylus: ^0.59.0 ts-jest: ^29.0.5 tsconfig-paths: ^4.1.2 - typescript: ^4.9.5 + typescript: 5.0.0-beta dependencies: '@types/postcss-modules-local-by-default': 4.0.0 @@ -67,8 +67,8 @@ devDependencies: '@types/reserved-words': 0.1.0 '@types/sass': 1.43.1 '@types/stylus': 0.48.38 - '@typescript-eslint/eslint-plugin': 5.52.0_6cfvjsbua5ptj65675bqcn6oza - '@typescript-eslint/parser': 5.52.0_7kw3g6rralp5ps6mg3uyzz6azm + '@typescript-eslint/eslint-plugin': 5.52.0_s4j7kgoalzoa554rncng25vpvy + '@typescript-eslint/parser': 5.52.0_ib2mota3gtssswgpztbt7y64nm bootstrap: 5.2.3 eslint: 8.34.0 eslint-config-prettier: 8.6.0_eslint@8.34.0 @@ -81,8 +81,8 @@ devDependencies: postcss-preset-env: 8.0.1_postcss@8.4.21 prettier: 2.8.4 sass-svg: 1.2.0 - ts-jest: 29.0.5_orzzknleilowtsz34rkaotjvzm - typescript: 4.9.5 + ts-jest: 29.0.5_5dldq2zudxxoi4cwkqyfusqsrm + typescript: 5.0.0-beta packages: @@ -1186,7 +1186,7 @@ packages: '@types/yargs-parser': 13.1.0 dev: true - /@typescript-eslint/eslint-plugin/5.52.0_6cfvjsbua5ptj65675bqcn6oza: + /@typescript-eslint/eslint-plugin/5.52.0_s4j7kgoalzoa554rncng25vpvy: resolution: {integrity: sha512-lHazYdvYVsBokwCdKOppvYJKaJ4S41CgKBcPvyd0xjZNbvQdhn/pnJlGtQksQ/NhInzdaeaSarlBjDXHuclEbg==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} peerDependencies: @@ -1197,10 +1197,10 @@ packages: typescript: optional: true dependencies: - '@typescript-eslint/parser': 5.52.0_7kw3g6rralp5ps6mg3uyzz6azm + '@typescript-eslint/parser': 5.52.0_ib2mota3gtssswgpztbt7y64nm '@typescript-eslint/scope-manager': 5.52.0 - '@typescript-eslint/type-utils': 5.52.0_7kw3g6rralp5ps6mg3uyzz6azm - '@typescript-eslint/utils': 5.52.0_7kw3g6rralp5ps6mg3uyzz6azm + '@typescript-eslint/type-utils': 5.52.0_ib2mota3gtssswgpztbt7y64nm + '@typescript-eslint/utils': 5.52.0_ib2mota3gtssswgpztbt7y64nm debug: 4.3.4 eslint: 8.34.0 grapheme-splitter: 1.0.4 @@ -1208,13 +1208,13 @@ packages: natural-compare-lite: 1.4.0 regexpp: 3.2.0 semver: 7.3.8 - tsutils: 3.21.0_typescript@4.9.5 - typescript: 4.9.5 + tsutils: 3.21.0_typescript@5.0.0-beta + typescript: 5.0.0-beta transitivePeerDependencies: - supports-color dev: true - /@typescript-eslint/parser/5.52.0_7kw3g6rralp5ps6mg3uyzz6azm: + /@typescript-eslint/parser/5.52.0_ib2mota3gtssswgpztbt7y64nm: resolution: {integrity: sha512-e2KiLQOZRo4Y0D/b+3y08i3jsekoSkOYStROYmPUnGMEoA0h+k2qOH5H6tcjIc68WDvGwH+PaOrP1XRzLJ6QlA==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} peerDependencies: @@ -1226,10 +1226,10 @@ packages: dependencies: '@typescript-eslint/scope-manager': 5.52.0 '@typescript-eslint/types': 5.52.0 - '@typescript-eslint/typescript-estree': 5.52.0_typescript@4.9.5 + '@typescript-eslint/typescript-estree': 5.52.0_typescript@5.0.0-beta debug: 4.3.4 eslint: 8.34.0 - typescript: 4.9.5 + typescript: 5.0.0-beta transitivePeerDependencies: - supports-color dev: true @@ -1242,7 +1242,7 @@ packages: '@typescript-eslint/visitor-keys': 5.52.0 dev: true - /@typescript-eslint/type-utils/5.52.0_7kw3g6rralp5ps6mg3uyzz6azm: + /@typescript-eslint/type-utils/5.52.0_ib2mota3gtssswgpztbt7y64nm: resolution: {integrity: sha512-tEKuUHfDOv852QGlpPtB3lHOoig5pyFQN/cUiZtpw99D93nEBjexRLre5sQZlkMoHry/lZr8qDAt2oAHLKA6Jw==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} peerDependencies: @@ -1252,12 +1252,12 @@ packages: typescript: optional: true dependencies: - '@typescript-eslint/typescript-estree': 5.52.0_typescript@4.9.5 - '@typescript-eslint/utils': 5.52.0_7kw3g6rralp5ps6mg3uyzz6azm + '@typescript-eslint/typescript-estree': 5.52.0_typescript@5.0.0-beta + '@typescript-eslint/utils': 5.52.0_ib2mota3gtssswgpztbt7y64nm debug: 4.3.4 eslint: 8.34.0 - tsutils: 3.21.0_typescript@4.9.5 - typescript: 4.9.5 + tsutils: 3.21.0_typescript@5.0.0-beta + typescript: 5.0.0-beta transitivePeerDependencies: - supports-color dev: true @@ -1267,7 +1267,7 @@ packages: engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} dev: true - /@typescript-eslint/typescript-estree/5.52.0_typescript@4.9.5: + /@typescript-eslint/typescript-estree/5.52.0_typescript@5.0.0-beta: resolution: {integrity: sha512-WeWnjanyEwt6+fVrSR0MYgEpUAuROxuAH516WPjUblIrClzYJj0kBbjdnbQXLpgAN8qbEuGywiQsXUVDiAoEuQ==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} peerDependencies: @@ -1282,13 +1282,13 @@ packages: globby: 11.1.0 is-glob: 4.0.3 semver: 7.3.8 - tsutils: 3.21.0_typescript@4.9.5 - typescript: 4.9.5 + tsutils: 3.21.0_typescript@5.0.0-beta + typescript: 5.0.0-beta transitivePeerDependencies: - supports-color dev: true - /@typescript-eslint/utils/5.52.0_7kw3g6rralp5ps6mg3uyzz6azm: + /@typescript-eslint/utils/5.52.0_ib2mota3gtssswgpztbt7y64nm: resolution: {integrity: sha512-As3lChhrbwWQLNk2HC8Ree96hldKIqk98EYvypd3It8Q1f8d5zWyIoaZEp2va5667M4ZyE7X8UUR+azXrFl+NA==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} peerDependencies: @@ -1298,7 +1298,7 @@ packages: '@types/semver': 7.3.13 '@typescript-eslint/scope-manager': 5.52.0 '@typescript-eslint/types': 5.52.0 - '@typescript-eslint/typescript-estree': 5.52.0_typescript@4.9.5 + '@typescript-eslint/typescript-estree': 5.52.0_typescript@5.0.0-beta eslint: 8.34.0 eslint-scope: 5.1.1 eslint-utils: 3.0.0_eslint@8.34.0 @@ -4323,7 +4323,7 @@ packages: dependencies: is-number: 7.0.0 - /ts-jest/29.0.5_orzzknleilowtsz34rkaotjvzm: + /ts-jest/29.0.5_5dldq2zudxxoi4cwkqyfusqsrm: resolution: {integrity: sha512-PL3UciSgIpQ7f6XjVOmbi96vmDHUqAyqDr8YxzopDqX3kfgYtX1cuNeBjP+L9sFXi6nzsGGA6R3fP3DDDJyrxA==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} hasBin: true @@ -4352,7 +4352,7 @@ packages: lodash.memoize: 4.1.2 make-error: 1.3.5 semver: 7.3.8 - typescript: 4.9.5 + typescript: 5.0.0-beta yargs-parser: 21.1.1 dev: true @@ -4372,14 +4372,14 @@ packages: /tslib/2.4.1: resolution: {integrity: sha512-tGyy4dAjRIEwI7BzsB0lynWgOpfqjUdq91XXAlIWD2OwKBH7oCl/GZG/HT4BOHrTlPMOASlMQ7veyTqpmRcrNA==} - /tsutils/3.21.0_typescript@4.9.5: + /tsutils/3.21.0_typescript@5.0.0-beta: resolution: {integrity: sha512-mHKK3iUXL+3UF6xL5k0PEhKRUBKPBCv/+RkEOpjRWxxx27KKRBmmA60A9pgOUvMi8GKhRMPEmjBRPzs2W7O1OA==} engines: {node: '>= 6'} peerDependencies: typescript: '>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta' dependencies: tslib: 1.11.0 - typescript: 4.9.5 + typescript: 5.0.0-beta dev: true /type-check/0.4.0: @@ -4404,8 +4404,8 @@ packages: engines: {node: '>=10'} dev: true - /typescript/4.9.5: - resolution: {integrity: sha512-1FXk9E2Hm+QzZQ7z+McJiHL4NW1F2EzMu9Nq9i3zAaGqibafqYwCVU6WyWAuyQRRzOlxou8xZSyXLEN8oKj24g==} + /typescript/5.0.0-beta: + resolution: {integrity: sha512-+SSabbSXG5mtF+QGdV9uXXt9Saq1cSyI6hSG7znhaLoquleJpnmfkwSxFngK9c2fWWi1W/263TuzXQOsIcdjjA==} engines: {node: '>=4.2.0'} hasBin: true dev: true diff --git a/src/helpers/getDtsSnapshot.ts b/src/helpers/getDtsSnapshot.ts index a14ca7f..5a74172 100644 --- a/src/helpers/getDtsSnapshot.ts +++ b/src/helpers/getDtsSnapshot.ts @@ -9,7 +9,7 @@ export const getDtsSnapshot = ( ts: typeof tsModule, processor: Processor, fileName: string, - scriptSnapshot: ts.IScriptSnapshot, + scriptSnapshot: tsModule.IScriptSnapshot, options: Options, logger: Logger, compilerOptions: tsModule.CompilerOptions, diff --git a/src/helpers/logger.ts b/src/helpers/logger.ts index a877f86..11a1a39 100644 --- a/src/helpers/logger.ts +++ b/src/helpers/logger.ts @@ -1,9 +1,11 @@ +import { server } from 'typescript/lib/tsserverlibrary'; + export interface Logger { log: (message: string) => void; error: (error: unknown) => void; } -export const createLogger = (info: ts.server.PluginCreateInfo): Logger => { +export const createLogger = (info: server.PluginCreateInfo): Logger => { const log = (message: string) => { info.project.projectService.logger.info( `[typescript-plugin-css-modules] ${message}`, diff --git a/src/index.ts b/src/index.ts index 06fa531..6364a87 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,7 @@ import path from 'path'; import dotenv from 'dotenv'; import { AcceptedPlugin } from 'postcss'; import postcssrc from 'postcss-load-config'; -import tsModule from 'typescript/lib/tsserverlibrary'; +import tsModule, { server } from 'typescript/lib/tsserverlibrary'; import { Options } from './options'; import { createMatchers } from './helpers/createMatchers'; import { isCSSFn } from './helpers/cssExtensions'; @@ -11,6 +11,7 @@ import { getDtsSnapshot } from './helpers/getDtsSnapshot'; import { createLogger } from './helpers/logger'; import { getProcessor } from './helpers/getProcessor'; import { filterPlugins } from './helpers/filterPlugins'; +import { LanguageService, SourceFile } from 'typescript'; const getPostCssConfigPlugins = (directory: string) => { try { @@ -23,7 +24,7 @@ const getPostCssConfigPlugins = (directory: string) => { function init({ typescript: ts }: { typescript: typeof tsModule }) { let _isCSS: isCSSFn; - function create(info: ts.server.PluginCreateInfo) { + function create(info: server.PluginCreateInfo): LanguageService { const logger = createLogger(info); const directory = info.project.getCurrentDirectory(); const compilerOptions = info.project.getCompilerOptions(); @@ -107,7 +108,7 @@ function init({ typescript: ts }: { typescript: typeof tsModule }) { fileName, scriptSnapshot, ...rest - ): ts.SourceFile => { + ): SourceFile => { if (isCSS(fileName)) { scriptSnapshot = getDtsSnapshot( ts, @@ -137,7 +138,7 @@ function init({ typescript: ts }: { typescript: typeof tsModule }) { sourceFile, scriptSnapshot, ...rest - ): ts.SourceFile => { + ): SourceFile => { if (isCSS(sourceFile.fileName)) { scriptSnapshot = getDtsSnapshot( ts, @@ -189,11 +190,15 @@ function init({ typescript: ts }: { typescript: typeof tsModule }) { moduleName, ), }; - } else if (isCSS(moduleName)) { + } else if ( + isCSS(moduleName) && + info.languageServiceHost + .getResolvedModuleWithFailedLookupLocationsFromCache + ) { // TODO: Move this section to a separate file and add basic tests. // Attempts to locate the module using TypeScript's previous search paths. These include "baseUrl" and "paths". const failedModule = - info.project.getResolvedModuleWithFailedLookupLocationsFromCache( + info.languageServiceHost.getResolvedModuleWithFailedLookupLocationsFromCache( moduleName, containingFile, );