|
| 1 | +const _ = require('lodash'); |
| 2 | +const path = require('path'); |
| 3 | +const spec = require('../specs'); |
| 4 | +const {versions, normalizePath} = require('../utils'); |
| 5 | + |
| 6 | +const ruleCode = 'GS130-NO-RECURSIVE-LAYOUT'; |
| 7 | +const layoutPattern = /{{!<\s+([A-Za-z0-9._/-]+)\s*}}/g; |
| 8 | + |
| 9 | +function stripLeadingSlash(filePath) { |
| 10 | + return filePath.replace(/^\/+/, ''); |
| 11 | +} |
| 12 | + |
| 13 | +function ensureExtension(layoutPath) { |
| 14 | + if (path.posix.extname(layoutPath)) { |
| 15 | + return layoutPath; |
| 16 | + } |
| 17 | + |
| 18 | + return `${layoutPath}.hbs`; |
| 19 | +} |
| 20 | + |
| 21 | +function resolveLayoutPath(sourceFile, layoutName) { |
| 22 | + const source = normalizePath(sourceFile); |
| 23 | + const layout = ensureExtension(normalizePath(layoutName)); |
| 24 | + |
| 25 | + if (layout.startsWith('./') || layout.startsWith('../')) { |
| 26 | + return stripLeadingSlash(path.posix.normalize(path.posix.join(path.posix.dirname(source), layout))); |
| 27 | + } |
| 28 | + |
| 29 | + return stripLeadingSlash(path.posix.normalize(layout)); |
| 30 | +} |
| 31 | + |
| 32 | +function getLocation(source, index) { |
| 33 | + const lines = source.slice(0, index).split('\n'); |
| 34 | + |
| 35 | + return { |
| 36 | + line: lines.length, |
| 37 | + column: lines[lines.length - 1].length |
| 38 | + }; |
| 39 | +} |
| 40 | + |
| 41 | +function getLayoutReferences(themeFile) { |
| 42 | + const source = normalizePath(themeFile.normalizedFile || themeFile.file); |
| 43 | + const content = themeFile.content || ''; |
| 44 | + const references = []; |
| 45 | + let match; |
| 46 | + |
| 47 | + layoutPattern.lastIndex = 0; |
| 48 | + |
| 49 | + while ((match = layoutPattern.exec(content)) !== null) { |
| 50 | + references.push({ |
| 51 | + source, |
| 52 | + target: resolveLayoutPath(source, match[1]), |
| 53 | + location: getLocation(content, match.index) |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + return references; |
| 58 | +} |
| 59 | + |
| 60 | +function buildInheritanceGraph(theme) { |
| 61 | + const hbsFiles = theme.files.filter(file => file.ext === '.hbs'); |
| 62 | + const existingFiles = new Set(hbsFiles.map(file => normalizePath(file.normalizedFile || file.file))); |
| 63 | + const graph = new Map(); |
| 64 | + |
| 65 | + hbsFiles.forEach((themeFile) => { |
| 66 | + getLayoutReferences(themeFile).forEach((reference) => { |
| 67 | + if (!existingFiles.has(reference.target)) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + if (!graph.has(reference.source)) { |
| 72 | + graph.set(reference.source, []); |
| 73 | + } |
| 74 | + |
| 75 | + graph.get(reference.source).push(reference); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + return graph; |
| 80 | +} |
| 81 | + |
| 82 | +function getCyclePath(stack, target) { |
| 83 | + const targetIndex = stack.indexOf(target); |
| 84 | + return stack.slice(targetIndex).concat(target).join(' -> '); |
| 85 | +} |
| 86 | + |
| 87 | +function getRecursiveLayoutFailures(graph) { |
| 88 | + const visited = new Set(); |
| 89 | + const visiting = new Set(); |
| 90 | + const stack = []; |
| 91 | + const reportedCycles = new Set(); |
| 92 | + const failures = []; |
| 93 | + |
| 94 | + function visit(file) { |
| 95 | + visiting.add(file); |
| 96 | + stack.push(file); |
| 97 | + |
| 98 | + const references = graph.get(file) || []; |
| 99 | + |
| 100 | + references.forEach((reference) => { |
| 101 | + if (visiting.has(reference.target)) { |
| 102 | + const cyclePath = getCyclePath(stack, reference.target); |
| 103 | + |
| 104 | + if (!reportedCycles.has(cyclePath)) { |
| 105 | + reportedCycles.add(cyclePath); |
| 106 | + |
| 107 | + failures.push({ |
| 108 | + ref: reference.source, |
| 109 | + line: reference.location.line, |
| 110 | + column: reference.location.column, |
| 111 | + message: `Recursive layout inheritance detected: ${cyclePath}` |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + if (!visited.has(reference.target)) { |
| 119 | + visit(reference.target); |
| 120 | + } |
| 121 | + }); |
| 122 | + |
| 123 | + stack.pop(); |
| 124 | + visiting.delete(file); |
| 125 | + visited.add(file); |
| 126 | + } |
| 127 | + |
| 128 | + graph.forEach((references, file) => { // eslint-disable-line no-unused-vars |
| 129 | + if (!visited.has(file)) { |
| 130 | + visit(file); |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + return failures; |
| 135 | +} |
| 136 | + |
| 137 | +const checkTemplateInheritance = function checkTemplateInheritance(theme, options) { |
| 138 | + const checkVersion = _.get(options, 'checkVersion', versions.default); |
| 139 | + const ruleSet = spec.get([checkVersion]); |
| 140 | + |
| 141 | + if (!ruleSet.rules[ruleCode]) { |
| 142 | + return theme; |
| 143 | + } |
| 144 | + |
| 145 | + const failures = getRecursiveLayoutFailures(buildInheritanceGraph(theme)); |
| 146 | + |
| 147 | + if (failures.length > 0) { |
| 148 | + theme.results.fail[ruleCode] = {failures}; |
| 149 | + } else { |
| 150 | + theme.results.pass.push(ruleCode); |
| 151 | + } |
| 152 | + |
| 153 | + return theme; |
| 154 | +}; |
| 155 | + |
| 156 | +module.exports = checkTemplateInheritance; |
0 commit comments