Skip to content

Commit 47ac74c

Browse files
Deep Furiyasatyakigh
authored andcommitted
getLines return undefined, getContents return undefined, line count default to 0
1 parent 0f64c79 commit 47ac74c

File tree

12 files changed

+80
-27
lines changed

12 files changed

+80
-27
lines changed

src/ai/CfnAI.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ export class CfnAI implements SettingsConfigurable, Closeable {
5858
throw new Error(`Template not found ${toString(templateFile)}`);
5959
}
6060

61-
return await agent.execute(
62-
await Prompts.describeTemplate(document.contents()),
63-
await this.getToolsWithFallback(),
64-
);
61+
const content = document.contents();
62+
if (!content) {
63+
throw new Error('Document content is undefined');
64+
}
65+
return await agent.execute(await Prompts.describeTemplate(content), await this.getToolsWithFallback());
6566
});
6667
}
6768

@@ -72,10 +73,11 @@ export class CfnAI implements SettingsConfigurable, Closeable {
7273
throw new Error(`Template not found ${toString(templateFile)}`);
7374
}
7475

75-
return await agent.execute(
76-
await Prompts.optimizeTemplate(document.contents()),
77-
await this.getToolsWithFallback(),
78-
);
76+
const content = document.contents();
77+
if (!content) {
78+
throw new Error('Document content is undefined');
79+
}
80+
return await agent.execute(await Prompts.optimizeTemplate(content), await this.getToolsWithFallback());
7981
});
8082
}
8183

@@ -86,8 +88,12 @@ export class CfnAI implements SettingsConfigurable, Closeable {
8688
return;
8789
}
8890

91+
const content = document.contents();
92+
if (!content) {
93+
throw new Error('Document content is undefined');
94+
}
8995
return await agent.execute(
90-
await Prompts.analyzeDiagnostic(document.contents(), diagnostics),
96+
await Prompts.analyzeDiagnostic(content, diagnostics),
9197
await this.getToolsWithFallback(),
9298
);
9399
});
@@ -107,6 +113,9 @@ export class CfnAI implements SettingsConfigurable, Closeable {
107113
}
108114

109115
const templateContent = document.contents();
116+
if (!templateContent) {
117+
throw new Error('Document content is undefined');
118+
}
110119

111120
const resourceTypes = this.relationshipSchemaService.extractResourceTypesFromTemplate(templateContent);
112121
const relationshipContext = this.relationshipSchemaService.getRelationshipContext(resourceTypes);

src/codeLens/ManagedResourceCodeLens.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export class ManagedResourceCodeLens {
2828
}
2929

3030
const text = document.getText();
31+
if (!text) {
32+
return [];
33+
}
3134
const lines = text.split('\n');
3235

