|
| 1 | +const isSqlQuery = require('../utils/sqlParser.js'); |
| 2 | +const isTagged = require('../utils/isTagged.js'); |
| 3 | +const getLocation = require('../utils/getLocation.js'); |
| 4 | + |
| 5 | +/** |
| 6 | + * no-shorthand-offset |
| 7 | + * MySQL permits the non-standard `LIMIT offset, count` syntax, but it is not portable to other SQL databases. |
| 8 | + * So this rule will flag the potential use of `LIMIT offset, count` syntax. |
| 9 | + */ |
| 10 | +module.exports = { |
| 11 | + meta: { |
| 12 | + type: 'suggestion', |
| 13 | + docs: { |
| 14 | + description: 'Disallow the use of `LIMIT offset, count` syntax', |
| 15 | + category: 'non-standard SQL', |
| 16 | + }, |
| 17 | + messages: { |
| 18 | + nonStandardSQL: |
| 19 | + 'Non-standard SQL syntax `LIMIT offset, count` is not portable', |
| 20 | + }, |
| 21 | + }, |
| 22 | + |
| 23 | + /** |
| 24 | + * Create `no-shorthand-offset` rule |
| 25 | + * |
| 26 | + * @param {object} context - Eslint Context object |
| 27 | + * @returns {object} Object rule |
| 28 | + */ |
| 29 | + create(context) { |
| 30 | + /** |
| 31 | + * Validate node. |
| 32 | + * @param {object} templateLiteralNode - Node |
| 33 | + * @returns {void} |
| 34 | + */ |
| 35 | + function validate(templateLiteralNode) { |
| 36 | + const {parent} = templateLiteralNode; |
| 37 | + |
| 38 | + // Tagged with a 'SQL' like name? |
| 39 | + const tagged = isTagged(parent); |
| 40 | + |
| 41 | + // Join up the parts... |
| 42 | + const literal = templateLiteralNode.quasis |
| 43 | + .map(quasi => quasi.value.raw) |
| 44 | + .join('?'); |
| 45 | + |
| 46 | + // Is this something other than a SQL expression? |
| 47 | + if (!tagged && !isSqlQuery(literal)) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + const incidents = []; |
| 52 | + |
| 53 | + // Loop through the TemplateElements |
| 54 | + templateLiteralNode.quasis.forEach((node, index) => { |
| 55 | + // Is there a `LIMIT offset,` clause? |
| 56 | + const hardcodedShortHandOffset = node.value.raw.match( |
| 57 | + /\b(?<prefix>limit(\s+(--.*\n)*)+)(?<body>(\d+|\?)\s*,)/i |
| 58 | + ); |
| 59 | + |
| 60 | + if (hardcodedShortHandOffset) { |
| 61 | + const matchStr = hardcodedShortHandOffset.groups.body; |
| 62 | + const matchIndex = |
| 63 | + hardcodedShortHandOffset.index + |
| 64 | + hardcodedShortHandOffset.groups.prefix.length; |
| 65 | + |
| 66 | + incidents.push({ |
| 67 | + messageId: 'nonStandardSQL', |
| 68 | + ...getLocation(node, matchIndex, matchStr), |
| 69 | + }); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + // Is there a `LIMIT` clause? |
| 74 | + const endsInLimitClause = node.value.raw.match( |
| 75 | + /limit(\s+(--.*\n)*)+$/i |
| 76 | + ); |
| 77 | + |
| 78 | + if (endsInLimitClause && !node.tail) { |
| 79 | + // Get the next node in sequence, does that start with a `,`? |
| 80 | + const nextNode = templateLiteralNode.quasis[index + 1]; |
| 81 | + |
| 82 | + const nextStartsWithComma = |
| 83 | + nextNode.value.raw.match(/^(\s*),/i); |
| 84 | + |
| 85 | + // console.log({ |
| 86 | + // nextNode, |
| 87 | + // endsInLimitClause, |
| 88 | + // nextStartsWithComma, |
| 89 | + // }); |
| 90 | + |
| 91 | + if (nextStartsWithComma) { |
| 92 | + const strPattern = ','; |
| 93 | + |
| 94 | + // Report the error |
| 95 | + const matchIndex = |
| 96 | + nextNode.value.raw.indexOf(strPattern); |
| 97 | + |
| 98 | + incidents.push({ |
| 99 | + messageId: 'nonStandardSQL', |
| 100 | + ...getLocation(nextNode, matchIndex, strPattern), |
| 101 | + }); |
| 102 | + } |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + // console.log(incidents); |
| 107 | + |
| 108 | + incidents.forEach(incident => context.report(incident)); |
| 109 | + } |
| 110 | + |
| 111 | + return { |
| 112 | + TemplateLiteral: validate, |
| 113 | + }; |
| 114 | + }, |
| 115 | +}; |
0 commit comments