Skip to content

Commit e3326b9

Browse files
author
Chuck Meyer
authored
Merge pull request #22 from kddejong/Feature/BetterTemplateFinding
Feature/better template finding
2 parents 5504113 + 494472c commit e3326b9

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "CloudFormation linter",
44
"author": "Kevin DeJong",
55
"license": "Apache-2.0",
6-
"version": "0.2.0",
6+
"version": "0.2.2",
77
"publisher": "kddejong",
88
"engines": {
99
"vscode": "^1.18.0"

server/src/server.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,23 @@ function convertSeverity(mistakeType: string): DiagnosticSeverity {
104104
return DiagnosticSeverity.Error;
105105
}
106106

107+
function isCloudFormation(template: string, filename: string): Boolean {
108+
109+
if (/"?AWSTemplateFormatVersion"?\s*/.exec(template)) {
110+
connection.console.log("Determined this file is a CloudFormation Template. " + filename +
111+
". Found the string AWSTemplateFormatVersion");
112+
return true;
113+
}
114+
if (/"?Resources"?\s*:/.exec(template)) {
115+
if (/"?Type"?\s*:\s*"?'?(AWS|Custom)::/.exec(template)) {
116+
connection.console.log("Determined this file is a CloudFormation Template. " + filename +
117+
". Found 'Resources' and 'Type: (AWS|Custom)::'");
118+
return true;
119+
}
120+
}
121+
return false;
122+
}
123+
107124
function validateCloudFormationFile(document: TextDocument): void {
108125
let uri = document.uri;
109126

@@ -114,16 +131,7 @@ function validateCloudFormationFile(document: TextDocument): void {
114131

115132
let file_to_lint = Files.uriToFilePath(uri);
116133

117-
let is_cfn_regex = new RegExp('"?AWSTemplateFormatVersion"?\s*');
118-
let is_cfn = false;
119-
let text = document.getText().split("\n");
120-
for (var index in text) {
121-
if (is_cfn_regex.exec(text[index])) {
122-
is_cfn = true;
123-
}
124-
}
125-
126-
connection.console.log("File '" + uri.toString() + " is a CFN? " + is_cfn);
134+
let is_cfn = isCloudFormation(document.getText(), uri.toString());
127135

128136
if (is_cfn) {
129137
let args = ['--format', 'json', '--template', file_to_lint];
@@ -222,6 +230,10 @@ function validateCloudFormationFile(document: TextDocument): void {
222230
connection.sendDiagnostics({ uri: filename, diagnostics });
223231
isValidating[uri] = false;
224232
});
233+
} else {
234+
connection.console.log("Don't believe this is a CloudFormation template. " + uri.toString() +
235+
". If it is please add AWSTemplateFormatVersion: '2010-09-09' (YAML) or " +
236+
" \"AWSTemplateFormatVersion\": \"2010-09-09\" (JSON) into the root level of the document.");
225237
}
226238
}
227239

0 commit comments

Comments
 (0)