Skip to content

Commit

Permalink
Fix incremental api for vue files (TypeStrong#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfspiritM committed Mar 4, 2019
1 parent 547d087 commit 6d74947
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 103 deletions.
6 changes: 4 additions & 2 deletions src/ApiIncrementalChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export class ApiIncrementalChecker implements IncrementalCheckerInterface {
private context: string,
private linterConfigFile: string | boolean,
private linterAutoFix: boolean,
checkSyntacticErrors: boolean
checkSyntacticErrors: boolean,
private vue: boolean = false
) {
this.hasFixedConfig = typeof this.linterConfigFile === 'string';

Expand All @@ -51,7 +52,8 @@ export class ApiIncrementalChecker implements IncrementalCheckerInterface {
typescript,
programConfigFile,
compilerOptions,
checkSyntacticErrors
checkSyntacticErrors,
this.vue
);
}

Expand Down
72 changes: 41 additions & 31 deletions src/CompilerHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ export class CompilerHost
return this.knownFiles;
}

public resolveModuleNames?(
moduleNames: string[],
containingFile: string,
reusedNames?: string[],
redirectedReference?: ts.ResolvedProjectReference
): (ts.ResolvedModule | undefined)[];

public configFileName: string;
public optionsToExtend: ts.CompilerOptions;

Expand All @@ -55,23 +62,38 @@ export class CompilerHost
private typescript: typeof ts,
programConfigFile: string,
compilerOptions: ts.CompilerOptions,
checkSyntacticErrors: boolean
checkSyntacticErrors: boolean,
private vue: boolean = false
) {
this.tsHost = typescript.createWatchCompilerHost(
programConfigFile,
compilerOptions,
typescript.sys,
typescript.createEmitAndSemanticDiagnosticsBuilderProgram,
(diag: ts.Diagnostic) => {
if (!checkSyntacticErrors && diag.code >= 1000 && diag.code < 2000) {
return;
}
this.gatheredDiagnostic.push(diag);
},
() => {
// do nothing
const diagCallback = (diag: ts.Diagnostic) => {
if (!checkSyntacticErrors && diag.code >= 1000 && diag.code < 2000) {
return;
}
);
this.gatheredDiagnostic.push(diag);
};

if (this.vue) {
this.tsHost = VueProgram.createWatchCompilerHost(
typescript,
programConfigFile,
compilerOptions,
diagCallback
);

this.createProgram = this.tsHost.createProgram;
this.resolveModuleNames = this.tsHost.resolveModuleNames;
} else {
this.tsHost = typescript.createWatchCompilerHost(
programConfigFile,
compilerOptions,
typescript.sys,
typescript.createEmitAndSemanticDiagnosticsBuilderProgram,
diagCallback,
() => {
// do nothing
}
);
}

this.configFileName = this.tsHost.configFileName;
this.optionsToExtend = this.tsHost.optionsToExtend || {};
Expand Down Expand Up @@ -259,13 +281,6 @@ export class CompilerHost

public readFile(path: string, encoding?: string) {
const content = this.tsHost.readFile(path, encoding);

// get typescript contents from Vue file
if (content && VueProgram.isVue(path)) {
const resolved = VueProgram.resolveScriptBlock(this.typescript, content);
return resolved.content;
}

return content;
}

Expand All @@ -289,17 +304,12 @@ export class CompilerHost
include?: ReadonlyArray<string>,
depth?: number
): string[] {
return this.typescript.sys.readDirectory(
path,
extensions,
exclude,
include,
depth
);
return this.tsHost.readDirectory(path, extensions, exclude, include, depth);
}

public createProgram = this.typescript
.createEmitAndSemanticDiagnosticsBuilderProgram;
public createProgram: ts.CreateProgram<
ts.EmitAndSemanticDiagnosticsBuilderProgram
> = this.typescript.createEmitAndSemanticDiagnosticsBuilderProgram;

public getCurrentDirectory(): string {
return this.tsHost.getCurrentDirectory();
Expand Down
Loading

0 comments on commit 6d74947

Please sign in to comment.