Skip to content

Commit 1afb765

Browse files
committed
feature: @putout/processor-html: renamed to svelte
1 parent abecf67 commit 1afb765

20 files changed

+502
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ Here is a list of built-in processors:
466466
| [`@putout/processor-yaml`](/packages/processor-yaml#readme) | [![npm](https://img.shields.io/npm/v/@putout/processor-yaml.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/processor-yaml) |
467467
| [`@putout/processor-css`](/packages/processor-css#readme) | [![npm](https://img.shields.io/npm/v/@putout/processor-css.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/processor-css) |
468468
| [`@putout/processor-filesystem`](/packages/processor-filesystem#readme) | [![npm](https://img.shields.io/npm/v/@putout/processor-filesystem.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/processor-filesystem) |
469+
| [`@putout/processor-html`](/packages/processor-html#readme) | [![npm](https://img.shields.io/npm/v/@putout/processor-html.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/processor-html) |
469470

470471
You can disable any of them with:
471472

@@ -482,7 +483,7 @@ Not bundled processors:
482483
| Package | Version |
483484
|--------|-------|
484485
| [`@putout/processor-typescript`](/packages/processor-typescript#readme) | [![npm](https://img.shields.io/npm/v/@putout/processor-typescript.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/processor-typescript) |
485-
| [`@putout/processor-html`](/packages/processor-html#readme) | [![npm](https://img.shields.io/npm/v/@putout/processor-html.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/processor-html) |
486+
| [`@putout/processor-svelte`](/packages/processor-svelte#readme) | [![npm](https://img.shields.io/npm/v/@putout/processor-svelte.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/processor-svelte) |
486487
| [`@putout/processor-wasm`](/packages/processor-wasm#readme) | [![npm](https://img.shields.io/npm/v/@putout/processor-wasm.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/processor-wasm) |
487488

488489
External processors:
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
node_modules
2+
*.swp
3+
yarn-error.log
4+
5+
coverage
6+
.nyc_output
7+
.idea
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {run} from 'madrun';
2+
3+
export default {
4+
'test': () => `tape 'lib/**/*.spec.js' 'test/*.js'`,
5+
'watch:test': async () => `nodemon -w lib -w test -x "${await run('test')}"`,
6+
'lint': () => `putout .`,
7+
'fresh:lint': () => run('lint', '--fresh'),
8+
'lint:fresh': () => run('lint', '--fresh'),
9+
'fix:lint': () => run('lint', '--fix'),
10+
'coverage': async () => `c8 ${await run('test')}`,
11+
'report': () => 'c8 report --reporter=lcov',
12+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.*
2+
*.spec.js
3+
test
4+
yarn-error.log
5+
fixture
6+
7+
coverage
8+
*.config.*
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"check-coverage": true,
3+
"all": true,
4+
"exclude": [
5+
"**/*.spec.*",
6+
"**/fixture",
7+
"test",
8+
".*.*",
9+
"**/*.config.*"
10+
],
11+
"branches": 100,
12+
"lines": 100,
13+
"functions": 100,
14+
"statements": 100
15+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"eslint/apply-ignores": "off"
4+
}
5+
}

packages/processor-svelte/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) coderaiser
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# @putout/processor-svelte [![NPM version][NPMIMGURL]][NPMURL]
2+
3+
[NPMIMGURL]: https://img.shields.io/npm/v/@putout/processor-html.svg?style=flat&longCache=true
4+
[NPMURL]: https://npmjs.org/package/@putout/processor-html "npm"
5+
6+
🐊[**Putout**](https://github.com/coderaiser/putout) processor adds ability to get `js` and `css` code from `html` files.
7+
8+
## Install
9+
10+
```
11+
npm i @putout/processor-html -D
12+
```
13+
14+
## Usage
15+
16+
```json
17+
{
18+
"processors": ["html"]
19+
}
20+
```
21+
22+
## License
23+
24+
MIT
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import {safeAlign} from 'eslint-plugin-putout';
2+
3+
export default safeAlign;
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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

Comments
 (0)