Skip to content

Commit

Permalink
Refactor tests and linter to use matchObject for error assertions and…
Browse files Browse the repository at this point in the history
… simplify offset handling

Signed-off-by: worksofliam <[email protected]>
  • Loading branch information
worksofliam committed Dec 11, 2024
1 parent 71b6491 commit d7cf530
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 107 deletions.
46 changes: 23 additions & 23 deletions language/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import opcodes from "./models/opcodes";
import Document from "./document";
import { IssueRange, Rules, SelectBlock } from "./parserTypes";
import Declaration from "./models/declaration";
import { IRange } from "./types";
import { IRange, Token } from "./types";

const BANNED_FROM_INCLUDES = [`NoUnreferenced`];
const INCLUDE_EXTENSIONS = [`rpgleinc`, `rpgleh`];
Expand Down Expand Up @@ -199,7 +199,7 @@ export default class Linter {
if (lineNumber > 0 && statement[0].value.startsWith(`**`)) {
if (rules.NoCTDATA) {
errors.push({
offset: { start: statement[0].range.start, end: statement[0].range.end },
offset: statement[0].range,
type: `NoCTDATA`,
});
}
Expand Down Expand Up @@ -295,7 +295,7 @@ export default class Linter {
}
if (correctValue !== correctDirective) {
errors.push({
offset: { start: statement[0].range.start, end: statement[0].range.end },
offset: statement[0].range,
type: `CopybookDirective`,
newValue: correctDirective
});
Expand All @@ -307,7 +307,7 @@ export default class Linter {
if (rules.DirectiveCase === `lower`) {
if (value !== value.toLowerCase()) {
errors.push({
offset: { start: statement[0].range.start, end: statement[0].range.end },
offset: statement[0].range,
type: `DirectiveCase`,
newValue: value.toLowerCase()
});
Expand All @@ -317,7 +317,7 @@ export default class Linter {
if (rules.DirectiveCase === `upper`) {
if (value !== value.toUpperCase()) {
errors.push({
offset: { start: statement[0].range.start, end: statement[0].range.end },
offset: statement[0].range,
type: `DirectiveCase`,
newValue: value.toUpperCase()
});
Expand All @@ -342,7 +342,7 @@ export default class Linter {
}
if (statement[0].value !== expected) {
errors.push({
offset: { start: statement[0].range.start, end: statement[0].range.end },
offset: statement[0].range,
type: `SpecificCasing`,
newValue: expected
});
Expand All @@ -363,7 +363,7 @@ export default class Linter {
case `BEGSR`:
if (inSubroutine) {
errors.push({
offset: { start: statement[0].range.start, end: statement[0].range.end },
offset: statement[0].range,
type: `UnexpectedEnd`,
});
}
Expand All @@ -380,7 +380,7 @@ export default class Linter {
} else {
if (rules.NoGlobalSubroutines) {
errors.push({
offset: { start: statement[0].range.start, end: statement[0].range.end },
offset: statement[0].range,
type: `NoGlobalSubroutines`
});
}
Expand All @@ -389,7 +389,7 @@ export default class Linter {
case `DCL-PROC`:
if (inSubroutine || inProcedure) {
errors.push({
offset: { start: statement[0].range.start, end: statement[0].range.end },
offset: statement[0].range,
type: `UnexpectedEnd`,
});
}
Expand Down Expand Up @@ -537,7 +537,7 @@ export default class Linter {
}
if (statement[0].value !== expected) {
errors.push({
offset: { start: statement[0].range.start, end: statement[0].range.end },
offset: statement[0].range,
type: `SpecificCasing`,
newValue: expected
});
Expand All @@ -549,7 +549,7 @@ export default class Linter {
case `ENDSR`:
if (!inSubroutine) {
errors.push({
offset: { start: statement[0].range.start, end: statement[0].range.end },
offset: statement[0].range,
type: `UnexpectedEnd`,
});
} else {
Expand All @@ -559,7 +559,7 @@ export default class Linter {
if (inProcedure === false) {
if (rules.NoGlobalSubroutines) {
errors.push({
offset: { start: statement[0].range.start, end: statement[0].range.end },
offset: statement[0].range,
type: `NoGlobalSubroutines`
});
}
Expand All @@ -574,7 +574,7 @@ export default class Linter {
case `END-PROC`:
if (inProcedure === false || inSubroutine) {
errors.push({
offset: { start: statement[0].range.start, end: statement[0].range.end },
offset: statement[0].range,
type: `UnexpectedEnd`,
});
}
Expand All @@ -585,7 +585,7 @@ export default class Linter {
case `END-PI`:
if (inPrototype === false) {
errors.push({
offset: { start: statement[0].range.start, end: statement[0].range.end },
offset: statement[0].range,
type: `UnexpectedEnd`,
});
}
Expand All @@ -612,7 +612,7 @@ export default class Linter {
}
if (statement[0].value !== expected) {
errors.push({
offset: { start: statement[0].range.start, end: statement[0].range.end },
offset: statement[0].range,
type: `SpecificCasing`,
newValue: expected
});
Expand Down Expand Up @@ -699,7 +699,7 @@ export default class Linter {
const prior = statement[index - 1];
if (prior && ![`dot`, `seperator`].includes(prior.type)) {
errors.push({
offset: { start: part.range.start, end: part.range.end },
offset: part.range,
type: `SQLHostVarCheck`,
newValue: `:${part.value}`
});
Expand Down Expand Up @@ -766,7 +766,7 @@ export default class Linter {
}
}

let part;
let part: Token;

if (statement.length > 0) {
// const isSQL = (statement[0].type === `word` && statement[0].value.toUpperCase() === `EXEC`);
Expand All @@ -791,7 +791,7 @@ export default class Linter {
}
if (part.value !== expected) {
errors.push({
offset: { start: part.range.start, end: part.range.end },
offset: part.range,
type: `SpecificCasing`,
newValue: expected
});
Expand All @@ -808,7 +808,7 @@ export default class Linter {
const existingVariable = globalScope.variables.find(variable => variable.name.toUpperCase() === upperName);
if (existingVariable) {
errors.push({
offset: { start: part.range.start, end: part.range.end },
offset: part.range,
type: `NoGlobalsInProcedures`,
});
}
Expand All @@ -826,7 +826,7 @@ export default class Linter {
const definedName = definedNames.find(defName => defName.toUpperCase() === upperName);
if (definedName && definedName !== part.value) {
errors.push({
offset: { start: part.range.start, end: part.range.end },
offset: part.range,
type: `IncorrectVariableCase`,
newValue: definedName
});
Expand Down Expand Up @@ -855,7 +855,7 @@ export default class Linter {

if (requiresBlock) {
errors.push({
offset: { start: part.range.start, end: part.range.end },
offset: part.range,
type: `RequiresParameter`,
});
}
Expand All @@ -867,7 +867,7 @@ export default class Linter {
case `string`:
if (part.value.substring(1, part.value.length - 1).trim() === `` && rules.RequireBlankSpecial && !isEmbeddedSQL) {
errors.push({
offset: { start: part.range.start, end: part.range.end },
offset: part.range,
type: `RequireBlankSpecial`,
newValue: `*BLANK`
});
Expand All @@ -888,7 +888,7 @@ export default class Linter {
// Then add our new found literal location to the list
foundBefore.list.push({
line: lineNumber,
offset: { start: part.range.start, end: part.range.end }
offset: part.range
});
}
break;
Expand Down
22 changes: 11 additions & 11 deletions tests/suite/directives.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ test('incorrectEnd1', async () => {
PrettyComments: true
}, cache);

expect(errors[0]).toEqual({
expect(errors[0]).toMatchObject({
offset: { start: 45, end: 53 }, type: `UnexpectedEnd`
});

Expand All @@ -332,7 +332,7 @@ test('incorrectEnd2', async () => {
PrettyComments: true
}, cache);

expect(errors[0]).toEqual({
expect(errors[0]).toMatchObject({
offset: { start: 48, end: 53 }, type: `UnexpectedEnd`
});

Expand All @@ -354,7 +354,7 @@ test('incorrectEnd3', async () => {
PrettyComments: true
}, cache);

expect(errors[0]).toEqual({
expect(errors[0]).toMatchObject({
offset: { start: 44, end: 52 }, type: `UnexpectedEnd`
});

Expand Down Expand Up @@ -383,7 +383,7 @@ test('incorrectEnd4', async () => {
PrettyComments: true
}, cache);

expect(errors[0]).toEqual({
expect(errors[0]).toMatchObject({
offset: { start: 187, end: 195 }, type: `UnexpectedEnd`
});
})
Expand Down Expand Up @@ -483,19 +483,19 @@ test('variable_case1', async () => {

expect(errors.length).toBe(3);

expect(errors[0]).toEqual({
expect(errors[0]).toMatchObject({
offset: { start: 86, end: 100 },
type: `IncorrectVariableCase`,
newValue: `CustomerName_t`
});

expect(errors[1]).toEqual({
expect(errors[1]).toMatchObject({
offset: { start: 174, end: 188 },
type: `IncorrectVariableCase`,
newValue: `CustomerName_t`
});

expect(errors[2]).toEqual({
expect(errors[2]).toMatchObject({
offset: { start: 218, end: 232 },
type: `IncorrectVariableCase`,
newValue: `CustomerName_t`
Expand Down Expand Up @@ -544,13 +544,13 @@ test('uppercase1', async () => {

expect(errors.length).toBe(2);

expect(errors[0]).toEqual({
expect(errors[0]).toMatchObject({
offset: { start: 31, end: 36 },
type: `DirectiveCase`,
newValue: `/COPY`
});

expect(errors[1]).toEqual({
expect(errors[1]).toMatchObject({
offset: { start: 59, end: 64 },
type: `DirectiveCase`,
newValue: `/COPY`
Expand All @@ -577,13 +577,13 @@ test('lowercase1', async () => {

expect(errors.length).toBe(2);

expect(errors[0]).toEqual({
expect(errors[0]).toMatchObject({
offset: { start: 59, end: 64 },
type: `DirectiveCase`,
newValue: `/copy`
});

expect(errors[1]).toEqual({
expect(errors[1]).toMatchObject({
offset: { start: 87, end: 92 },
type: `DirectiveCase`,
newValue: `/copy`
Expand Down
Loading

0 comments on commit d7cf530

Please sign in to comment.