Skip to content

Commit 9d81ece

Browse files
authored
Merge pull request #141 from microsoft/user/winstonliu/expression-key-must-be-scalar
Only check for expressions when keys are scalar
2 parents 379231f + 87c06d2 commit 9d81ece

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

language-service/src/parser/yamlParser.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,7 @@ function recursivelyBuildAst(parent: ASTNode, node: Yaml.YAMLNode): ASTNode {
184184
// and remove the expression from the parsed YAML.
185185
function addPropertiesToObjectNode(node: ObjectASTNode, properties: Yaml.YAMLMapping[]): void {
186186
for (const property of properties) {
187-
// The endsWith check is necessary because you can have dynamically-generated variables;
188-
// for example, ${{ environment }}Release: true.
189-
if (property.key.value.startsWith("${{") && property.key.value.endsWith("}}")) {
187+
if (isCompileTimeExpression(property)) {
190188
// Ensure we have a value (object) _and_ that the value has mappings (properties).
191189
if (property.value?.mappings !== undefined) {
192190
addPropertiesToObjectNode(node, property.value.mappings);
@@ -214,9 +212,7 @@ function addItemsToArrayNode(node: ArrayASTNode, items: Yaml.YAMLNode[]): void {
214212
// Hoisted expressions must be the first (and only) property in an object,
215213
// so we can safely check only the first key.
216214
// TODO: Confirm the above statement.
217-
if (item.kind === Yaml.Kind.MAP &&
218-
item.mappings[0].key.value.startsWith("${{") &&
219-
item.mappings[0].key.value.endsWith("}}")) {
215+
if (item.kind === Yaml.Kind.MAP && isCompileTimeExpression(item.mappings[0])) {
220216
const value = item.mappings[0].value;
221217
if (value === null) {
222218
// Incomplete object: they're still working on the value :).
@@ -274,6 +270,12 @@ function addItemsToArrayNode(node: ArrayASTNode, items: Yaml.YAMLNode[]): void {
274270
}
275271
}
276272

273+
function isCompileTimeExpression(node: Yaml.YAMLNode): boolean {
274+
return node.key.kind === Yaml.Kind.SCALAR &&
275+
node.key.value.startsWith("${{") &&
276+
node.key.value.endsWith("}}");
277+
}
278+
277279
function convertError(e: Yaml.YAMLException): YAMLError {
278280
return { getMessage: () => e.reason, start: e.mark.position, end: e.mark.position + e.mark.column };
279281
}

language-service/test/pipelinesTests/yamlvalidation.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,22 @@ steps:
119119
`);
120120
assert.equal(diagnostics.length, 2);
121121
});
122+
123+
it('validates incorrectly-indented pipelines that look like they have an array property', async function () {
124+
// Note: the real purpose of this test is to ensure we don't throw,
125+
// but I can't figure out how to assert that yet.
126+
// diagnostics.length can be whatever, as long as we get to that point :).
127+
const diagnostics = await runValidationTest(`
128+
steps:
129+
- task: PowerShellOnTargetMachines@3
130+
inputs:
131+
Machines: EXAMPLE
132+
InlineScript: |
133+
[System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem") | Out-Null
134+
CommunicationProtocol: Http);
135+
`);
136+
assert.equal(diagnostics.length, 4);
137+
});
122138
});
123139

124140
const workspaceContext = {

0 commit comments

Comments
 (0)