Skip to content

Commit

Permalink
fix: ensure typescript is imported only as type-only (#633)
Browse files Browse the repository at this point in the history
Ensure typescript is imported only as type-only and close reporter on unsuccessful `configure` or `connect`

Co-authored-by: Mark Molinaro <[email protected]>
  • Loading branch information
piotr-oles and Mark Molinaro authored Jul 23, 2021
1 parent 8d8125c commit d660ad1
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 16 deletions.
12 changes: 10 additions & 2 deletions src/reporter/reporter-rpc/ReporterRpcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,16 @@ function createReporterRpcClient<TConfiguration extends object>(
await channel.open();
}
if (!rpcClient.isConnected()) {
await rpcClient.connect();
await rpcClient.dispatchCall(configure, configuration);
try {
await rpcClient.connect();
await rpcClient.dispatchCall(configure, configuration);
} catch (error) {
// connect or configure was not successful -
// close the reporter and re-throw an error
await rpcClient.disconnect();
await channel.close();
throw error;
}
}
},
disconnect: async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import type * as ts from 'typescript';
import { extname } from 'path';
import { TypeScriptExtension } from './TypeScriptExtension';
import { Issue } from '../../issue';
Expand Down
2 changes: 1 addition & 1 deletion src/typescript-reporter/extension/TypeScriptExtension.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import type * as ts from 'typescript';
import { Issue } from '../../issue';
import { FilesMatch } from '../../reporter';

Expand Down
2 changes: 1 addition & 1 deletion src/typescript-reporter/issue/TypeScriptIssueFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import type * as ts from 'typescript';
import * as os from 'os';
import { deduplicateAndSortIssues, Issue, IssueLocation } from '../../issue';

Expand Down
2 changes: 1 addition & 1 deletion src/typescript-reporter/profile/TypeScriptPerformance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import type * as ts from 'typescript';
import { Performance } from '../../profile/Performance';

interface TypeScriptPerformance {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import type * as ts from 'typescript';
import { dirname, join } from 'path';
import { createPassiveFileSystem } from '../file-system/PassiveFileSystem';
import forwardSlash from '../../utils/path/forwardSlash';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import type * as ts from 'typescript';
import { TypeScriptHostExtension } from '../extension/TypeScriptExtension';
import { ControlledTypeScriptSystem } from './ControlledTypeScriptSystem';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import type * as ts from 'typescript';
import { createControlledWatchCompilerHost } from './ControlledWatchCompilerHost';
import { TypeScriptHostExtension } from '../extension/TypeScriptExtension';
import { ControlledTypeScriptSystem } from './ControlledTypeScriptSystem';
Expand Down Expand Up @@ -59,7 +59,7 @@ function createControlledWatchSolutionBuilderHost<TProgram extends ts.BuilderPro
system.deleteFile(fileName);
},
getParsedCommandLine(fileName: string): ts.ParsedCommandLine | undefined {
return ts.getParsedCommandLineOfConfigFile(
return typescript.getParsedCommandLineOfConfigFile(
fileName,
{ skipLibCheck: true },
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import type * as ts from 'typescript';
import { normalize, dirname, basename, resolve, relative } from 'path';
import { TypeScriptConfigurationOverwrite } from '../TypeScriptConfigurationOverwrite';
import { FilesMatch } from '../../reporter';
Expand Down Expand Up @@ -114,10 +114,10 @@ function removeJsonExtension(path: string) {
}
}

function getTsBuildInfoEmitOutputFilePath(options: ts.CompilerOptions) {
if (typeof ts.getTsBuildInfoEmitOutputFilePath === 'function') {
function getTsBuildInfoEmitOutputFilePath(typescript: typeof ts, options: ts.CompilerOptions) {
if (typeof typescript.getTsBuildInfoEmitOutputFilePath === 'function') {
// old TypeScript version doesn't provides this method
return ts.getTsBuildInfoEmitOutputFilePath(options);
return typescript.getTsBuildInfoEmitOutputFilePath(options);
}

// based on the implementation from typescript
Expand Down Expand Up @@ -159,7 +159,10 @@ function getArtifactsFromTypeScriptConfiguration(
if (parsedConfiguration.options.outFile) {
files.add(resolve(configFileContext, parsedConfiguration.options.outFile));
}
const tsBuildInfoPath = getTsBuildInfoEmitOutputFilePath(parsedConfiguration.options);
const tsBuildInfoPath = getTsBuildInfoEmitOutputFilePath(
typescript,
parsedConfiguration.options
);
if (tsBuildInfoPath) {
files.add(resolve(configFileContext, tsBuildInfoPath));
}
Expand Down
2 changes: 1 addition & 1 deletion src/typescript-reporter/reporter/TypeScriptReporter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ts from 'typescript';
import type * as ts from 'typescript';
import path from 'path';
import { FilesMatch, Reporter } from '../../reporter';
import { createIssuesFromTsDiagnostics } from '../issue/TypeScriptIssueFactory';
Expand Down

0 comments on commit d660ad1

Please sign in to comment.