Skip to content

Commit

Permalink
Implement #3
Browse files Browse the repository at this point in the history
  • Loading branch information
worksofliam committed Nov 18, 2021
1 parent 4ce7969 commit e56388d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
56 changes: 52 additions & 4 deletions src/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const vscode = require(`vscode`);

const Cache = require(`./models/cache`);
const Declaration = require(`./models/declaration`);
const Statement = require(`./statement`);
const oneLineTriggers = require(`./models/oneLineTriggers`);

Expand All @@ -24,6 +25,7 @@ const errorText = {
'CopybookDirective': `Directive does not match requirement.`,
'UppercaseDirectives': `Directives must be in uppercase.`,
'NoSQLJoins': `SQL joins are not allowed. Consider creating a view instead.`,
'NoGlobalsInProcedures': `Global variables should not be referenced in procedures.`,
}

module.exports = class Linter {
Expand Down Expand Up @@ -51,6 +53,7 @@ module.exports = class Linter {
* CopybookDirective?: "copy"|"include"
* UppercaseDirectives?: boolean,
* NoSQLJoins?: boolean,
* NoGlobalsInProcedures?: boolean,
* SpecificCasing?: {operation: string, expected: string}[],
* }} rules
* @param {Cache|null} [definitions]
Expand All @@ -61,18 +64,29 @@ module.exports = class Linter {

const indent = rules.indent || 2;

let definedNames = [];
/** @type {string[]} */
let definedNames = []

/** @type {Declaration[]} */
let globalVariables = [];

if (definitions) {
globalVariables = [
...definitions.variables,
...definitions.structs
]

definedNames = [
...definitions.constants.map(def => def.name),
...definitions.variables.map(def => def.name),
...definitions.procedures.map(def => def.name),
...definitions.subroutines.map(def => def.name),
...definitions.structs.map(def => def.name)
...globalVariables.map(def => def.name),
];
}

let inProcedure = false;
let inPrototype = false;

let lineNumber = -1;

/** @type {{line: number, expectedIndent: number, currentIndent: number}[]} */
Expand All @@ -86,7 +100,7 @@ module.exports = class Linter {
* "NoOCCURS"|"NoSELECTAll"|"UselessOperationCheck"|"UppercaseConstants"|"SpecificCasing"|
* "InvalidDeclareNumber"|"IncorrectVariableCase"|"RequiresParameter"|
* "RequiresProcedureDescription"|"StringLiteralDupe"|"RequireBlankSpecial"|
* "CopybookDirective"|"UppercaseDirectives"|"NoSQLJoins",
* "CopybookDirective"|"UppercaseDirectives"|"NoSQLJoins"|"NoGlobalsInProcedures",
* newValue?: string}[]
* } */
let errors = [];
Expand Down Expand Up @@ -234,6 +248,7 @@ module.exports = class Linter {

switch (statement[0].value.toUpperCase()) {
case `DCL-PROC`:
inProcedure = true;
if (statement.length < 2) break;
if (rules.RequiresProcedureDescription) {
value = statement[1].value;
Expand Down Expand Up @@ -267,6 +282,12 @@ module.exports = class Linter {
}
break;

case `DCL-PI`:
if (!statement.some(s => s.type === `end`)) {
inPrototype = true;
}
break;

case `DCL-PR`:
if (rules.PrototypeCheck) {
// Unneeded PR
Expand Down Expand Up @@ -311,6 +332,17 @@ module.exports = class Linter {

break;

case `end`:
switch (statement[0].value.toUpperCase()) {
case `END-PROC`:
inProcedure = false;
break;
case `END-PI`:
inPrototype = false;
break;
}
break;

case `word`:
value = statement[0].value.toUpperCase();

Expand Down Expand Up @@ -397,6 +429,22 @@ module.exports = class Linter {

if (part.type === `word` && part.value) {
const upperName = part.value.toUpperCase();

if (rules.NoGlobalsInProcedures) {
if (inProcedure && !inPrototype) {
const existingVariable = globalVariables.find(variable => variable.name.toUpperCase() === upperName);
if (existingVariable) {
errors.push({
range: new vscode.Range(
statementStart,
statementEnd
),
offset: {position: part.position, length: part.position + part.value.length},
type: `NoGlobalsInProcedures`
});
}
}
}

if (rules.IncorrectVariableCase) {
// Check the casing of the reference matches the definition
Expand Down
5 changes: 5 additions & 0 deletions src/schemas/rpglint.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@
"type": "boolean",
"description": "JOINs in Embedded SQL are not allowed."
},
"NoGlobalsInProcedures": {
"$id": "#/properties/NoGlobalsInProcedures",
"type": "boolean",
"description": "Globals are not allowed in procedures."
},
"SpecificCasing": {
"$id": "#/properties/SpecificCasing",
"type": "array",
Expand Down

0 comments on commit e56388d

Please sign in to comment.