3336
for (const [, resourceContext] of resourcesMap) {

src/codeLens/StackActionsCodeLens.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ export function getStackActionsCodeLenses(
3636

3737
let codeLensLine = 0;
3838
const lines = document.getLines();
39+
if (!lines) {
40+
return [];
41+
}
3942
for (const [i, line] of lines.entries()) {
4043
const lineContents = line.trim();
4144
if (lineContents.length > 0 && !lineContents.startsWith('#')) {

src/context/FileContextManager.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@ export class FileContextManager {
2222
return undefined;
2323
}
2424

25+
const content = document.contents();
26+
if (!content) {
27+
return undefined;
28+
}
29+
2530
try {
26-
return new FileContext(uri, document.documentType, document.contents());
31+
return new FileContext(uri, document.documentType, content);
2732
} catch (error) {
2833
this.log.error(error, `Failed to create file context ${uri}`);
2934
return undefined;

src/document/Document.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ export class Document {
138138
}
139139

140140
public getText(range?: Range) {
141-
return this.getTextDocument()?.getText(range) ?? '';
141+
return this.getTextDocument()?.getText(range);
142142
}
143143

144-
public getLines(): string[] {
145-
return this.getText().split('\n');
144+
public getLines(): string[] | undefined {
145+
return this.getText()?.split('\n');
146146
}
147147

148148
public positionAt(offset: number) {
@@ -158,7 +158,7 @@ export class Document {
158158
}
159159

160160
public contents() {
161-
return this.getTextDocument()?.getText() ?? '';
161+
return this.getTextDocument()?.getText();
162162
}
163163

164164
public metadata(): DocumentMetadata {
@@ -170,7 +170,7 @@ export class Document {
170170
cfnType: this.cfnFileType,
171171
languageId: this.languageId ?? '',
172172
version: this.version ?? 0,
173-
lineCount: this.lineCount ?? -1,
173+
lineCount: this.lineCount ?? 0,
174174
};
175175
}
176176

@@ -190,6 +190,9 @@ export class Document {
190190

191191
private detectIndentationFromContent(): number | undefined {
192192
const content = this.contents();
193+
if (!content) {
194+
return undefined;
195+
}
193196
const lines = content.split('\n');
194197

195198
const maxLinesToAnalyze = Math.min(lines.length, 30);

src/document/DocumentManager.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,10 @@ export class DocumentManager implements SettingsConfigurable, Closeable {
176176
private emitDocSizeMetrics() {
177177
for (const doc of this.documentMap.values()) {
178178
if (doc.isTemplate()) {
179-
this.telemetry.histogram('documents.template.size.bytes', byteSize(doc.contents()), { unit: 'By' });
179+
const content = doc.contents();
180+
if (content) {
181+
this.telemetry.histogram('documents.template.size.bytes', byteSize(content), { unit: 'By' });
182+
}
180183
}
181184
}
182185
}

src/handlers/DocumentHandler.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export function didOpenHandler(components: ServerComponents): (event: TextDocume
2525
}
2626

2727
const content = document.contents();
28+
if (!content) {
29+
log.error(`No content found for document ${uri}`);
30+
return;
31+
}
2832

2933
if (document.isTemplate() || document.cfnFileType === CloudFormationFileType.Empty) {
3034
try {
@@ -57,6 +61,10 @@ export function didChangeHandler(
5761
// This is the document AFTER changes
5862
const document = new Document(textDocument.uri, () => textDocument);
5963
const finalContent = document.getText();
64+
if (!finalContent) {
65+
log.error(`No content found for document ${documentUri}`);
66+
return;
67+
}
6068

6169
const tree = components.syntaxTreeManager.getSyntaxTree(documentUri);
6270

src/handlers/StackHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export function getTemplateArtifactsHandler(
9797
components.s3Service,
9898
document.documentType,
9999
document.uri,
100-
document.contents(),
100+
document.contents() ?? '',
101101
);
102102
const artifacts = template.getTemplateArtifacts();
103103
return { artifacts };

src/resourceState/ResourceStateImporter.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,16 @@ export class ResourceStateImporter {
443443
// For JSON without Resources section, check if file is essentially empty
444444
if (!resourcesSection) {
445445
try {
446-
const parsed = JSON.parse(document.getText()) as Record<string, unknown>;
446+
const text = document.getText();
447+
if (!text) {
448+
return {
449+
position: { line: 0, character: 0 },
450+
commaPrefixNeeded: false,
451+
newLineSuffixNeeded: false,
452+
replaceEntireFile: true,
453+
};
454+
}
455+
const parsed = JSON.parse(text) as Record<string, unknown>;
447456
const hasContent = Object.keys(parsed).length > 0;
448457

449458
// If no content, replace entire file

src/stacks/actions/StackActionOperations.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,22 @@ export async function processChangeSet(
9797
throw new ResponseError(ErrorCodes.InvalidParams, `Document not found: ${params.uri}`);
9898
}
9999
let templateBody = document.contents();
100+
if (!templateBody) {
101+
throw new ResponseError(ErrorCodes.InvalidParams, `Document content is undefined: ${params.uri}`);
102+
}
100103
let templateS3Url: string | undefined;
101104
let expectedETag: string | undefined;
102105
try {
103106
if (params.s3Bucket) {
104107
const s3KeyPrefix = params.s3Key?.includes('/')
105108
? params.s3Key.slice(0, params.s3Key.lastIndexOf('/'))
106109
: undefined;
107-
const template = new ArtifactExporter(s3Service, document.documentType, document.uri, document.contents());
110+
const template = new ArtifactExporter(
111+
s3Service,
112+
document.documentType,
113+
document.uri,
114+
document.contents() ?? '',
115+
);
108116

109117
const exportedTemplate = await template.export(params.s3Bucket, s3KeyPrefix);
110118

0 commit comments

Comments
 (0)