Skip to content

Commit d4dc61a

Browse files
authored
Merge pull request #399 from codefori/fix/empty-file-cache
Fix cache when files are cleared
2 parents a56ad99 + 9db2587 commit d4dc61a

File tree

2 files changed

+127
-95
lines changed

2 files changed

+127
-95
lines changed

language/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export default class Parser {
165165
return existingCache;
166166
}
167167

168-
if (!baseContent) return null;
168+
if (baseContent === undefined) return null;
169169

170170
let scopes: Cache[] = [];
171171

tests/suite/docs.test.ts

Lines changed: 126 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,127 @@
1-
2-
import setupParser from "../parserSetup";
3-
import Linter from "../../language/linter";
4-
import { test, expect } from "vitest";
5-
6-
const parser = setupParser();
7-
const uri = `source.rpgle`;
8-
9-
test("issue_202", async () => {
10-
const lines = [
11-
`**free`,
12-
`///`,
13-
`// Transform to lowercase`,
14-
`// This procedure will take a string and transform it to lowercase`,
15-
`//`,
16-
`// @param The string`,
17-
`// @return The lowercase value`,
18-
`///`,
19-
`Dcl-Proc ToLower Export;`,
20-
` Dcl-Pi *N Char(20);`,
21-
` stringIn Char(20);`,
22-
` End-pi;`,
23-
``,
24-
` return STRLOWER(stringIn);`,
25-
`End-Proc;`,
26-
].join(`\n`);
27-
28-
const cache = await parser.getDocs(uri, lines, {ignoreCache: true, withIncludes: true});
29-
30-
const toLower = cache.find(`ToLower`);
31-
32-
const titleTag = toLower.tags.find(tag => tag.tag === `title`);
33-
expect(titleTag.content).toBe(`Transform to lowercase`);
34-
35-
const descTag = toLower.tags.find(tag => tag.tag === `description`);
36-
expect(descTag.content).toBe(`This procedure will take a string and transform it to lowercase`);
37-
38-
const tags = toLower.tags;
39-
expect(tags[2]).toEqual({
40-
tag: `param`,
41-
content: `The string`
42-
});
43-
44-
expect(tags[3]).toEqual({
45-
tag: `return`,
46-
content: `The lowercase value`
47-
});
48-
49-
const stringInParam = toLower.subItems[0];
50-
const parmTag = stringInParam.tags.find(tag => tag.tag === `description`);
51-
expect(parmTag.content).toBe(`The string`);
52-
});
53-
54-
test("issue_231", async () => {
55-
const lines = [
56-
`**FREE`,
57-
`Ctl-Opt Main(MainLine);`,
58-
`/// -------------------------------------`,
59-
`// Main`,
60-
`/// -------------------------------------`,
61-
`Dcl-Proc MainLine;`,
62-
` Dcl-Pi MainLine Extpgm('MAINTLINE');`,
63-
` Iof Char(1);`,
64-
` End-Pi;`,
65-
` Dcl-S myString Varchar(20);`,
66-
``,
67-
` myString = CvtToMixed(myString);`,
68-
`End-Proc;`,
69-
``,
70-
`/// -------------------------------------`,
71-
`// CvtToMixed`,
72-
`// Convert the passed string to mixed case or `,
73-
`// what is normally called Title case.`,
74-
`// @param Source string`,
75-
`// @return Title cased string`,
76-
`/// -------------------------------------`,
77-
`Dcl-Proc CvtToMixed;`,
78-
` Dcl-Pi CvtToMixed Extpgm('MAINTLINE');`,
79-
` theString Varchar(100);`,
80-
` End-Pi;`,
81-
``,
82-
` return theString;`,
83-
`End-Proc;`,
84-
].join(`\n`);
85-
86-
const cache = await parser.getDocs(uri, lines, {ignoreCache: true, withIncludes: true});
87-
88-
const { indentErrors, errors } = Linter.getErrors({ uri, content: lines }, {
89-
indent: 2,
90-
PrettyComments: true,
91-
}, cache);
92-
93-
expect(indentErrors.length).toBe(0);
94-
expect(errors.length).toBe(0);
1+
2+
import setupParser from "../parserSetup";
3+
import Linter from "../../language/linter";
4+
import { test, expect } from "vitest";
5+
6+
const parser = setupParser();
7+
const uri = `source.rpgle`;
8+
9+
test("issue_202", async () => {
10+
const lines = [
11+
`**free`,
12+
`///`,
13+
`// Transform to lowercase`,
14+
`// This procedure will take a string and transform it to lowercase`,
15+
`//`,
16+
`// @param The string`,
17+
`// @return The lowercase value`,
18+
`///`,
19+
`Dcl-Proc ToLower Export;`,
20+
` Dcl-Pi *N Char(20);`,
21+
` stringIn Char(20);`,
22+
` End-pi;`,
23+
``,
24+
` return STRLOWER(stringIn);`,
25+
`End-Proc;`,
26+
].join(`\n`);
27+
28+
const cache = await parser.getDocs(uri, lines, { ignoreCache: true, withIncludes: true });
29+
30+
const toLower = cache.find(`ToLower`);
31+
32+
const titleTag = toLower.tags.find(tag => tag.tag === `title`);
33+
expect(titleTag.content).toBe(`Transform to lowercase`);
34+
35+
const descTag = toLower.tags.find(tag => tag.tag === `description`);
36+
expect(descTag.content).toBe(`This procedure will take a string and transform it to lowercase`);
37+
38+
const tags = toLower.tags;
39+
expect(tags[2]).toEqual({
40+
tag: `param`,
41+
content: `The string`
42+
});
43+
44+
expect(tags[3]).toEqual({
45+
tag: `return`,
46+
content: `The lowercase value`
47+
});
48+
49+
const stringInParam = toLower.subItems[0];
50+
const parmTag = stringInParam.tags.find(tag => tag.tag === `description`);
51+
expect(parmTag.content).toBe(`The string`);
52+
});
53+
54+
test("issue_231", async () => {
55+
const lines = [
56+
`**FREE`,
57+
`Ctl-Opt Main(MainLine);`,
58+
`/// -------------------------------------`,
59+
`// Main`,
60+
`/// -------------------------------------`,
61+
`Dcl-Proc MainLine;`,
62+
` Dcl-Pi MainLine Extpgm('MAINTLINE');`,
63+
` Iof Char(1);`,
64+
` End-Pi;`,
65+
` Dcl-S myString Varchar(20);`,
66+
``,
67+
` myString = CvtToMixed(myString);`,
68+
`End-Proc;`,
69+
``,
70+
`/// -------------------------------------`,
71+
`// CvtToMixed`,
72+
`// Convert the passed string to mixed case or `,
73+
`// what is normally called Title case.`,
74+
`// @param Source string`,
75+
`// @return Title cased string`,
76+
`/// -------------------------------------`,
77+
`Dcl-Proc CvtToMixed;`,
78+
` Dcl-Pi CvtToMixed Extpgm('MAINTLINE');`,
79+
` theString Varchar(100);`,
80+
` End-Pi;`,
81+
``,
82+
` return theString;`,
83+
`End-Proc;`,
84+
].join(`\n`);
85+
86+
const cache = await parser.getDocs(uri, lines, { ignoreCache: true, withIncludes: true });
87+
88+
const { indentErrors, errors } = Linter.getErrors({ uri, content: lines }, {
89+
indent: 2,
90+
PrettyComments: true,
91+
}, cache);
92+
93+
expect(indentErrors.length).toBe(0);
94+
expect(errors.length).toBe(0);
95+
});
96+
97+
test("Cache for empty files", async () => {
98+
const emptyLines = ``;
99+
const cache = await parser.getDocs(uri, emptyLines, { ignoreCache: true });
100+
expect(cache.procedures.length).toBe(0);
101+
});
102+
103+
test("Clear cache on change to empty file", async () => {
104+
const lines = [
105+
`**free`,
106+
`ctl-opt nomain;`,
107+
`dcl-proc Add export;`,
108+
` dcl-pi *n int(10);`,
109+
` num1 int(10) value;`,
110+
` num2 int(10) value;`,
111+
` end-pi;`,
112+
` return num1 + num2;`,
113+
`end-proc;`
114+
].join(`\n`);
115+
116+
// First parse should add the proc to the cache
117+
const cache1 = await parser.getDocs(uri, lines, { ignoreCache: true });
118+
expect(cache1.procedures.length).toBe(1);
119+
120+
// Second parse with empty content should clear the cache
121+
const emptyLines = ``;
122+
await parser.getDocs(uri, emptyLines, { ignoreCache: true });
123+
124+
// Verify that the cache is cleared
125+
const cache2 = parser.getParsedCache(uri);
126+
expect(cache2.procedures.length).toBe(0);
95127
});

0 commit comments

Comments
 (0)