Skip to content

Commit

Permalink
No longer check strings inside of SQL statements (#124)
Browse files Browse the repository at this point in the history
Signed-off-by: Liam Barry Allan <[email protected]>
  • Loading branch information
worksofliam committed Sep 21, 2022
1 parent 74269ba commit 85b2c65
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/language/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ module.exports = class Linter {

const statement = Statement.parseStatement(currentStatement);
let value;
let isEmbeddedSQL = false;

if (statement.length >= 1) {
if (statement[0].type === `directive` && statement[0].value.toUpperCase() === `/EOF`) {
Expand Down Expand Up @@ -681,6 +682,7 @@ module.exports = class Linter {
}
break;
case `EXEC`:
isEmbeddedSQL = true;
if (rules.NoSELECTAll) {
if (currentStatementUpper.includes(`SELECT *`)) {
errors.push({
Expand Down Expand Up @@ -756,7 +758,7 @@ module.exports = class Linter {
let part;

if (statement.length > 0 && [`declare`, `end`].includes(statement[0].type) === false) {
const isSQL = (statement[0].type === `word` && statement[0].value.toUpperCase() === `EXEC`);
// const isSQL = (statement[0].type === `word` && statement[0].value.toUpperCase() === `EXEC`);

for (let i = 0; i < statement.length; i++) {
part = statement[i];
Expand Down Expand Up @@ -833,7 +835,7 @@ module.exports = class Linter {
// Check the casing of the reference matches the definition
const definedName = definedNames.find(defName => defName.toUpperCase() === upperName);
if (definedName && definedName !== part.value) {
if (isSQL === false || (isSQL && statement[i-1] && statement[i-1].type === `seperator`)) {
if (isEmbeddedSQL === false || (isEmbeddedSQL && statement[i-1] && statement[i-1].type === `seperator`)) {
errors.push({
range: new vscode.Range(
statementStart,
Expand Down Expand Up @@ -938,7 +940,7 @@ module.exports = class Linter {
break;

case `string`:
if (part.value.substring(1, part.value.length-1).trim() === `` && rules.RequireBlankSpecial) {
if (part.value.substring(1, part.value.length-1).trim() === `` && rules.RequireBlankSpecial && !isEmbeddedSQL) {
errors.push({
range: new vscode.Range(
statementStart,
Expand All @@ -949,7 +951,7 @@ module.exports = class Linter {
newValue: `*BLANK`
});

} else if (rules.StringLiteralDupe) {
} else if (rules.StringLiteralDupe && !isEmbeddedSQL) {
let foundBefore = stringLiterals.find(literal => literal.value === part.value);

// If it does not exist on our list, we can add it
Expand Down
57 changes: 56 additions & 1 deletion tests/suite/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2676,4 +2676,59 @@ exports.linter40 = async () => {
}, cache);

assert.strictEqual(errors.length, 0);
};
};

exports.linter4 = async () => {
const lines = [
`**FREE`,
``,
`Ctl-Opt DFTACTGRP(*No);`,
``,
`Dsply 'aaa';`,
`DSPLY '';`,
`Dsply 'aaa';`,
``,
`EXEC SQL`,
` Select nullif('aaa', '') from sysibm/sysdummy1;`,
`Return;`
].join(`\n`);

const parser = new Parser();
const cache = await parser.getDocs(uri, lines);
const { errors } = Linter.getErrors({uri, content: lines}, {
RequireBlankSpecial: true,
StringLiteralDupe: true
}, cache);

assert.strictEqual(errors.length, 3);

assert.deepStrictEqual(errors[0], {
range: new vscode.Range(5, 0, 5, 8),
offset: {
position: 6,
length: 8
},
type: `RequireBlankSpecial`,
newValue: `*BLANK`
});

assert.deepStrictEqual(errors[1], {
range: new vscode.Range(4, 0, 4, 11),
offset: {
position: 6,
length: 11
},
type: `StringLiteralDupe`,
newValue: undefined
});

assert.deepStrictEqual(errors[2], {
range: new vscode.Range(6, 0, 6, 11),
offset: {
position: 6,
length: 11
},
type: `StringLiteralDupe`,
newValue: undefined
});
}

0 comments on commit 85b2c65

Please sign in to comment.