Skip to content

Commit d5c1206

Browse files
authored
Merge pull request #153 from IBM/feature/ignore_undefined_object_types
Ignore unknown object types
2 parents c73b6bc + 2ac6f32 commit d5c1206

File tree

16 files changed

+75
-82
lines changed

16 files changed

+75
-82
lines changed

cli/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ async function main() {
177177
let files: string[];
178178

179179
if (!scanGlob) {
180-
scanGlob = targets.getSearchGlob();
180+
scanGlob = Targets.LanguageProvider.getGlob();
181181
}
182182

183183
try {

cli/src/targets/index.ts

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ export class Targets {
9595
return ignoredObjects;
9696
}
9797

98-
getSearchGlob(): string {
99-
return Targets.LanguageProvider.getGlob();
100-
}
101-
10298
public getCwd() {
10399
return this.cwd;
104100
}
@@ -139,12 +135,12 @@ export class Targets {
139135
this.resolvedObjects[localPath] = ileObject;
140136
}
141137

142-
public async loadProject(withRef?: string) {
143-
if (withRef) {
144-
await this.handleRefsFile(path.join(this.cwd, withRef));
138+
public async loadProject(options: { withRef?: string, additionalExtensions?: string[] } = {}) {
139+
if (options.withRef) {
140+
await this.handleRefsFile(path.join(this.cwd, options.withRef));
145141
}
146142

147-
const initialFiles = await this.fs.getFiles(this.cwd, this.getSearchGlob());
143+
const initialFiles = await this.fs.getFiles(this.cwd, Targets.LanguageProvider.getGlob(options.additionalExtensions));
148144
await this.loadObjectsFromPaths(initialFiles);
149145
await Promise.allSettled(initialFiles.map(f => this.parseFile(f)));
150146
}
@@ -153,7 +149,7 @@ export class Targets {
153149
return [`MODULE`, `PGM`].includes(Targets.LanguageProvider.getObjectType(ext));
154150
}
155151

156-
public async resolvePathToObject(localPath: string, newText?: string) {
152+
public async resolvePathToObject(localPath: string, newText?: string): Promise<ILEObject|undefined> {
157153
if (this.resolvedObjects[localPath]) {
158154
if (newText) this.resolvedObjects[localPath].text = newText;
159155
return this.resolvedObjects[localPath];
@@ -168,6 +164,15 @@ export class Targets {
168164
const name = getSystemNameFromPath(hasProgramAttribute ? detail.name.substring(0, detail.name.length - 4) : detail.name);
169165
const type: ObjectType = (isProgram ? "PGM" : this.getObjectType(relativePath, extension));
170166

167+
if (!type) {
168+
this.logger.fileLog(relativePath, {
169+
type: `warning`,
170+
message: `Unknown object type for ${relativePath} with extension ${extension}.`
171+
});
172+
173+
return;
174+
}
175+
171176
const theObject: ILEObject = {
172177
systemName: name,
173178
type: type,
@@ -371,19 +376,8 @@ export class Targets {
371376
}
372377

373378
// TODO: move this to language provider
374-
getObjectType(relativePath: string, ext: string): ObjectType {
375-
const objType = Targets.LanguageProvider.getObjectType(ext);
376-
377-
if (!objType) {
378-
this.logger.fileLog(relativePath, {
379-
type: `warning`,
380-
message: `'${ext}' not found a matching object type. Defaulting to '${ext}'`
381-
});
382-
383-
return (ext.toUpperCase() as ObjectType);
384-
}
385-
386-
return objType;
379+
getObjectType(relativePath: string, ext: string): ObjectType|undefined {
380+
return Targets.LanguageProvider.getObjectType(ext);
387381
}
388382

389383
public loadObjectsFromPaths(paths: string[]) {
@@ -408,9 +402,6 @@ export class Targets {
408402
try {
409403
const content = await this.fs.readFile(filePath);
410404

411-
// Really only applied to rpg
412-
const isFree = (content.length >= 6 ? content.substring(0, 6).toLowerCase() === `**free` : false);
413-
414405
let textMatch;
415406
try {
416407
[textMatch] = content.match(TextRegex);
@@ -422,11 +413,14 @@ export class Targets {
422413
} catch (e) { }
423414

424415
const options: FileOptions = {
425-
isFree,
426416
text: textMatch
427417
};
428418

429-
await Targets.LanguageProvider.handleLanguage(this, filePath, content, options);
419+
const ileObject = await this.resolvePathToObject(filePath, options.text);
420+
if (ileObject) {
421+
await Targets.LanguageProvider.handleLanguage(this, filePath, content, ileObject);
422+
}
423+
430424
} catch (e) {
431425
this.logger.fileLog(relative, {
432426
message: `Failed to parse file.`,

cli/src/targets/languages.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FileOptions, ObjectType, Targets } from ".";
1+
import { FileOptions, ILEObject, ObjectType, Targets } from ".";
22
import { clExtensions, clleTargetCallback, clObjects } from "./languages/clle";
33
import { ddsExtension, ddsObjects, ddsTargetCallback } from "./languages/dds";
44
import { rpgleExtensions, rpgleObjects, rpgleTargetCallback } from "./languages/rpgle";
@@ -7,7 +7,7 @@ import { binderExtensions, binderObjects, binderTargetCallback } from "./languag
77
import { cmdExtensions, cmdObjects, cmdTargetCallback } from "./languages/cmd";
88
import { noSourceObjects, noSourceTargetCallback, noSourceTargetObjects } from "./languages/nosrc";
99

10-
export type LanguageCallback = (targets: Targets, relativePath: string, content: string, options: FileOptions) => Promise<void>
10+
export type LanguageCallback = (targets: Targets, relativePath: string, content: string, ileObject: ILEObject) => Promise<void>
1111
interface LanguageGroup {
1212
extensions: string[];
1313
callback: LanguageCallback;
@@ -33,16 +33,16 @@ export class TargetsLanguageProvider {
3333
return this.languageTargets.map(lang => lang.extensions).flat();
3434
}
3535

36-
public getGlob() {
37-
const allExtensions = this.getExtensions();
36+
public getGlob(additionalExtensions: string[] = []): string {
37+
const allExtensions = this.getExtensions().concat(additionalExtensions);
3838
return `**/*.{${allExtensions.join(`,`)},${allExtensions.map(e => e.toUpperCase()).join(`,`)}}`;
3939
}
4040

41-
public async handleLanguage(targets: Targets, relativePath: string, content: string, options: FileOptions = {}) {
41+
public async handleLanguage(targets: Targets, relativePath: string, content: string, ileObject: ILEObject) {
4242
const ext = relativePath.split('.').pop()?.toLowerCase();
4343
const language = this.languageTargets.find(lang => lang.extensions.includes(ext));
4444
if (ext && language) {
45-
await language.callback(targets, relativePath, content, options);
45+
await language.callback(targets, relativePath, content, ileObject);
4646
}
4747
}
4848

cli/src/targets/languages/binder.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from "path";
22
import { CLParser, DefinitionType, Module, File } from "vscode-clle/language";
3-
import { FileOptions, ILEObjectTarget, Targets } from "..";
3+
import { FileOptions, ILEObject, ILEObjectTarget, Targets } from "..";
44
import { infoOut } from "../../cli";
55
import { trimQuotes } from "../../utils";
66
import { ExtensionMap } from "../languages";
@@ -11,15 +11,13 @@ export const binderObjects: ExtensionMap = {
1111
bnd: `SRVPGM`,
1212
}
1313

14-
export async function binderTargetCallback(targets: Targets, localPath: string, content: string, options: FileOptions) {
14+
export async function binderTargetCallback(targets: Targets, localPath: string, content: string, ileObject: ILEObject) {
1515
const clDocs = new CLParser();
1616
const tokens = clDocs.parseDocument(content);
1717

1818
const module = new Module();
1919
module.parseStatements(tokens);
2020

21-
const ileObject = await targets.resolvePathToObject(localPath, options.text);
22-
2321
const target: ILEObjectTarget = {
2422
...ileObject,
2523
deps: [],

cli/src/targets/languages/clle.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from "path";
22
import { CLParser, DefinitionType, Module, File } from "vscode-clle/language";
3-
import { FileOptions, ILEObjectTarget, Targets } from "..";
3+
import { FileOptions, ILEObject, ILEObjectTarget, Targets } from "..";
44
import { infoOut } from "../../cli";
55
import { ExtensionMap } from "../languages";
66

@@ -11,15 +11,13 @@ export const clObjects: ExtensionMap = {
1111
clp: `PGM`
1212
}
1313

14-
export async function clleTargetCallback(targets: Targets, filePath: string, content: string, options: FileOptions) {
14+
export async function clleTargetCallback(targets: Targets, filePath: string, content: string, ileObject: ILEObject) {
1515
const clDocs = new CLParser();
1616
const tokens = clDocs.parseDocument(content);
1717

1818
const module = new Module();
1919
module.parseStatements(tokens);
2020

21-
const ileObject = await targets.resolvePathToObject(filePath);
22-
2321
const pathDetail = path.parse(filePath);
2422
const target: ILEObjectTarget = {
2523
...ileObject,

cli/src/targets/languages/cmd.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { FileOptions, Targets } from "..";
1+
import { FileOptions, ILEObject, Targets } from "..";
22
import { ExtensionMap } from "../languages";
33

44
export const cmdExtensions = [`cmd`];
55
export const cmdObjects: ExtensionMap = {
66
cmd: `CMD`
77
}
88

9-
export async function cmdTargetCallback(targets: Targets, localPath: string, content: string, options: FileOptions) {
10-
targets.resolvePathToObject(localPath, options.text);
9+
export async function cmdTargetCallback(targets: Targets, localPath: string, content: string, ileObject: ILEObject) {
10+
// Do nothing!
1111
}

cli/src/targets/languages/dds.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from "path";
22
import { DisplayFile as dds } from "vscode-displayfile/src/dspf";
3-
import { FileOptions, ILEObjectTarget, Targets } from "..";
3+
import { FileOptions, ILEObject, ILEObjectTarget, Targets } from "..";
44
import { infoOut } from "../../cli";
55
import { ExtensionMap } from "../languages";
66

@@ -12,14 +12,12 @@ export const ddsObjects: ExtensionMap = {
1212
prtf: `FILE`
1313
}
1414

15-
export async function ddsTargetCallback(targets: Targets, filePath: string, content: string, options: FileOptions) {
15+
export async function ddsTargetCallback(targets: Targets, filePath: string, content: string, ileObject: ILEObject) {
1616
const eol = content.indexOf(`\r\n`) >= 0 ? `\r\n` : `\n`;
1717

1818
const ddsFile = new dds();
1919
ddsFile.parse(content.split(eol));
2020

21-
const ileObject = await targets.resolvePathToObject(filePath, options.text);
22-
2321
const target: ILEObjectTarget = {
2422
...ileObject,
2523
deps: []

cli/src/targets/languages/nosrc.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Targets, FileOptions } from "..";
1+
import { Targets, FileOptions, ILEObject } from "..";
22
import { ExtensionMap } from "../languages";
33

44
export const noSourceObjects = [`dtaara`, `mnucmd`, `msgf`, `dtaq`, `bnddir`];
@@ -10,6 +10,6 @@ export const noSourceTargetObjects: ExtensionMap = {
1010
bnddir: `BNDDIR`
1111
}
1212

13-
export async function noSourceTargetCallback(targets: Targets, localPath: string, content: string, options: FileOptions) {
14-
targets.resolvePathToObject(localPath, options.text);
13+
export async function noSourceTargetCallback(targets: Targets, localPath: string, content: string, ileObject: ILEObject) {
14+
// Do nothing!
1515
}

cli/src/targets/languages/rpgle.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from "path";
2-
import { FileOptions, ILEObjectTarget, Targets } from "..";
2+
import { FileOptions, ILEObject, ILEObjectTarget, Targets } from "..";
33
import { infoOut } from "../../cli";
44
import Parser from "vscode-rpgle/language/parser";
55
import { IncludeStatement } from "vscode-rpgle/language/parserTypes";
@@ -20,7 +20,7 @@ interface RpgLookup {
2020

2121
const includeFileCache: { [path: string]: string } = {};
2222

23-
export async function rpgleTargetCallback(targets: Targets, localPath: string, content: string, options: FileOptions) {
23+
export async function rpgleTargetCallback(targets: Targets, localPath: string, content: string, ileObject: ILEObject) {
2424
const parser = setupParser(targets);
2525

2626
const cache = await parser.getDocs(
@@ -33,9 +33,9 @@ export async function rpgleTargetCallback(targets: Targets, localPath: string, c
3333
);
3434

3535
if (cache) {
36-
const ileObject = await targets.resolvePathToObject(localPath, options.text);
37-
36+
const isFree = (content.length >= 6 ? content.substring(0, 6).toLowerCase() === `**free` : false);
3837
const pathDetail = path.parse(localPath);
38+
3939
// define internal imports
4040
ileObject.imports = cache.procedures
4141
.filter((proc: any) => proc.keyword[`EXTPROC`] && !proc.keyword[`EXPORT`])
@@ -125,7 +125,7 @@ export async function rpgleTargetCallback(targets: Targets, localPath: string, c
125125
type: `includeFix`,
126126
line: include.line,
127127
change: {
128-
lineContent: (options.isFree ? `` : ``.padEnd(6)) + `/copy '${theIncludePath}'`
128+
lineContent: (isFree ? `` : ``.padEnd(6)) + `/copy '${theIncludePath}'`
129129
}
130130
});
131131
} else {

cli/src/targets/languages/sql.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const sqlObjects: ExtensionMap = {
3737
'sqltrg': `PGM`
3838
}
3939

40-
export async function sqlTargetCallback(targets: Targets, localPath: string, content: string, options: FileOptions) {
40+
export async function sqlTargetCallback(targets: Targets, localPath: string, content: string, ileObject: ILEObject) {
4141
const document = new Document(content);
4242

4343
const pathDetail = path.parse(localPath);
@@ -130,16 +130,18 @@ export async function sqlTargetCallback(targets: Targets, localPath: string, con
130130
let hasLongName = mainDef.object.name && mainDef.object.name.length > 10 ? mainDef.object.name : undefined;
131131
let objectName = mainDef.object.system || trimQuotes(mainDef.object.name, `"`);
132132

133-
const extension = pathDetail.ext.substring(1);
133+
// let ileObject: ILEObject = {
134+
// systemName: objectName.toUpperCase(),
135+
// longName: hasLongName,
136+
// type: targets.getObjectType(relativePath, mainDef.createType),
137+
// text: defaultObject.text,
138+
// relativePath,
139+
// extension
140+
// }
134141

135-
let ileObject: ILEObject = {
136-
systemName: objectName.toUpperCase(),
137-
longName: hasLongName,
138-
type: targets.getObjectType(relativePath, mainDef.createType),
139-
text: options.text,
140-
relativePath,
141-
extension
142-
}
142+
ileObject.systemName = objectName.toUpperCase();
143+
ileObject.longName = hasLongName;
144+
ileObject.type = targets.getObjectType(relativePath, mainDef.createType);
143145

144146
let suggestRename = false;
145147
const sqlFileName = pathDetail.name;
@@ -152,7 +154,7 @@ export async function sqlTargetCallback(targets: Targets, localPath: string, con
152154
}
153155

154156
// Then make an extension suggestion
155-
if (extension.toUpperCase() === `SQL` && mainDef.createType) {
157+
if (ileObject.extension.toUpperCase() === `SQL` && mainDef.createType) {
156158
suggestRename = true;
157159
}
158160

@@ -214,9 +216,6 @@ export async function sqlTargetCallback(targets: Targets, localPath: string, con
214216
infoOut(`Depends on: ${newTarget.deps.map(d => `${d.systemName}.${d.type}`).join(` `)}`);
215217
}
216218

217-
// So we can later resolve the path to the created object
218-
targets.storeResolved(localPath, ileObject);
219-
220219
targets.addNewTarget(newTarget);
221220

222221
// If the extension is SQL, let's make better suggestions

0 commit comments

Comments
 (0)