|
1 | 1 | import { parseRuleOptions } from '../utils.js' |
2 | | -import { parse as parseSFC } from '@vue/compiler-sfc' |
3 | | -import { minimatch } from 'minimatch' |
| 2 | +import { shouldIgnoreFile, parseOrderOption, processVueFile } from './sfc-sections-order-utils.js' |
4 | 3 |
|
5 | 4 | const defaultOptions = { |
6 | 5 | order: ['script', 'template', 'style'], |
7 | 6 | ignore: [], |
8 | 7 | } |
9 | 8 |
|
10 | | -function shouldIgnoreFile(filename, ignorePatterns) { |
11 | | - if (!ignorePatterns || !Array.isArray(ignorePatterns)) { |
12 | | - return false |
13 | | - } |
14 | | - |
15 | | - return ignorePatterns.some((pattern) => { |
16 | | - try { |
17 | | - return minimatch(filename, pattern) |
18 | | - } catch { |
19 | | - return false |
20 | | - } |
21 | | - }) |
22 | | -} |
23 | | - |
24 | | -function parseOrderOption(options, defaultOrder) { |
25 | | - if (Array.isArray(options.order) && options.order.length === 3 && options.order.every((section) => defaultOrder.includes(section))) { |
26 | | - return options.order |
27 | | - } |
28 | | - return defaultOrder |
29 | | -} |
30 | | - |
31 | | -function processVueFile(context, node, filename, order) { |
32 | | - const source = context.getSourceCode().text |
33 | | - const tags = [] |
34 | | - |
35 | | - try { |
36 | | - const { descriptor } = parseSFC(source, { filename }) |
37 | | - |
38 | | - // Process template |
39 | | - if (descriptor.template && descriptor.template.loc) { |
40 | | - tags.push({ name: 'template', index: descriptor.template.loc.start.offset }) |
41 | | - } |
42 | | - |
43 | | - // Process script blocks with ordering validation |
44 | | - if (!processScriptBlocks(descriptor, context, node, tags)) { |
45 | | - return |
46 | | - } |
47 | | - |
48 | | - // Process style blocks with ordering validation |
49 | | - if (!processStyleBlocks(descriptor, context, node, tags)) { |
50 | | - return |
51 | | - } |
52 | | - |
53 | | - // Validate overall section order |
54 | | - validateSectionOrder(tags, order, context, node) |
55 | | - } catch { |
56 | | - context.report({ node, messageId: 'parsingError' }) |
57 | | - return |
58 | | - } |
59 | | -} |
60 | | - |
61 | | -function collectScriptBlocks(descriptor) { |
62 | | - const scriptBlocks = [] |
63 | | - if (descriptor.scriptSetup && descriptor.scriptSetup.loc) { |
64 | | - scriptBlocks.push({ type: 'setup', offset: descriptor.scriptSetup.loc.start.offset }) |
65 | | - } |
66 | | - if (descriptor.script && descriptor.script.loc) { |
67 | | - scriptBlocks.push({ type: 'regular', offset: descriptor.script.loc.start.offset }) |
68 | | - } |
69 | | - return scriptBlocks |
70 | | -} |
71 | | - |
72 | | -function validateScriptOrder(scriptBlocks, context, node) { |
73 | | - if (scriptBlocks.length === 2) { |
74 | | - const setupBlock = scriptBlocks.find((b) => b.type === 'setup') |
75 | | - const regularBlock = scriptBlocks.find((b) => b.type === 'regular') |
76 | | - if (setupBlock.offset > regularBlock.offset) { |
77 | | - context.report({ node, messageId: 'scriptSetupBeforeScript' }) |
78 | | - return false |
79 | | - } |
80 | | - } |
81 | | - return true |
82 | | -} |
83 | | - |
84 | | -function addScriptBlocksToTags(scriptBlocks, tags) { |
85 | | - scriptBlocks.forEach((block) => { |
86 | | - tags.push({ name: 'script', index: block.offset }) |
87 | | - }) |
88 | | -} |
89 | | - |
90 | | -function processScriptBlocks(descriptor, context, node, tags) { |
91 | | - const scriptBlocks = collectScriptBlocks(descriptor) |
92 | | - |
93 | | - if (!validateScriptOrder(scriptBlocks, context, node)) { |
94 | | - return false |
95 | | - } |
96 | | - |
97 | | - addScriptBlocksToTags(scriptBlocks, tags) |
98 | | - return true |
99 | | -} |
100 | | - |
101 | | -function collectStyleBlocks(descriptor) { |
102 | | - const styleBlocks = [] |
103 | | - descriptor.styles.forEach((style) => { |
104 | | - if (style.loc) { |
105 | | - const isScoped = style.scoped === true |
106 | | - styleBlocks.push({ |
107 | | - type: isScoped ? 'scoped' : 'global', |
108 | | - offset: style.loc.start.offset, |
109 | | - }) |
110 | | - } |
111 | | - }) |
112 | | - return styleBlocks |
113 | | -} |
114 | | - |
115 | | -function validateStyleOrder(styleBlocks, context, node) { |
116 | | - const globalStyles = styleBlocks.filter((s) => s.type === 'global') |
117 | | - const scopedStyles = styleBlocks.filter((s) => s.type === 'scoped') |
118 | | - |
119 | | - if (globalStyles.length > 0 && scopedStyles.length > 0) { |
120 | | - const lastGlobal = Math.max(...globalStyles.map((s) => s.offset)) |
121 | | - const firstScoped = Math.min(...scopedStyles.map((s) => s.offset)) |
122 | | - if (firstScoped < lastGlobal) { |
123 | | - context.report({ node, messageId: 'scopedStyleAfterGlobal' }) |
124 | | - return false |
125 | | - } |
126 | | - } |
127 | | - return true |
128 | | -} |
129 | | - |
130 | | -function addStyleBlocksToTags(styleBlocks, tags) { |
131 | | - styleBlocks.forEach((style) => { |
132 | | - tags.push({ name: 'style', index: style.offset }) |
133 | | - }) |
134 | | -} |
135 | | - |
136 | | -function processStyleBlocks(descriptor, context, node, tags) { |
137 | | - const styleBlocks = collectStyleBlocks(descriptor) |
138 | | - |
139 | | - if (!validateStyleOrder(styleBlocks, context, node)) { |
140 | | - return false |
141 | | - } |
142 | | - |
143 | | - addStyleBlocksToTags(styleBlocks, tags) |
144 | | - return true |
145 | | -} |
146 | | - |
147 | | -function checkRequiredSections(tags, context, node) { |
148 | | - const names = tags.map((t) => t.name) |
149 | | - const hasTemplate = names.includes('template') |
150 | | - const hasScript = names.includes('script') |
151 | | - |
152 | | - if (!hasTemplate && !hasScript) { |
153 | | - context.report({ node, messageId: 'missingScriptOrTemplate' }) |
154 | | - return false |
155 | | - } |
156 | | - return true |
157 | | -} |
158 | | - |
159 | | -function groupSectionsByType(tags) { |
160 | | - // Sort tags by their position in the file |
161 | | - tags.sort((a, b) => a.index - b.index) |
162 | | - |
163 | | - // Group consecutive blocks of the same type |
164 | | - const sections = [] |
165 | | - let currentSection = null |
166 | | - |
167 | | - for (const tag of tags) { |
168 | | - if (currentSection && currentSection.name === tag.name) { |
169 | | - continue // Skip, we already have this section type |
170 | | - } |
171 | | - currentSection = { name: tag.name } |
172 | | - sections.push(currentSection) |
173 | | - } |
174 | | - return sections |
175 | | -} |
176 | | - |
177 | | -function checkSectionOrder(sections, order, context, node) { |
178 | | - const sectionOrder = sections.map((s) => order.indexOf(s.name)).filter((i) => i >= 0) |
179 | | - |
180 | | - for (let i = 0; i + 1 < sectionOrder.length; i++) { |
181 | | - if (sectionOrder[i] > sectionOrder[i + 1]) { |
182 | | - context.report({ node, messageId: 'wrongOrder', data: { order: order.join(' -> ') } }) |
183 | | - return |
184 | | - } |
185 | | - } |
186 | | -} |
187 | | - |
188 | | -function validateSectionOrder(tags, order, context, node) { |
189 | | - if (!checkRequiredSections(tags, context, node)) { |
190 | | - return |
191 | | - } |
192 | | - |
193 | | - const sections = groupSectionsByType(tags) |
194 | | - checkSectionOrder(sections, order, context, node) |
195 | | -} |
196 | | - |
197 | 9 | export default { |
198 | 10 | meta: { |
199 | 11 | type: 'suggestion', |
|
0 commit comments