|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2025-2026 Max Trunnikov |
| 3 | + * SPDX-License-Identifier: MIT |
| 4 | + */ |
| 5 | + |
| 6 | +const {nodes} = require('./xpath') |
| 7 | +const {tokenized, TOKENS} = require('./tokens') |
| 8 | +const {yaml} = require('./helpers') |
| 9 | +const path = require('path') |
| 10 | +const {logger} = require('./logger') |
| 11 | + |
| 12 | +/** |
| 13 | + * Name of the check this linter owns. |
| 14 | + * @type {string} |
| 15 | + */ |
| 16 | +const CHECK = 'use-node-set-extension' |
| 17 | + |
| 18 | +/** |
| 19 | + * Defect metadata of the check. |
| 20 | + * @type {{severity: string, message: string}} |
| 21 | + */ |
| 22 | +const META = yaml.parsedFromFile( |
| 23 | + path.join(__dirname, 'resources', 'checks', 'format', `${CHECK}.yaml`), |
| 24 | +) |
| 25 | + |
| 26 | +/** |
| 27 | + * Names of the checks this linter owns. |
| 28 | + * @type {Array.<string>} |
| 29 | + */ |
| 30 | +const names = [CHECK] |
| 31 | + |
| 32 | +/** |
| 33 | + * Stylesheet versions where the node-set() extension is redundant. |
| 34 | + * @type {Array.<string>} |
| 35 | + */ |
| 36 | +const MODERN = ['2.0', '3.0'] |
| 37 | + |
| 38 | +/** |
| 39 | + * Pattern of a `prefix:node-set(` call opener. |
| 40 | + * @type {RegExp} |
| 41 | + */ |
| 42 | +const CALL = /[\w.-]+:node-set\s*\(/g |
| 43 | + |
| 44 | +/** |
| 45 | + * A select value with its string and comment spans blanked to spaces, so a |
| 46 | + * `node-set(` call can be found and its parentheses balanced without tripping |
| 47 | + * over text inside a literal. Blanking keeps every offset intact. |
| 48 | + * @param {string} select - The `select` attribute value |
| 49 | + * @return {string} - The value with literals blanked |
| 50 | + */ |
| 51 | +const masked = function(select) { |
| 52 | + const chars = Array.from(select) |
| 53 | + for (const token of tokenized(select)) { |
| 54 | + if (token.type === TOKENS.STRING || token.type === TOKENS.COMMENT) { |
| 55 | + for (let at = token.start; at < token.start + token.value.length; at++) { |
| 56 | + chars[at] = ' ' |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + return chars.join('') |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Offset of the `)` that closes the `(` at `open` in a literal-free expression, |
| 65 | + * or -1 when it is unbalanced. |
| 66 | + * @param {string} expression - Expression with literals already blanked |
| 67 | + * @param {number} open - Offset of the opening `(` |
| 68 | + * @return {number} - Offset of the matching `)`, or -1 |
| 69 | + */ |
| 70 | +const closes = function(expression, open) { |
| 71 | + let depth = 0 |
| 72 | + for (let at = open; at < expression.length; at++) { |
| 73 | + if (expression[at] === '(') { |
| 74 | + depth++ |
| 75 | + } else if (expression[at] === ')') { |
| 76 | + depth-- |
| 77 | + if (depth === 0) { |
| 78 | + return at |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + return -1 |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * The node-set() wrappers in a select value: each carries the offset it starts |
| 87 | + * at, its verbatim text, and the inner argument that replaces it. A call whose |
| 88 | + * parentheses do not balance is skipped. |
| 89 | + * @param {string} select - The `select` attribute value |
| 90 | + * @return {Array.<{offset: number, value: string, replacement: string}>} - |
| 91 | + * The node-set() calls found |
| 92 | + */ |
| 93 | +const wrappers = function(select) { |
| 94 | + const found = [] |
| 95 | + const blanked = masked(select) |
| 96 | + for (const match of blanked.matchAll(CALL)) { |
| 97 | + const open = match.index + match[0].length - 1 |
| 98 | + const close = closes(blanked, open) |
| 99 | + if (close >= 0) { |
| 100 | + found.push({ |
| 101 | + offset: match.index, |
| 102 | + value: select.slice(match.index, close + 1), |
| 103 | + replacement: select.slice(open + 1, close), |
| 104 | + }) |
| 105 | + } |
| 106 | + } |
| 107 | + return found |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Lint the corpus for the `node-set()` extension used in XSLT 2.0 or 3.0, where |
| 112 | + * a variable is already a node sequence, reporting one defect per call with the |
| 113 | + * fix that unwraps it. |
| 114 | + * @param {Array.<{file: string, xsl: Document}>} corpus - Parsed stylesheets |
| 115 | + * @param {Array.<string>} suppressions - Array of suppressed checks |
| 116 | + * @return {{name: string, severity: string, message: string, file: string, |
| 117 | + * line: number, pos: number, fix: object}[]} - Defects found |
| 118 | + */ |
| 119 | +const lintByNodeSet = function(corpus, suppressions = []) { |
| 120 | + logger.debug(`Node-set linting started`) |
| 121 | + const defects = [] |
| 122 | + if (!suppressions.some((sup) => CHECK.includes(sup))) { |
| 123 | + for (const {file, xsl} of corpus) { |
| 124 | + if (MODERN.includes(xsl.documentElement.getAttribute('version'))) { |
| 125 | + for (const attribute of nodes(xsl, '//@select')) { |
| 126 | + for (const {offset, value, replacement} of wrappers( |
| 127 | + attribute.nodeValue, |
| 128 | + )) { |
| 129 | + const pos = attribute.columnNumber + 1 + offset |
| 130 | + defects.push({ |
| 131 | + name: CHECK, |
| 132 | + severity: META.severity, |
| 133 | + message: META.message, |
| 134 | + file: file, |
| 135 | + line: attribute.lineNumber, |
| 136 | + pos: pos, |
| 137 | + fix: { |
| 138 | + line: attribute.lineNumber, |
| 139 | + col: pos, |
| 140 | + value: value, |
| 141 | + replacement: replacement, |
| 142 | + }, |
| 143 | + }) |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + logger.debug(`Found ${defects.length} node-set extension defects`) |
| 150 | + return defects |
| 151 | +} |
| 152 | + |
| 153 | +module.exports = { |
| 154 | + lintByNodeSet, |
| 155 | + names, |
| 156 | +} |
0 commit comments