fix(language-service): Add plugin option to force strictTemplates (#41063)

This commit adds a new configuration option, `forceStrictTemplates` to the
language service plugin to allow users to force enable `strictTemplates`.

This is needed so that the Angular extension can be used inside Google without
changing the underlying compiler options in the `ng_module` build rule.

PR Close #41063
This commit is contained in:
Keen Yee Liau
2021-03-02 15:46:11 -08:00
committed by Zach Arend
parent ef87953bc6
commit 95f748c238
9 changed files with 75 additions and 12 deletions
+18
View File
@@ -14,6 +14,24 @@
import * as ts from 'typescript';
export interface PluginConfig {
/**
* If true, return only Angular results. Otherwise, return Angular + TypeScript
* results.
*/
angularOnly: boolean;
/**
* If true, return factory function for Ivy LS during plugin initialization.
* Otherwise return factory function for View Engine LS.
*/
ivy: boolean;
/**
* If true, enable `strictTemplates` in Angular compiler options regardless
* of its value in tsconfig.json.
*/
forceStrictTemplates?: true;
}
export type GetTcbResponse = {
/**
* The filename of the SourceFile this typecheck block belongs to.
@@ -27,6 +27,14 @@ import {getTargetAtPosition, TargetContext, TargetNodeKind} from './template_tar
import {findTightestNode, getClassDeclFromDecoratorProp, getPropertyAssignmentFromValue} from './ts_utils';
import {getTemplateInfoAtPosition, isTypeScriptFile} from './utils';
interface LanguageServiceConfig {
/**
* If true, enable `strictTemplates` in Angular compiler options regardless
* of its value in tsconfig.json.
*/
forceStrictTemplates?: true;
}
export class LanguageService {
private options: CompilerOptions;
readonly compilerFactory: CompilerFactory;
@@ -35,9 +43,12 @@ export class LanguageService {
private readonly parseConfigHost: LSParseConfigHost;
constructor(
private readonly project: ts.server.Project, private readonly tsLS: ts.LanguageService) {
private readonly project: ts.server.Project,
private readonly tsLS: ts.LanguageService,
private readonly config: LanguageServiceConfig,
) {
this.parseConfigHost = new LSParseConfigHost(project.projectService.host);
this.options = parseNgCompilerOptions(project, this.parseConfigHost);
this.options = parseNgCompilerOptions(project, this.parseConfigHost, config);
logCompilerOptions(project, this.options);
this.strategy = createTypeCheckingProgramStrategy(project);
this.adapter = new LanguageServiceAdapter(project);
@@ -315,7 +326,7 @@ export class LanguageService {
project.getConfigFilePath(), (fileName: string, eventKind: ts.FileWatcherEventKind) => {
project.log(`Config file changed: ${fileName}`);
if (eventKind === ts.FileWatcherEventKind.Changed) {
this.options = parseNgCompilerOptions(project, this.parseConfigHost);
this.options = parseNgCompilerOptions(project, this.parseConfigHost, this.config);
logCompilerOptions(project, this.options);
}
});
@@ -329,7 +340,8 @@ function logCompilerOptions(project: ts.server.Project, options: CompilerOptions
}
function parseNgCompilerOptions(
project: ts.server.Project, host: ConfigurationHost): CompilerOptions {
project: ts.server.Project, host: ConfigurationHost,
config: LanguageServiceConfig): CompilerOptions {
if (!(project instanceof ts.server.ConfiguredProject)) {
return {};
}
@@ -350,6 +362,12 @@ function parseNgCompilerOptions(
// and only the real component declaration is used.
options.compileNonExportedClasses = false;
// If `forceStrictTemplates` is true, always enable `strictTemplates`
// regardless of its value in tsconfig.json.
if (config.forceStrictTemplates === true) {
options.strictTemplates = true;
}
return options;
}
@@ -18,7 +18,7 @@ describe('definitions', () => {
beforeAll(() => {
const {project, service: _service, tsLS} = setup();
service = _service;
ngLS = new LanguageService(project, tsLS);
ngLS = new LanguageService(project, tsLS, {});
});
beforeEach(() => {
@@ -17,7 +17,7 @@ describe('getSemanticDiagnostics', () => {
beforeAll(() => {
const {project, service: _service, tsLS} = setup();
service = _service;
ngLS = new LanguageService(project, tsLS);
ngLS = new LanguageService(project, tsLS, {});
});
beforeEach(() => {
@@ -23,7 +23,7 @@ describe('language service adapter', () => {
const {project: _project, tsLS, service: _service, configFileFs: _configFileFs} = setup();
project = _project;
service = _service;
ngLS = new LanguageService(project, tsLS);
ngLS = new LanguageService(project, tsLS, {});
configFileFs = _configFileFs;
});
@@ -57,6 +57,33 @@ describe('language service adapter', () => {
strictTemplates: false,
}));
});
it('should always enable strictTemplates if forceStrictTemplates is true', () => {
const {project, tsLS, configFileFs} = setup();
const ngLS = new LanguageService(project, tsLS, {
forceStrictTemplates: true,
});
// First make sure the default for strictTemplates is true
expect(ngLS.getCompilerOptions()).toEqual(jasmine.objectContaining({
enableIvy: true, // default for ivy is true
strictTemplates: true,
strictInjectionParameters: true,
}));
// Change strictTemplates to false
configFileFs.overwriteConfigFile(TSCONFIG, {
angularCompilerOptions: {
strictTemplates: false,
}
});
// Make sure strictTemplates is still true because forceStrictTemplates
// is enabled.
expect(ngLS.getCompilerOptions()).toEqual(jasmine.objectContaining({
strictTemplates: true,
}));
});
});
describe('compiler options diagnostics', () => {
@@ -19,7 +19,7 @@ describe('getExternalFiles()', () => {
// a global analysis
expect(externalFiles).toEqual([]);
// Trigger global analysis
const ngLS = new LanguageService(project, tsLS);
const ngLS = new LanguageService(project, tsLS, {});
ngLS.getSemanticDiagnostics(APP_COMPONENT);
// Now that global analysis is run, we should have all the typecheck files
externalFiles = getExternalFiles(project);
@@ -18,7 +18,7 @@ describe('type definitions', () => {
beforeAll(() => {
const {project, service: _service, tsLS} = setup();
service = _service;
ngLS = new LanguageService(project, tsLS);
ngLS = new LanguageService(project, tsLS, {});
});
const possibleArrayDefFiles = new Set([
@@ -92,7 +92,7 @@ export class Project {
// The following operation forces a ts.Program to be created.
this.tsLS = tsProject.getLanguageService();
this.ngLS = new LanguageService(tsProject, this.tsLS);
this.ngLS = new LanguageService(tsProject, this.tsLS, {});
}
openFile(projectFileName: string): OpenBuffer {
@@ -188,4 +188,4 @@ function getClassOrError(sf: ts.SourceFile, name: string): ts.ClassDeclaration {
}
}
throw new Error(`Class ${name} not found in file: ${sf.fileName}: ${sf.text}`);
}
}
+1 -1
View File
@@ -14,7 +14,7 @@ export function create(info: ts.server.PluginCreateInfo): NgLanguageService {
const {project, languageService: tsLS, config} = info;
const angularOnly = config?.angularOnly === true;
const ngLS = new LanguageService(project, tsLS);
const ngLS = new LanguageService(project, tsLS, config);
function getSemanticDiagnostics(fileName: string): ts.Diagnostic[] {
const diagnostics: ts.Diagnostic[] = [];