Skip to content

Commit 17fdb9e

Browse files
authored
feat(extension): Add option to force strict templates (#1646)
This feature request is to provide an extension option to override the project's `angularCompilerOptions`. This would allow the language service to enable `strictTemplates`. With the release of v12, the Ivy-based Language Service became the default. With that, the information provided by the extension follows what the application has configured the compiler to interpret (see Alex's [blog post](https://blog.angular.io/under-the-hood-of-the-language-service-ab763c26f522) for more information on why this was done). As a result, developers upgrading from previous versions that have apps which do not have strict templates enabled often experience a loss of information from the extension. It also may not be feasible for large projects to enable `strictTemplates` immediately due to an abundance of new errors that would need to be fixed. Importantly, this would likely result in the language service producing diagnostics that are not produced when running/compiling the application. However, this may still be a more ideal state than the current experience (which would be simply not getting _any_ useful information for lots of locations in the template which require stricter type checking options). resolves #1418
1 parent 4a7538e commit 17fdb9e

File tree

5 files changed

+19
-2
lines changed

5 files changed

+19
-2
lines changed

client/src/client.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,8 @@ function registerNotificationHandlers(client: lsp.LanguageClient): vscode.Dispos
331331

332332
disposables.push(client.onNotification(SuggestStrictMode, async (params: SuggestStrictModeParams) => {
333333
const config = vscode.workspace.getConfiguration();
334-
if (config.get('angular.enable-strict-mode-prompt') === false) {
334+
if (config.get('angular.enable-strict-mode-prompt') === false ||
335+
config.get('angular.forceStrictTemplates')) {
335336
return;
336337
}
337338

@@ -425,6 +426,11 @@ function constructArgs(ctx: vscode.ExtensionContext, viewEngine: boolean): strin
425426
args.push('--disableAutomaticNgcc');
426427
}
427428

429+
const forceStrictTemplates = config.get<boolean>('angular.forceStrictTemplates');
430+
if (forceStrictTemplates) {
431+
args.push('--forceStrictTemplates');
432+
}
433+
428434
const tsdk: string|null = config.get('typescript.tsdk', null);
429435
const tsProbeLocations = [tsdk, ...getProbeLocations(ctx.extensionPath)];
430436
args.push('--tsProbeLocations', tsProbeLocations.join(','));

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@
133133
"type": "boolean",
134134
"default": false,
135135
"markdownDescription": "Disable the step to automatically run ngcc. [ngcc](https://github.com/angular/angular/blob/master/packages/compiler/design/architecture.md#high-level-proposal) is required to run and gather metadata from libraries not published with Ivy instructions. This can be run outside of VSCode instead (for example, as part of the build/rebuild in the CLI). Note that ngcc needs to run not only at startup, but also whenever the dependencies change. Failing to run ngcc when required can result in incomplete information and spurious errors reported by the language service."
136+
},
137+
"angular.forceStrictTemplates": {
138+
"type": "boolean",
139+
"default": false,
140+
"markdownDescription": "Enabling this option will force the language service to use [strictTemplates](https://angular.io/guide/angular-compiler-options#stricttemplates) and ignore the user settings in the `tsconfig.json`."
136141
}
137142
}
138143
},

server/src/cmdline_utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ interface CommandLineOptions {
4343
tsProbeLocations: string[];
4444
includeAutomaticOptionalChainCompletions: boolean;
4545
includeCompletionsWithSnippetText: boolean;
46+
forceStrictTemplates: boolean;
4647
}
4748

4849
export function parseCommandLine(argv: string[]): CommandLineOptions {
@@ -58,6 +59,7 @@ export function parseCommandLine(argv: string[]): CommandLineOptions {
5859
includeAutomaticOptionalChainCompletions:
5960
hasArgument(argv, '--includeAutomaticOptionalChainCompletions'),
6061
includeCompletionsWithSnippetText: hasArgument(argv, '--includeCompletionsWithSnippetText'),
62+
forceStrictTemplates: hasArgument(argv, '--forceStrictTemplates'),
6163
};
6264
}
6365

server/src/server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ function main() {
4747
logToConsole: options.logToConsole,
4848
includeAutomaticOptionalChainCompletions: options.includeAutomaticOptionalChainCompletions,
4949
includeCompletionsWithSnippetText: options.includeCompletionsWithSnippetText,
50+
forceStrictTemplates: isG3 || options.forceStrictTemplates,
5051
});
5152

5253
// Log initialization info

server/src/session.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface SessionOptions {
3131
logToConsole: boolean;
3232
includeAutomaticOptionalChainCompletions: boolean;
3333
includeCompletionsWithSnippetText: boolean;
34+
forceStrictTemplates: boolean;
3435
}
3536

3637
enum LanguageId {
@@ -158,9 +159,11 @@ export class Session {
158159
const pluginConfig: PluginConfig = {
159160
angularOnly: true,
160161
};
162+
if (options.forceStrictTemplates) {
163+
pluginConfig.forceStrictTemplates = true;
164+
}
161165
if (options.host.isG3) {
162166
options.ivy = true;
163-
pluginConfig.forceStrictTemplates = true;
164167
}
165168
projSvc.configurePlugin({
166169
pluginName: options.ngPlugin,

0 commit comments

Comments
 (0)