Skip to content

Commit 4ab3481

Browse files
authored
feat(syntaxes): add support for let declarations (#2042)
Adds highlighting support for the new `@let` syntax.
1 parent ed753c8 commit 4ab3481

File tree

10 files changed

+373
-20
lines changed

10 files changed

+373
-20
lines changed

.aspect/rules/external_repository_action_cache/npm_translate_lock_LTE4Nzc1MDcwNjU=

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Input hashes for repository rule npm_translate_lock(name = "npm", pnpm_lock = "//:pnpm-lock.yaml").
33
# This file should be checked into version control along with the pnpm-lock.yaml file.
44
.npmrc=974837034
5-
pnpm-lock.yaml=915470004
5+
pnpm-lock.yaml=730915817
66
yarn.lock=1032276408
7-
package.json=-1247013691
7+
package.json=-257701941
88
pnpm-workspace.yaml=1711114604

package.json

+8
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,14 @@
185185
"expression.ng": "javascript"
186186
}
187187
},
188+
{
189+
"path": "./syntaxes/let-declaration.json",
190+
"scopeName": "template.let.ng",
191+
"injectTo": [
192+
"text.html.derivative",
193+
"source.ts"
194+
]
195+
},
188196
{
189197
"path": "./syntaxes/template-tag.json",
190198
"scopeName": "template.tag.ng",

pnpm-lock.yaml

+32-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

syntaxes/BUILD.bazel

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ js_run_binary(
1515
"_template.json",
1616
"_template-blocks.json",
1717
"_template-tag.json",
18+
"_let-declaration.json",
1819
]
1920
)
2021

@@ -27,6 +28,7 @@ write_source_files(
2728
"template.json": "_template.json",
2829
"template-blocks.json": "_template-blocks.json",
2930
"template-tag.json": "_template-tag.json",
31+
"let-declaration.json": "_let-declaration.json",
3032
}
3133
)
3234

syntaxes/let-declaration.json

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"scopeName": "template.let.ng",
3+
"injectionSelector": "L:text.html -comment -expression.ng -meta.tag -source.css -source.js",
4+
"patterns": [
5+
{
6+
"include": "#letDeclaration"
7+
}
8+
],
9+
"repository": {
10+
"letDeclaration": {
11+
"begin": "(@let)\\s+([_$[:alpha:]][_$[:alnum:]]*)\\s*(=)?",
12+
"beginCaptures": {
13+
"1": {
14+
"name": "storage.type.ng"
15+
},
16+
"2": {
17+
"name": "meta.definition.variable.ng"
18+
},
19+
"3": {
20+
"name": "keyword.operator.assignment.ng"
21+
}
22+
},
23+
"patterns": [
24+
{
25+
"include": "#letInitializer"
26+
}
27+
],
28+
"contentName": "meta.definition.variable.ng",
29+
"end": "(?<=;)"
30+
},
31+
"letInitializer": {
32+
"begin": "\\s*",
33+
"beginCaptures": {
34+
"0": {
35+
"name": "keyword.operator.assignment.ng"
36+
}
37+
},
38+
"contentName": "meta.definition.variable.initializer.ng",
39+
"patterns": [
40+
{
41+
"include": "expression.ng"
42+
}
43+
],
44+
"end": ";",
45+
"endCaptures": {
46+
"0": {
47+
"name": "punctuation.terminator.statement.ng"
48+
}
49+
}
50+
}
51+
}
52+
}

syntaxes/src/build.ts

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {InlineStyles} from './inline-styles';
1313
import {InlineTemplate} from './inline-template';
1414
import {Template} from './template';
1515
import {TemplateBlocks} from './template-blocks';
16+
import {LetDeclaration} from './template-let-declaration';
1617
import {TemplateTag} from './template-tag';
1718
import {GrammarDefinition, JsonObject} from './types';
1819

@@ -57,3 +58,4 @@ build(InlineTemplate, 'inline-template');
5758
build(InlineStyles, 'inline-styles');
5859
build(TemplateBlocks, 'template-blocks');
5960
build(TemplateTag, 'template-tag');
61+
build(LetDeclaration, 'let-declaration');
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {GrammarDefinition} from './types';
10+
11+
export const LetDeclaration: GrammarDefinition = {
12+
scopeName: 'template.let.ng',
13+
injectionSelector: 'L:text.html -comment -expression.ng -meta.tag -source.css -source.js',
14+
patterns: [
15+
{include: '#letDeclaration'},
16+
],
17+
repository: {
18+
letDeclaration: {
19+
// Equals group is optional so that we start highlighting as
20+
// soon as the user starts writing a valid name.
21+
begin: /(@let)\s+([_$[:alpha:]][_$[:alnum:]]*)\s*(=)?/,
22+
beginCaptures: {
23+
1: {name: 'storage.type.ng'},
24+
2: {name: 'meta.definition.variable.ng'},
25+
3: {name: 'keyword.operator.assignment.ng'},
26+
},
27+
patterns: [{include: '#letInitializer'}],
28+
contentName: 'meta.definition.variable.ng',
29+
end: /(?<=;)/,
30+
},
31+
32+
letInitializer: {
33+
begin: /\s*/,
34+
beginCaptures: {
35+
0: {name: 'keyword.operator.assignment.ng'},
36+
},
37+
contentName: 'meta.definition.variable.initializer.ng',
38+
patterns: [{include: 'expression.ng'}],
39+
end: /;/,
40+
endCaptures: {
41+
0: {name: 'punctuation.terminator.statement.ng'},
42+
},
43+
},
44+
},
45+
};

syntaxes/test/cases.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,11 @@ export const cases = [
3737
'scopeName': 'template.ng',
3838
'grammarFiles': ['syntaxes/template.json', 'syntaxes/expression.json'],
3939
'testFile': 'syntaxes/test/data/expression.html'
40-
}
40+
},
41+
{
42+
'name': 'let syntax',
43+
'scopeName': 'template.let.ng',
44+
'grammarFiles': ['syntaxes/let-declaration.json', 'syntaxes/expression.json'],
45+
'testFile': 'syntaxes/test/data/let-declaration.html'
46+
},
4147
];
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
@let basicLet = 123 + 456;
2+
3+
@let noSpaceAfterEquals =true;
4+
5+
@let noSpaceBeforeEquals= true;
6+
7+
@let noSpaceAroundEquals=true;
8+
9+
@let lotOfSpaceAroundEquals = true;
10+
11+
@let #invalid = true;
12+
@let invalidIn#TheMiddle = true;
13+
@letinvalid = true;
14+
15+
@let stringContainingSemicolon = 'hello ;' + 'world';
16+
17+
@let complexExpression = something ? 123 : {prop: 'hello' + true + 'world'};
18+
19+
@let usingPipes = 123 + foo | async | multiply: 2 | separator: ';';
20+
21+
@if (someExpr | async) {
22+
@let inBlock = true;
23+
24+
@for (foo of bar; track foo) {
25+
@let inNestedBlock = 123;
26+
}
27+
}
28+
29+
@let noEquals
30+
31+
@let noValue =

0 commit comments

Comments
 (0)