|
| 1 | +import {lint} from '@putout/processor-css'; |
| 2 | + |
| 3 | +const createSpaceLine = (a) => Array(a).join(' '); |
| 4 | +const addSpaces = (spacesCount) => (a) => createSpaceLine(spacesCount) + a; |
| 5 | +const cutSpaces = (spacesCount) => (a) => a.slice(spacesCount); |
| 6 | + |
| 7 | +export const files = [ |
| 8 | + '*.html', |
| 9 | + '*.svelte', |
| 10 | +]; |
| 11 | + |
| 12 | +export const fix = async (rawSource) => { |
| 13 | + const svelte = await import('svelte/compiler'); |
| 14 | + |
| 15 | + const {code} = await svelte.preprocess(rawSource, { |
| 16 | + async style({content}) { |
| 17 | + const source = removePrefixSpaces(content); |
| 18 | + |
| 19 | + const [currentSource] = await lint(source, { |
| 20 | + fix: true, |
| 21 | + }); |
| 22 | + |
| 23 | + const code = addPrefixSpaces({ |
| 24 | + currentSource: currentSource.slice(0, -1), |
| 25 | + content, |
| 26 | + }); |
| 27 | + |
| 28 | + return { |
| 29 | + code, |
| 30 | + }; |
| 31 | + }, |
| 32 | + }); |
| 33 | + |
| 34 | + return code; |
| 35 | +}; |
| 36 | + |
| 37 | +export const find = async (rawSource) => { |
| 38 | + const svelte = await import('svelte/compiler'); |
| 39 | + const allPlaces = []; |
| 40 | + |
| 41 | + await svelte.preprocess(rawSource, { |
| 42 | + async style({content}) { |
| 43 | + const source = removePrefixSpaces(content); |
| 44 | + |
| 45 | + const [currentSource, places] = await lint(source, { |
| 46 | + fix: false, |
| 47 | + }); |
| 48 | + |
| 49 | + const code = addPrefixSpaces({ |
| 50 | + currentSource: currentSource.slice(0, -1), |
| 51 | + content, |
| 52 | + }); |
| 53 | + |
| 54 | + const index = rawSource.indexOf(content); |
| 55 | + |
| 56 | + const startLine = getStartLine({ |
| 57 | + rawSource, |
| 58 | + index, |
| 59 | + }); |
| 60 | + |
| 61 | + const line = rawSource.split('\n')[startLine]; |
| 62 | + const startColumn = getSpacesCount(line); |
| 63 | + |
| 64 | + for (const place of places) { |
| 65 | + place.position.line += startLine; |
| 66 | + place.position.column += startColumn; |
| 67 | + } |
| 68 | + |
| 69 | + allPlaces.push(...places); |
| 70 | + |
| 71 | + return { |
| 72 | + code, |
| 73 | + }; |
| 74 | + }, |
| 75 | + }); |
| 76 | + |
| 77 | + return allPlaces; |
| 78 | +}; |
| 79 | + |
| 80 | +export const branch = async (rawSource) => { |
| 81 | + const list = []; |
| 82 | + const svelte = await import('svelte/compiler'); |
| 83 | + |
| 84 | + await svelte.preprocess(rawSource, { |
| 85 | + async script({content}) { |
| 86 | + if (!content) |
| 87 | + return; |
| 88 | + |
| 89 | + const source = removePrefixSpaces(content); |
| 90 | + const index = rawSource.indexOf(content); |
| 91 | + |
| 92 | + const startLine = getStartLine({ |
| 93 | + rawSource, |
| 94 | + index, |
| 95 | + }); |
| 96 | + |
| 97 | + list.push({ |
| 98 | + startLine, |
| 99 | + source, |
| 100 | + extension: 'js', |
| 101 | + }); |
| 102 | + }, |
| 103 | + }); |
| 104 | + |
| 105 | + return list; |
| 106 | +}; |
| 107 | + |
| 108 | +export const merge = async (rawSource, list) => { |
| 109 | + const svelte = await import('svelte/compiler'); |
| 110 | + |
| 111 | + const {code} = await svelte.preprocess(rawSource, { |
| 112 | + script({content}) { |
| 113 | + if (!content) |
| 114 | + return; |
| 115 | + |
| 116 | + const currentSource = list |
| 117 | + .shift() |
| 118 | + .trim(); |
| 119 | + |
| 120 | + const code = addPrefixSpaces({ |
| 121 | + content, |
| 122 | + currentSource, |
| 123 | + }); |
| 124 | + |
| 125 | + return { |
| 126 | + code, |
| 127 | + }; |
| 128 | + }, |
| 129 | + }); |
| 130 | + |
| 131 | + return code; |
| 132 | +}; |
| 133 | + |
| 134 | +function addPrefixSpaces({content, currentSource}) { |
| 135 | + const spacesCount = getSpacesCount(content); |
| 136 | + |
| 137 | + const lastLine = content |
| 138 | + .split('\n') |
| 139 | + .pop(); |
| 140 | + |
| 141 | + const code = '\n' + currentSource |
| 142 | + .split('\n') |
| 143 | + .map(addSpaces(spacesCount)) |
| 144 | + .join('\n') + |
| 145 | + '\n' + lastLine; |
| 146 | + |
| 147 | + return code; |
| 148 | +} |
| 149 | + |
| 150 | +function removePrefixSpaces(text) { |
| 151 | + const lines = text |
| 152 | + .split('\n') |
| 153 | + .slice(1); |
| 154 | + |
| 155 | + const spacesCount = getSpacesCount(lines[0]); |
| 156 | + |
| 157 | + return lines |
| 158 | + .map(cutSpaces(spacesCount)) |
| 159 | + .join('\n'); |
| 160 | +} |
| 161 | + |
| 162 | +function getSpacesCount(text) { |
| 163 | + let i = 0; |
| 164 | + |
| 165 | + while (/\s/.test(text[i])) |
| 166 | + ++i; |
| 167 | + |
| 168 | + return i; |
| 169 | +} |
| 170 | + |
| 171 | +function getStartLine({index, rawSource}) { |
| 172 | + let i = 0; |
| 173 | + let lines = 0; |
| 174 | + |
| 175 | + while (i <= index) { |
| 176 | + if (rawSource[i] === '\n') |
| 177 | + ++lines; |
| 178 | + |
| 179 | + ++i; |
| 180 | + } |
| 181 | + |
| 182 | + return lines; |
| 183 | +} |
0 commit comments