Skip to content

Commit 9eac3da

Browse files
authored
Updated to use parseValidYaml (#208)
1 parent ce0eaa9 commit 9eac3da

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
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.

src/document/Document.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { detectCfnFileType } from './CloudFormationDetection';
55
import { DocumentMetadata } from './DocumentProtocol';
66
import { detectDocumentType, uriToPath } from './DocumentUtils';
77
import { parseJson } from './JsonParser';
8-
import { parseYaml } from './YamlParser';
8+
import { parseValidYaml } from './YamlParser';
99

1010
export class Document {
1111
private readonly log = LoggerFactory.getLogger(Document);
@@ -44,7 +44,7 @@ export class Document {
4444
if (this.documentType === DocumentType.JSON) {
4545
return parseJson(this.contents());
4646
}
47-
return parseYaml(this.contents(), 0, true);
47+
return parseValidYaml(this.contents());
4848
}
4949

5050
public getLine(lineNumber: number): string | undefined {

src/document/YamlParser.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export function parseYaml(yamlString: string, parseAttempt: number = 0, jsonPars
3737
}
3838
}
3939

40+
export function parseValidYaml(validYamlString: string): any {
41+
return load(validYamlString, { schema: CloudFormationSchema });
42+
}
43+
4044
const CloudFormationYamlTypes = [
4145
// !Ref
4246
new Type('!Ref', {

tst/unit/document/Document.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,12 @@ describe('Document', () => {
156156
expect(result).toBeDefined();
157157
});
158158

159-
it('should handle invalid YAML gracefully', () => {
160-
const content = 'invalid:\n - yaml\n - structure';
159+
it('should throw for invalid YAML', () => {
160+
const content = 'key: value\n invalid: indentation';
161161
const textDocument = TextDocument.create('file:///test.yaml', 'yaml', 1, content);
162162
const doc = new Document(textDocument);
163163

164-
const result = doc.getParsedDocumentContent();
165-
expect(result).toBeDefined();
164+
expect(() => doc.getParsedDocumentContent()).toThrow();
166165
});
167166
});
168167
});

0 commit comments

Comments
 (0)