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

Commit a41c691

Browse files
authored
Fix update section bug when there are errors (#1017)
* fix bug * add newline for test case * remove only
1 parent 635a08e commit a41c691

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

packages/lu/src/parser/lufile/sectionOperator.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class SectionOperator {
141141
let index = -1;
142142

143143
while ((index = errors.findIndex(u =>
144-
u.Range && ((u.Range.Start.Line >= startLine && u.Range.Start.Line <= endLine)
144+
!u.Range || ((u.Range.Start.Line >= startLine && u.Range.Start.Line <= endLine)
145145
|| (u.Range.End.Line >= startLine && u.Range.End.Line <= endLine)))) >= 0) {
146146
this.Luresource.Errors.splice(index, 1);
147147
}
@@ -156,13 +156,13 @@ class SectionOperator {
156156
});
157157
} else if (startLine >= 0 && (endLine === undefined || endLine < startLine)) {
158158
errors.forEach(u => {
159-
if (u.Range.Start.Line >= startLine) {
159+
if (u.Range && u.Range.Start.Line >= startLine) {
160160
this.adjustErrorRange(u, offset);
161161
}
162162
});
163163
} else if (startLine >= 0 && endLine >= startLine) {
164164
errors.forEach(u => {
165-
if (u.Range.Start.Line >= startLine && u.Range.End.Line <= endLine) {
165+
if (u.Range && u.Range.Start.Line >= startLine && u.Range.End.Line <= endLine) {
166166
this.adjustErrorRange(u, offset);
167167
}
168168
});

packages/lu/test/parser/lufile/sectionapi.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,33 @@ describe('Section range tests', () => {
497497
assert.equal(luresource.Sections[1].Range.End.Line, 11)
498498
assert.equal(luresource.Sections[1].Range.End.Character, 7)
499499
});
500+
501+
it('simple intent section with errors test', () => {
502+
let fileContent =
503+
`#WhoAreYou
504+
- my name is {@userName=luhan}
505+
@ prebuilt personName hasRoles
506+
`;
507+
508+
let updatedContent =
509+
`#WhoAreYou
510+
- my name is {@userName=luhan}
511+
@ prebuilt personName hasRoles userName`;
512+
513+
let luresource = luparser.parse(fileContent);
514+
515+
assert.equal(luresource.Errors.length, 2);
516+
assert.equal(luresource.Sections.length, 1);
517+
assert.equal(luresource.Content.replace(/\r\n/g, "\n"), `${fileContent}`);
518+
assert.equal(`#WhoAreYou${NEWLINE}${luresource.Sections[0].Body}`.replace(/\r\n/g, "\n"), fileContent);
519+
520+
luresource = new SectionOperator(luresource).updateSection(luresource.Sections[0].Id, updatedContent);
521+
522+
assert.equal(luresource.Errors.length, 0);
523+
assert.equal(luresource.Sections.length, 1);
524+
assert.equal(luresource.Content.replace(/\r\n/g, "\n"), updatedContent);
525+
assert.equal(`#WhoAreYou${NEWLINE}${luresource.Sections[0].Body}`.replace(/\r\n/g, "\n"), updatedContent);
526+
});
500527
})
501528

502529
describe('Section CRUD tests for insert and update sections with newline', () => {

0 commit comments

Comments
 (0)