Skip to content

Commit 6240613

Browse files
committed
refactor(language-service): Add flag to allow disabling block syntax parsing (#52691)
This commit adds a flag to the language service config options to disable block parsing in the compiler. PR Close #52691
1 parent 227de98 commit 6240613

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

packages/language-service/api.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ export interface PluginConfig {
2525
* of its value in tsconfig.json.
2626
*/
2727
forceStrictTemplates?: true;
28+
29+
/**
30+
* If false, disables parsing control flow blocks in the compiler. Should be used only when older
31+
* versions of Angular that do not support blocks (pre-v17) used with the language service.
32+
*/
33+
enableBlockSyntax?: false;
2834
}
2935

3036
export type GetTcbResponse = {

packages/language-service/src/language_service.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {isNamedClassDeclaration} from '@angular/compiler-cli/src/ngtsc/reflectio
1717
import {OptimizeFor} from '@angular/compiler-cli/src/ngtsc/typecheck/api';
1818
import ts from 'typescript/lib/tsserverlibrary';
1919

20-
import {GetComponentLocationsForTemplateResponse, GetTcbResponse, GetTemplateLocationForComponentResponse} from '../api';
20+
import {GetComponentLocationsForTemplateResponse, GetTcbResponse, GetTemplateLocationForComponentResponse, PluginConfig} from '../api';
2121

2222
import {LanguageServiceAdapter, LSParseConfigHost} from './adapters';
2323
import {ALL_CODE_FIXES_METAS, CodeFixes} from './codefixes';
@@ -33,13 +33,7 @@ import {getTargetAtPosition, getTcbNodesOfTemplateAtPosition, TargetNodeKind} fr
3333
import {findTightestNode, getClassDeclFromDecoratorProp, getParentClassDeclaration, getPropertyAssignmentFromValue} from './ts_utils';
3434
import {getTemplateInfoAtPosition, isTypeScriptFile} from './utils';
3535

36-
interface LanguageServiceConfig {
37-
/**
38-
* If true, enable `strictTemplates` in Angular compiler options regardless
39-
* of its value in tsconfig.json.
40-
*/
41-
forceStrictTemplates?: true;
42-
}
36+
type LanguageServiceConfig = Omit<PluginConfig, 'angularOnly'>;
4337

4438
export class LanguageService {
4539
private options: CompilerOptions;
@@ -52,7 +46,7 @@ export class LanguageService {
5246
constructor(
5347
private readonly project: ts.server.Project,
5448
private readonly tsLS: ts.LanguageService,
55-
private readonly config: LanguageServiceConfig,
49+
private readonly config: Omit<PluginConfig, 'angularOnly'>,
5650
) {
5751
this.parseConfigHost = new LSParseConfigHost(project.projectService.host);
5852
this.options = parseNgCompilerOptions(project, this.parseConfigHost, config);
@@ -525,6 +519,9 @@ function parseNgCompilerOptions(
525519
if (config.forceStrictTemplates === true) {
526520
options.strictTemplates = true;
527521
}
522+
if (config.enableBlockSyntax === false) {
523+
options['_enableBlockSyntax'] = false;
524+
}
528525

529526
return options;
530527
}

packages/language-service/test/legacy/language_service_spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ describe('language service adapter', () => {
8181
strictTemplates: true,
8282
}));
8383
});
84+
85+
it('should always disable block syntax if enableBlockSyntax is false', () => {
86+
const {project, tsLS, configFileFs} = setup();
87+
const ngLS = new LanguageService(project, tsLS, {
88+
enableBlockSyntax: false,
89+
});
90+
91+
expect(ngLS.getCompilerOptions()).toEqual(jasmine.objectContaining({
92+
'_enableBlockSyntax': false,
93+
}));
94+
});
8495
});
8596

8697
describe('compiler options diagnostics', () => {

0 commit comments

Comments
 (0)