Skip to content

Commit

Permalink
fixup! fix(language-service): Add resource files as roots to their as…
Browse files Browse the repository at this point in the history
…sociated projects
  • Loading branch information
atscott committed Apr 19, 2022
1 parent aaeb0e3 commit 6c8fbff
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 12 deletions.
Expand Up @@ -7,6 +7,7 @@
*/
import ts from 'typescript';

import {isShim} from '../../../../compiler-cli/src/ngtsc/shims';
import {IncrementalBuild} from '../../../src/ngtsc/incremental/api';
import {SemanticSymbol} from '../../../src/ngtsc/incremental/semantic_graph';
import {NOOP_PERF_RECORDER} from '../../../src/ngtsc/perf';
Expand All @@ -28,7 +29,7 @@ export class NgccTraitCompiler extends TraitCompiler {
super(
handlers, ngccReflector, NOOP_PERF_RECORDER, new NoIncrementalBuild(),
/* compileNonExportedClasses */ true, CompilationMode.FULL, new DtsTransformRegistry(),
/* semanticDepGraphUpdater */ null);
/* semanticDepGraphUpdater */ null, {isShim, isResource: () => false});
}

get analyzedFiles(): ts.SourceFile[] {
Expand Down
15 changes: 14 additions & 1 deletion packages/compiler-cli/src/ngtsc/core/api/src/adapter.ts
Expand Up @@ -43,7 +43,8 @@ export interface NgCompilerAdapter extends
// incompatible with the `ts.CompilerHost` version which isn't. The combination of these two
// still satisfies `ts.ModuleResolutionHost`.
Omit<ts.ModuleResolutionHost, 'getCurrentDirectory'>,
Pick<ExtendedTsCompilerHost, 'getCurrentDirectory'|ExtendedCompilerHostMethods> {
Pick<ExtendedTsCompilerHost, 'getCurrentDirectory'|ExtendedCompilerHostMethods>,
SourceFileTypeIdentifier {
/**
* A path to a single file which represents the entrypoint of an Angular Package Format library,
* if the current program is one.
Expand Down Expand Up @@ -86,7 +87,9 @@ export interface NgCompilerAdapter extends
* Resolved list of root directories explicitly set in, or inferred from, the tsconfig.
*/
readonly rootDirs: ReadonlyArray<AbsoluteFsPath>;
}

export interface SourceFileTypeIdentifier {
/**
* Distinguishes between shim files added by Angular to the compilation process (both those
* intended for output, like ngfactory files, as well as internal shims like ngtypecheck files)
Expand All @@ -96,4 +99,14 @@ export interface NgCompilerAdapter extends
* `true` if a file was written by the user, and `false` if a file was added by the compiler.
*/
isShim(sf: ts.SourceFile): boolean;

/**
* Distinguishes between resource files added by Angular to the project and original files in the
* user's program.
*
* This is necessary only for the language service because it adds resource files as root files
* when they are read. This is done to indicate to TS Server that these resources are part of the
* project and ensures that projects are retained properly when navigating around the workspace.
*/
isResource(sf: ts.SourceFile): boolean;
}
2 changes: 1 addition & 1 deletion packages/compiler-cli/src/ngtsc/core/src/compiler.ts
Expand Up @@ -1046,7 +1046,7 @@ export class NgCompiler {
const traitCompiler = new TraitCompiler(
handlers, reflector, this.delegatingPerfRecorder, this.incrementalCompilation,
this.options.compileNonExportedClasses !== false, compilationMode, dtsTransforms,
semanticDepGraphUpdater);
semanticDepGraphUpdater, this.adapter);

// Template type-checking may use the `ProgramDriver` to produce new `ts.Program`(s). If this
// happens, they need to be tracked by the `NgCompiler`.
Expand Down
10 changes: 10 additions & 0 deletions packages/compiler-cli/src/ngtsc/core/src/host.ts
Expand Up @@ -234,6 +234,16 @@ export class NgCompilerHost extends DelegatingCompilerHost implements
return isShim(sf);
}

/**
* Check whether the given `ts.SourceFile` is a resource file.
*
* This simply returns `false` for the compiler-cli since resource files are not added as root
* files to the project.
*/
isResource(sf: ts.SourceFile): boolean {
return false;
}

getSourceFile(
fileName: string, languageVersion: ts.ScriptTarget,
onError?: ((message: string) => void)|undefined,
Expand Down
17 changes: 11 additions & 6 deletions packages/compiler-cli/src/ngtsc/transform/src/compilation.ts
Expand Up @@ -9,16 +9,16 @@
import {ConstantPool} from '@angular/compiler';
import ts from 'typescript';

import {SourceFileTypeIdentifier} from '../../core/api';
import {ErrorCode, FatalDiagnosticError} from '../../diagnostics';
import {IncrementalBuild} from '../../incremental/api';
import {SemanticDepGraphUpdater, SemanticSymbol} from '../../incremental/semantic_graph';
import {IndexingContext} from '../../indexer';
import {PerfEvent, PerfRecorder} from '../../perf';
import {ClassDeclaration, DeclarationNode, Decorator, isNamedClassDeclaration, ReflectionHost} from '../../reflection';
import {isShim} from '../../shims';
import {ProgramTypeCheckAdapter, TypeCheckContext} from '../../typecheck/api';
import {ExtendedTemplateChecker} from '../../typecheck/extended/api';
import {getSourceFile, isExported} from '../../util/src/typescript';
import {getSourceFile} from '../../util/src/typescript';
import {Xi18nContext} from '../../xi18n';

import {AnalysisOutput, CompilationMode, CompileResult, DecoratorHandler, HandlerFlags, HandlerPrecedence, ResolveResult} from './api';
Expand Down Expand Up @@ -97,11 +97,15 @@ export class TraitCompiler implements ProgramTypeCheckAdapter {

constructor(
private handlers: DecoratorHandler<unknown, unknown, SemanticSymbol|null, unknown>[],
private reflector: ReflectionHost, private perf: PerfRecorder,
private reflector: ReflectionHost,
private perf: PerfRecorder,
private incrementalBuild: IncrementalBuild<ClassRecord, unknown>,
private compileNonExportedClasses: boolean, private compilationMode: CompilationMode,
private compileNonExportedClasses: boolean,
private compilationMode: CompilationMode,
private dtsTransforms: DtsTransformRegistry,
private semanticDepGraphUpdater: SemanticDepGraphUpdater|null) {
private semanticDepGraphUpdater: SemanticDepGraphUpdater|null,
private sourceFileTypeIdentifier: SourceFileTypeIdentifier,
) {
for (const handler of handlers) {
this.handlersByName.set(handler.name, handler);
}
Expand All @@ -119,7 +123,8 @@ export class TraitCompiler implements ProgramTypeCheckAdapter {
private analyze(sf: ts.SourceFile, preanalyze: true): Promise<void>|undefined;
private analyze(sf: ts.SourceFile, preanalyze: boolean): Promise<void>|undefined {
// We shouldn't analyze declaration, shim, or resource files.
if (sf.isDeclarationFile || isShim(sf) || !sf.fileName.endsWith('.ts')) {
if (sf.isDeclarationFile || this.sourceFileTypeIdentifier.isShim(sf) ||
this.sourceFileTypeIdentifier.isResource(sf)) {
return undefined;
}

Expand Down
Expand Up @@ -8,6 +8,7 @@
import {ConstantPool} from '@angular/compiler';
import * as o from '@angular/compiler/src/output/output_ast';

import {SourceFileTypeIdentifier} from '../../core/api';
import {absoluteFrom} from '../../file_system';
import {runInEachFileSystem} from '../../file_system/testing';
import {NOOP_INCREMENTAL_BUILD} from '../../incremental';
Expand All @@ -17,6 +18,11 @@ import {getDeclaration, makeProgram} from '../../testing';
import {CompilationMode, DetectResult, DtsTransformRegistry, TraitCompiler} from '../../transform';
import {AnalysisOutput, CompileResult, DecoratorHandler, HandlerPrecedence} from '../src/api';

const fakeSfTypeIdentifier: SourceFileTypeIdentifier = {
isShim: () => false,
isResource: () => false
};

runInEachFileSystem(() => {
describe('TraitCompiler', () => {
let _: typeof absoluteFrom;
Expand Down Expand Up @@ -49,7 +55,7 @@ runInEachFileSystem(() => {
const reflectionHost = new TypeScriptReflectionHost(checker);
const compiler = new TraitCompiler(
[new FakeDecoratorHandler()], reflectionHost, NOOP_PERF_RECORDER, NOOP_INCREMENTAL_BUILD,
true, CompilationMode.FULL, new DtsTransformRegistry(), null);
true, CompilationMode.FULL, new DtsTransformRegistry(), null, fakeSfTypeIdentifier);
const sourceFile = program.getSourceFile('lib.d.ts')!;
const analysis = compiler.analyzeSync(sourceFile);

Expand Down Expand Up @@ -138,7 +144,7 @@ runInEachFileSystem(() => {
const compiler = new TraitCompiler(
[new PartialDecoratorHandler(), new FullDecoratorHandler()], reflectionHost,
NOOP_PERF_RECORDER, NOOP_INCREMENTAL_BUILD, true, CompilationMode.PARTIAL,
new DtsTransformRegistry(), null);
new DtsTransformRegistry(), null, fakeSfTypeIdentifier);
const sourceFile = program.getSourceFile('test.ts')!;
compiler.analyzeSync(sourceFile);
compiler.resolve();
Expand Down Expand Up @@ -168,7 +174,7 @@ runInEachFileSystem(() => {
const compiler = new TraitCompiler(
[new PartialDecoratorHandler(), new FullDecoratorHandler()], reflectionHost,
NOOP_PERF_RECORDER, NOOP_INCREMENTAL_BUILD, true, CompilationMode.FULL,
new DtsTransformRegistry(), null);
new DtsTransformRegistry(), null, fakeSfTypeIdentifier);
const sourceFile = program.getSourceFile('test.ts')!;
compiler.analyzeSync(sourceFile);
compiler.resolve();
Expand Down
5 changes: 5 additions & 0 deletions packages/language-service/src/adapters.ts
Expand Up @@ -61,6 +61,11 @@ export class LanguageServiceAdapter implements NgCompilerAdapter {
return isShim(sf);
}

isResource(sf: ts.SourceFile): boolean {
const scriptInfo = this.project.getScriptInfo(sf.fileName);
return scriptInfo?.scriptKind === ts.ScriptKind.Unknown;
}

fileExists(fileName: string): boolean {
return this.project.fileExists(fileName);
}
Expand Down

0 comments on commit 6c8fbff

Please sign in to comment.