From ebc92e9bb7334121f2b2eea23e670feddeff3bf7 Mon Sep 17 00:00:00 2001 From: Benny Powers Date: Sun, 9 Jul 2023 15:13:07 +0300 Subject: [PATCH 1/8] fix: derive files from tsconfigs which extend Fixes #3999 --- .../labs/analyzer/src/lib/analyze-package.ts | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/labs/analyzer/src/lib/analyze-package.ts b/packages/labs/analyzer/src/lib/analyze-package.ts index a353b63d71..1a9724df1d 100644 --- a/packages/labs/analyzer/src/lib/analyze-package.ts +++ b/packages/labs/analyzer/src/lib/analyze-package.ts @@ -4,7 +4,7 @@ * SPDX-License-Identifier: BSD-3-Clause */ -import ts from 'typescript'; +import ts, {type ParseConfigFileHost} from 'typescript'; import {AbsolutePath} from './paths.js'; import * as path from 'path'; import {DiagnosticsError} from './errors.js'; @@ -44,13 +44,20 @@ export const createPackageAnalyzer = ( if (options.exclude !== undefined) { (configFile.config.exclude ??= []).push(...options.exclude); } - commandLine = ts.parseJsonConfigFileContent( - configFile.config /* json */, - ts.sys /* host */, - packagePath /* basePath */, - undefined /* existingOptions */, - path.relative(packagePath, configFileName) /* configFileName */ + const maybeCommandLine = ts.getParsedCommandLineOfConfigFile( + configFileName /* json */, + {} /* optionsToExtend */, + // typescript themselves bypass this check 🤷 + // SEE: https://github.com/microsoft/TypeScript/blob/9701f55f7217d7bd56d28e73b250444efcefa8dd/src/compiler/watch.ts#L217 + ts.sys as unknown as ParseConfigFileHost /* host */ ); + if (maybeCommandLine) { + commandLine = maybeCommandLine; + } else { + throw new Error( + `Could not parse tsconfig at specified path '${packagePath}'.` + ); + } } else if (isDirectory) { console.info(`No tsconfig.json found; assuming package is JavaScript.`); commandLine = ts.parseJsonConfigFileContent( From 68f474c670f8f47d65e141c5c3389d6293c3a205 Mon Sep 17 00:00:00 2001 From: Benny Powers Date: Sun, 9 Jul 2023 15:21:25 +0300 Subject: [PATCH 2/8] docs: changelog --- .changeset/analyzer-tsconfig-extends.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/analyzer-tsconfig-extends.md diff --git a/.changeset/analyzer-tsconfig-extends.md b/.changeset/analyzer-tsconfig-extends.md new file mode 100644 index 0000000000..7cf31bb765 --- /dev/null +++ b/.changeset/analyzer-tsconfig-extends.md @@ -0,0 +1,5 @@ +--- +'@lit-labs/analyzer': minor +--- + +Detect sources when the `tsconfig.json` `extends` from another config. From 1aaa749a051a654ec87962833978a74da6e1a2f5 Mon Sep 17 00:00:00 2001 From: Benny Powers Date: Sun, 9 Jul 2023 16:06:21 +0300 Subject: [PATCH 3/8] fix: parse config as object --- .../labs/analyzer/src/lib/analyze-package.ts | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/packages/labs/analyzer/src/lib/analyze-package.ts b/packages/labs/analyzer/src/lib/analyze-package.ts index 1a9724df1d..5e3761a15a 100644 --- a/packages/labs/analyzer/src/lib/analyze-package.ts +++ b/packages/labs/analyzer/src/lib/analyze-package.ts @@ -4,7 +4,7 @@ * SPDX-License-Identifier: BSD-3-Clause */ -import ts, {type ParseConfigFileHost} from 'typescript'; +import ts, {CompilerOptions} from 'typescript'; import {AbsolutePath} from './paths.js'; import * as path from 'path'; import {DiagnosticsError} from './errors.js'; @@ -41,23 +41,17 @@ export const createPackageAnalyzer = ( let commandLine: ts.ParsedCommandLine; if (ts.sys.fileExists(configFileName)) { const configFile = ts.readConfigFile(configFileName, ts.sys.readFile); + const existingOptions: CompilerOptions = {}; if (options.exclude !== undefined) { - (configFile.config.exclude ??= []).push(...options.exclude); + existingOptions.exclude = options.exclude; } - const maybeCommandLine = ts.getParsedCommandLineOfConfigFile( - configFileName /* json */, - {} /* optionsToExtend */, - // typescript themselves bypass this check 🤷 - // SEE: https://github.com/microsoft/TypeScript/blob/9701f55f7217d7bd56d28e73b250444efcefa8dd/src/compiler/watch.ts#L217 - ts.sys as unknown as ParseConfigFileHost /* host */ + commandLine = ts.parseJsonConfigFileContent( + configFile.config /* json */, + ts.sys, + packagePath, + existingOptions, + configFileName ); - if (maybeCommandLine) { - commandLine = maybeCommandLine; - } else { - throw new Error( - `Could not parse tsconfig at specified path '${packagePath}'.` - ); - } } else if (isDirectory) { console.info(`No tsconfig.json found; assuming package is JavaScript.`); commandLine = ts.parseJsonConfigFileContent( From f943def1d9a0d06c5c95699dbb00e8fa4403c560 Mon Sep 17 00:00:00 2001 From: Benny Powers Date: Sun, 9 Jul 2023 16:06:45 +0300 Subject: [PATCH 4/8] docs: changeset level --- .changeset/analyzer-tsconfig-extends.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/analyzer-tsconfig-extends.md b/.changeset/analyzer-tsconfig-extends.md index 7cf31bb765..c5e0469e63 100644 --- a/.changeset/analyzer-tsconfig-extends.md +++ b/.changeset/analyzer-tsconfig-extends.md @@ -1,5 +1,5 @@ --- -'@lit-labs/analyzer': minor +'@lit-labs/analyzer': patch --- Detect sources when the `tsconfig.json` `extends` from another config. From 92c0594d3c0f86589883e8b7482d5a00bfaf6e90 Mon Sep 17 00:00:00 2001 From: Benny Powers Date: Sun, 9 Jul 2023 17:10:21 +0300 Subject: [PATCH 5/8] fix: excludes --- packages/labs/analyzer/src/lib/analyze-package.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/labs/analyzer/src/lib/analyze-package.ts b/packages/labs/analyzer/src/lib/analyze-package.ts index 5e3761a15a..410ad3e6cf 100644 --- a/packages/labs/analyzer/src/lib/analyze-package.ts +++ b/packages/labs/analyzer/src/lib/analyze-package.ts @@ -41,16 +41,19 @@ export const createPackageAnalyzer = ( let commandLine: ts.ParsedCommandLine; if (ts.sys.fileExists(configFileName)) { const configFile = ts.readConfigFile(configFileName, ts.sys.readFile); - const existingOptions: CompilerOptions = {}; + // TODO: handle configFile.errors if (options.exclude !== undefined) { - existingOptions.exclude = options.exclude; + configFile.config.exclude = [ + ...(configFile.config.exclude ?? []), + options.exclude, + ]; } commandLine = ts.parseJsonConfigFileContent( configFile.config /* json */, - ts.sys, - packagePath, - existingOptions, - configFileName + ts.sys /* host */, + packagePath /* basePath */, + {} /* existingOptions */, + configFileName /* configFileName */ ); } else if (isDirectory) { console.info(`No tsconfig.json found; assuming package is JavaScript.`); From 10a973b9b7c5577afdaa70ae50239e6a4c02d95e Mon Sep 17 00:00:00 2001 From: Benny Powers Date: Sun, 9 Jul 2023 18:03:59 +0300 Subject: [PATCH 6/8] fix: spread the excludes --- packages/labs/analyzer/src/lib/analyze-package.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/labs/analyzer/src/lib/analyze-package.ts b/packages/labs/analyzer/src/lib/analyze-package.ts index 410ad3e6cf..a06b313b40 100644 --- a/packages/labs/analyzer/src/lib/analyze-package.ts +++ b/packages/labs/analyzer/src/lib/analyze-package.ts @@ -4,7 +4,7 @@ * SPDX-License-Identifier: BSD-3-Clause */ -import ts, {CompilerOptions} from 'typescript'; +import ts from 'typescript'; import {AbsolutePath} from './paths.js'; import * as path from 'path'; import {DiagnosticsError} from './errors.js'; @@ -45,7 +45,7 @@ export const createPackageAnalyzer = ( if (options.exclude !== undefined) { configFile.config.exclude = [ ...(configFile.config.exclude ?? []), - options.exclude, + ...options.exclude, ]; } commandLine = ts.parseJsonConfigFileContent( From a82b0e7862fa3a77065f9574b01ec2c898b962d5 Mon Sep 17 00:00:00 2001 From: Benny Powers Date: Sun, 9 Jul 2023 18:45:15 +0300 Subject: [PATCH 7/8] chore: revert syntactic changes --- packages/labs/analyzer/src/lib/analyze-package.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/labs/analyzer/src/lib/analyze-package.ts b/packages/labs/analyzer/src/lib/analyze-package.ts index a06b313b40..fe6b22153e 100644 --- a/packages/labs/analyzer/src/lib/analyze-package.ts +++ b/packages/labs/analyzer/src/lib/analyze-package.ts @@ -41,12 +41,8 @@ export const createPackageAnalyzer = ( let commandLine: ts.ParsedCommandLine; if (ts.sys.fileExists(configFileName)) { const configFile = ts.readConfigFile(configFileName, ts.sys.readFile); - // TODO: handle configFile.errors if (options.exclude !== undefined) { - configFile.config.exclude = [ - ...(configFile.config.exclude ?? []), - ...options.exclude, - ]; + (configFile.config.exclude ??= []).push(...options.exclude); } commandLine = ts.parseJsonConfigFileContent( configFile.config /* json */, From 04dbe7aee0eb07294ea44e7183dd41e927194c0b Mon Sep 17 00:00:00 2001 From: Benny Powers Date: Mon, 10 Jul 2023 09:52:10 +0300 Subject: [PATCH 8/8] fix: use correct base path when building commandline this change reduced the number of errors when analyzing patternfly-elements from 152 to 79 --- packages/labs/analyzer/src/lib/analyze-package.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/labs/analyzer/src/lib/analyze-package.ts b/packages/labs/analyzer/src/lib/analyze-package.ts index fe6b22153e..83477a3963 100644 --- a/packages/labs/analyzer/src/lib/analyze-package.ts +++ b/packages/labs/analyzer/src/lib/analyze-package.ts @@ -47,7 +47,7 @@ export const createPackageAnalyzer = ( commandLine = ts.parseJsonConfigFileContent( configFile.config /* json */, ts.sys /* host */, - packagePath /* basePath */, + isDirectory ? packagePath : path.dirname(packagePath) /* basePath */, {} /* existingOptions */, configFileName /* configFileName */ ); @@ -99,7 +99,9 @@ export const createPackageAnalyzer = ( const program = ts.createProgram( commandLine.fileNames, commandLine.options, - compilerHost + compilerHost, + undefined, + commandLine.errors ); const analyzer = new Analyzer({getProgram: () => program, fs: ts.sys, path});