Skip to content
This repository was archived by the owner on Jun 15, 2021. It is now read-only.

Commit 73ba6aa

Browse files
committed
feat: report errors on reserved environment variables (#34)
1 parent f32daa3 commit 73ba6aa

File tree

7 files changed

+107
-68
lines changed

7 files changed

+107
-68
lines changed

src/binding/binder.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ export function bindDocument(root: DocumentSyntax, bag: DiagnosticBag): BoundDoc
164164
bag.propertyAlreadyDefined(property.key);
165165
} else {
166166
env = new BoundEnv(bindObject(property.value), property);
167+
for (const variable of env.variables.keys()) {
168+
if (variable.startsWith("GITHUB_")) {
169+
bag.reservedEnvironmentVariable(property.key.range);
170+
}
171+
}
167172
}
168173
break;
169174
}
@@ -247,7 +252,7 @@ export function bindDocument(root: DocumentSyntax, bag: DiagnosticBag): BoundDoc
247252
}
248253
case SyntaxKind.ObjectValue: {
249254
(syntax as ObjectValueSyntax).members.forEach(variable => {
250-
const key = removeDoubleQuotes(variable.name.text);
255+
const key = variable.name.text;
251256
if (map.has(key)) {
252257
bag.duplicateKey(key, variable.name.range);
253258
} else {

src/binding/bound-nodes.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55
import { DocumentSyntax, BlockSyntax, BaseSyntaxNode, VersionSyntax, PropertySyntax } from "../parsing/syntax-nodes";
66
import { filterUndefined } from "../util/array-utils";
77

8-
export const enum BoundKind {
8+
export enum BoundKind {
99
// Top level
10-
Document = 1,
11-
Version = 2,
12-
Workflow = 3,
13-
Action = 4,
10+
Document,
11+
Version,
12+
Workflow,
13+
Action,
1414

1515
// Properties
16-
On = 5,
17-
Resolves = 6,
18-
Uses = 7,
19-
Needs = 8,
20-
Runs = 9,
21-
Args = 10,
22-
Env = 11,
23-
Secrets = 12,
16+
On,
17+
Resolves,
18+
Uses,
19+
Needs,
20+
Runs,
21+
Args,
22+
Env,
23+
Secrets,
2424
}
2525

2626
export abstract class BaseBoundNode<TSyntax extends BaseSyntaxNode> {

src/parsing/syntax-nodes.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@
44

55
import { Token, TokenKind, getTokenDescription } from "../scanning/tokens";
66

7-
export const enum SyntaxKind {
7+
export enum SyntaxKind {
88
// Top level
9-
Document = 1,
10-
Version = 2,
11-
Block = 3,
9+
Document,
10+
Version,
11+
Block,
1212

1313
// Properties
14-
Property = 4,
14+
Property,
1515

1616
// Strings
17-
StringValue = 5,
17+
StringValue,
1818

1919
// String Arrays
20-
StringArrayValue = 6,
21-
StringArrayItem = 7,
20+
StringArrayValue,
21+
StringArrayItem,
2222

2323
// Env Variables
24-
ObjectValue = 8,
25-
ObjectMember = 9,
24+
ObjectValue,
25+
ObjectMember,
2626
}
2727

2828
function assertTokenKind(token?: Token, ...acceptedKinds: TokenKind[]): void {

src/scanning/tokens.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,43 @@
22
* Copyright 2019 Omar Tawfik. Please see LICENSE file at the root of this repository.
33
*/
44

5-
export const enum TokenKind {
5+
export enum TokenKind {
66
// Top-level keywords
7-
VersionKeyword = 1,
8-
WorkflowKeyword = 2,
9-
ActionKeyword = 3,
7+
VersionKeyword,
8+
WorkflowKeyword,
9+
ActionKeyword,
1010

1111
// Bottom-level keywords
12-
OnKeyword = 4,
13-
ResolvesKeyword = 5,
14-
UsesKeyword = 6,
15-
NeedsKeyword = 7,
16-
RunsKeyword = 8,
17-
ArgsKeyword = 9,
18-
EnvKeyword = 10,
19-
SecretsKeyword = 11,
12+
OnKeyword,
13+
ResolvesKeyword,
14+
UsesKeyword,
15+
NeedsKeyword,
16+
RunsKeyword,
17+
ArgsKeyword,
18+
EnvKeyword,
19+
SecretsKeyword,
2020

2121
// Punctuation
22-
Equal = 12,
23-
Comma = 13,
22+
Equal,
23+
Comma,
2424

2525
// Brackets
26-
LeftCurlyBracket = 14,
27-
RightCurlyBracket = 15,
28-
LeftSquareBracket = 16,
29-
RightSquareBracket = 17,
26+
LeftCurlyBracket,
27+
RightCurlyBracket,
28+
LeftSquareBracket,
29+
RightSquareBracket,
3030

3131
// Misc
32-
Identifier = 18,
33-
Comment = 19,
32+
Identifier,
33+
Comment,
3434

3535
// Literals
36-
IntegerLiteral = 20,
37-
StringLiteral = 21,
36+
IntegerLiteral,
37+
StringLiteral,
3838

3939
// Generated
40-
Missing = 22,
41-
Unrecognized = 23,
40+
Missing,
41+
Unrecognized,
4242
}
4343

4444
export function getTokenDescription(kind: TokenKind): string {

src/util/diagnostics.ts

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,34 @@
55
import { TokenKind, TextRange, getTokenDescription, Token } from "../scanning/tokens";
66
import { MAXIMUM_SUPPORTED_VERSION, MAXIMUM_SUPPORTED_SECRETS, MAXIMUM_SUPPORTED_ACTIONS } from "./constants";
77

8-
export const enum DiagnosticCode {
8+
export enum DiagnosticCode {
99
// Scanning
10-
UnrecognizedCharacter = 1,
11-
UnterminatedStringLiteral = 2,
12-
UnsupportedEscapeSequence = 3,
10+
UnrecognizedCharacter,
11+
UnterminatedStringLiteral,
12+
UnsupportedEscapeSequence,
1313

1414
// Parsing
15-
MissingToken = 4,
16-
UnexpectedToken = 5,
15+
MissingToken,
16+
UnexpectedToken,
1717

1818
// Binding
19-
MultipleVersion = 6,
20-
UnrecognizedVersion = 7,
21-
VersionAfterBlock = 8,
22-
ValueIsNotString = 9,
23-
ValueIsNotStringOrArray = 10,
24-
ValueIsNotAnObject = 11,
25-
PropertyAlreadyDefined = 12,
26-
PropertyMustBeDefined = 13,
27-
InvalidProperty = 14,
28-
DuplicateKey = 15,
19+
MultipleVersion,
20+
UnrecognizedVersion,
21+
VersionAfterBlock,
22+
ValueIsNotString,
23+
ValueIsNotStringOrArray,
24+
ValueIsNotAnObject,
25+
PropertyAlreadyDefined,
26+
PropertyMustBeDefined,
27+
InvalidProperty,
28+
DuplicateKey,
29+
ReservedEnvironmentVariable,
2930

3031
// Analysis:
31-
TooManySecrets = 16,
32-
DuplicateSecrets = 17,
33-
TooManyActions = 18,
34-
DuplicateActions = 19,
32+
TooManySecrets,
33+
DuplicateSecrets,
34+
TooManyActions,
35+
DuplicateActions,
3536
}
3637

3738
export interface Diagnostic {
@@ -175,6 +176,14 @@ export class DiagnosticBag {
175176
});
176177
}
177178

179+
public reservedEnvironmentVariable(range: TextRange): void {
180+
this.items.push({
181+
range,
182+
code: DiagnosticCode.ReservedEnvironmentVariable,
183+
message: `Environment variables starting with 'GITHUB_' are reserved.`,
184+
});
185+
}
186+
178187
public tooManySecrets(range: TextRange): void {
179188
this.items.push({
180189
range,

test/binding-errors.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,28 @@ ERROR: Value must be an object.
223223
5 | }
224224
6 |
225225
"
226+
`);
227+
});
228+
229+
it("reports errors on reserved environment variables", () => {
230+
expectDiagnostics(`
231+
action "x" {
232+
uses = "./ci"
233+
env = {
234+
GITHUB_ACTION = "1"
235+
GITHUBNOUNDERSCORE = "2"
236+
SOMETHING_ELSE = "3"
237+
}
238+
}`).toMatchInlineSnapshot(`
239+
"
240+
ERROR: Environment variables starting with 'GITHUB_' are reserved.
241+
2 | action \\"x\\" {
242+
3 | uses = \\"./ci\\"
243+
4 | env = {
244+
| ^^^
245+
5 | GITHUB_ACTION = \\"1\\"
246+
6 | GITHUBNOUNDERSCORE = \\"2\\"
247+
"
226248
`);
227249
});
228250
});

tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
"types": ["jest"],
99
"lib": [],
1010

11+
// Features
12+
"downlevelIteration": true,
13+
1114
// Output files
1215
"declaration": true,
1316
"sourceMap": true,

0 commit comments

Comments
 (0)