Skip to content

Commit

Permalink
fix(client): fix detection of Angular context after string interpolation
Browse files Browse the repository at this point in the history
This probably still has its own issues but seems to be at least an improvement over
past scanner issues.

It may be good to spend more time on the correctness of this or just
`ts.createSourceFile`. This previously wasn't done due to perfomance
concerns.

fixes angular#1872
  • Loading branch information
atscott committed Sep 12, 2023
1 parent c44ce9d commit 45474b8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
15 changes: 15 additions & 0 deletions client/src/embedded_support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,23 @@ function isPropertyAssignmentToStringOrStringInArray(
let lastTokenText: string|undefined;
let unclosedBraces = 0;
let unclosedBrackets = 0;
let unclosedTaggedTemplates = 0;
let propertyAssignmentContext = false;

function isTemplateStartOfTaggedTemplate() {
return token === ts.SyntaxKind.NoSubstitutionTemplateLiteral ||
token === ts.SyntaxKind.TemplateHead;
}

while (token !== ts.SyntaxKind.EndOfFileToken && scanner.getStartPos() < offset) {
if (isTemplateStartOfTaggedTemplate()) {
unclosedTaggedTemplates++;
}
if (token === ts.SyntaxKind.CloseBraceToken && unclosedTaggedTemplates > 0) {
// https://github.com/Microsoft/TypeScript/issues/20055
token = scanner.reScanTemplateToken(/*isTaggedTemplate*/ true);
unclosedTaggedTemplates--;
}
if (lastToken === ts.SyntaxKind.Identifier && lastTokenText !== undefined &&
propertyAssignmentNames.includes(lastTokenText) && token === ts.SyntaxKind.ColonToken) {
propertyAssignmentContext = true;
Expand Down
17 changes: 16 additions & 1 deletion client/src/tests/embedded_support_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ describe('embedded language support', () => {
it('at end of template', () => {
test(`template: '<div></div>¦'`, isInsideInlineTemplateRegion, true);
});

it('works for inline templates after a template string', () => {
test(
'const x = `${""}`;\n' +
'template: `hello ¦world`',
isInsideInlineTemplateRegion, true);
});

it('works for inline templates after a tagged template string inside tagged template string',
() => {
test(
'const x = `${`${""}`}`;\n' +
'template: `hello ¦world`',
isInsideInlineTemplateRegion, true);
});
});

describe('isInsideAngularContext', () => {
Expand Down Expand Up @@ -112,4 +127,4 @@ function extractCursorInfo(textWithCursor: string): {cursor: number, text: strin
cursor,
text: textWithCursor.substr(0, cursor) + textWithCursor.substr(cursor + 1),
};
}
}

0 comments on commit 45474b8

Please sign in to comment.