Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(syntaxes): add support for let declarations #2042

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Input hashes for repository rule npm_translate_lock(name = "npm", pnpm_lock = "//:pnpm-lock.yaml").
# This file should be checked into version control along with the pnpm-lock.yaml file.
.npmrc=974837034
pnpm-lock.yaml=915470004
pnpm-lock.yaml=730915817
yarn.lock=1032276408
package.json=-1247013691
package.json=-257701941
pnpm-workspace.yaml=1711114604
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@
"expression.ng": "javascript"
}
},
{
"path": "./syntaxes/let-declaration.json",
"scopeName": "template.let.ng",
"injectTo": [
"text.html.derivative",
"source.ts"
]
},
{
"path": "./syntaxes/template-tag.json",
"scopeName": "template.tag.ng",
Expand Down
49 changes: 32 additions & 17 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions syntaxes/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ js_run_binary(
"_template.json",
"_template-blocks.json",
"_template-tag.json",
"_let-declaration.json",
]
)

Expand All @@ -27,6 +28,7 @@ write_source_files(
"template.json": "_template.json",
"template-blocks.json": "_template-blocks.json",
"template-tag.json": "_template-tag.json",
"let-declaration.json": "_let-declaration.json",
}
)

Expand Down
52 changes: 52 additions & 0 deletions syntaxes/let-declaration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"scopeName": "template.let.ng",
"injectionSelector": "L:text.html -comment -expression.ng -meta.tag -source.css -source.js",
"patterns": [
{
"include": "#letDeclaration"
}
],
"repository": {
"letDeclaration": {
"begin": "(@let)\\s+([_$[:alpha:]][_$[:alnum:]]*)\\s*(=)?",
"beginCaptures": {
"1": {
"name": "storage.type.ng"
},
"2": {
"name": "meta.definition.variable.ng"
},
"3": {
"name": "keyword.operator.assignment.ng"
}
},
"patterns": [
{
"include": "#letInitializer"
}
],
"contentName": "meta.definition.variable.ng",
"end": "(?<=;)"
},
"letInitializer": {
"begin": "\\s*",
"beginCaptures": {
"0": {
"name": "keyword.operator.assignment.ng"
}
},
"contentName": "meta.definition.variable.initializer.ng",
"patterns": [
{
"include": "expression.ng"
}
],
"end": ";",
"endCaptures": {
"0": {
"name": "punctuation.terminator.statement.ng"
}
}
}
}
}
2 changes: 2 additions & 0 deletions syntaxes/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {InlineStyles} from './inline-styles';
import {InlineTemplate} from './inline-template';
import {Template} from './template';
import {TemplateBlocks} from './template-blocks';
import {LetDeclaration} from './template-let-declaration';
import {TemplateTag} from './template-tag';
import {GrammarDefinition, JsonObject} from './types';

Expand Down Expand Up @@ -57,3 +58,4 @@ build(InlineTemplate, 'inline-template');
build(InlineStyles, 'inline-styles');
build(TemplateBlocks, 'template-blocks');
build(TemplateTag, 'template-tag');
build(LetDeclaration, 'let-declaration');
45 changes: 45 additions & 0 deletions syntaxes/src/template-let-declaration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {GrammarDefinition} from './types';

export const LetDeclaration: GrammarDefinition = {
scopeName: 'template.let.ng',
injectionSelector: 'L:text.html -comment -expression.ng -meta.tag -source.css -source.js',
patterns: [
{include: '#letDeclaration'},
],
repository: {
letDeclaration: {
// Equals group is optional so that we start highlighting as
// soon as the user starts writing a valid name.
begin: /(@let)\s+([_$[:alpha:]][_$[:alnum:]]*)\s*(=)?/,
beginCaptures: {
1: {name: 'storage.type.ng'},
2: {name: 'meta.definition.variable.ng'},
3: {name: 'keyword.operator.assignment.ng'},
},
patterns: [{include: '#letInitializer'}],
contentName: 'meta.definition.variable.ng',
end: /(?<=;)/,
},

letInitializer: {
begin: /\s*/,
beginCaptures: {
0: {name: 'keyword.operator.assignment.ng'},
},
contentName: 'meta.definition.variable.initializer.ng',
patterns: [{include: 'expression.ng'}],
end: /;/,
endCaptures: {
0: {name: 'punctuation.terminator.statement.ng'},
},
},
},
};
8 changes: 7 additions & 1 deletion syntaxes/test/cases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,11 @@ export const cases = [
'scopeName': 'template.ng',
'grammarFiles': ['syntaxes/template.json', 'syntaxes/expression.json'],
'testFile': 'syntaxes/test/data/expression.html'
}
},
{
'name': 'let syntax',
'scopeName': 'template.let.ng',
'grammarFiles': ['syntaxes/let-declaration.json', 'syntaxes/expression.json'],
'testFile': 'syntaxes/test/data/let-declaration.html'
},
];
31 changes: 31 additions & 0 deletions syntaxes/test/data/let-declaration.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@let basicLet = 123 + 456;

@let noSpaceAfterEquals =true;

@let noSpaceBeforeEquals= true;

@let noSpaceAroundEquals=true;

@let lotOfSpaceAroundEquals = true;

@let #invalid = true;
@let invalidIn#TheMiddle = true;
@letinvalid = true;

@let stringContainingSemicolon = 'hello ;' + 'world';

@let complexExpression = something ? 123 : {prop: 'hello' + true + 'world'};

@let usingPipes = 123 + foo | async | multiply: 2 | separator: ';';

@if (someExpr | async) {
@let inBlock = true;

@for (foo of bar; track foo) {
@let inNestedBlock = 123;
}
}

@let noEquals

@let noValue =
Loading
Loading