Skip to content

Commit 5fe5c0a

Browse files
committed
wheeeee
1 parent 9a63840 commit 5fe5c0a

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

src/preprocessors/ember-preprocessor.ts

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,45 @@ import { PrettierOptions } from '../types';
44
import { preprocessor } from './preprocessor.js';
55

66
const sortImports = (code: string, options: PrettierOptions) => {
7-
const importsExports = parseImportsExports(code, { ignoreDynamicImports: true, ignoreRegexpLiterals: true, ignoreRequires: true, ignoreCommonJsExports: true });
8-
9-
// console.log(importsExports);
10-
11-
let justImports = '';
12-
13-
for (let [path, info] of Object.entries({...importsExports.namedImports})) {
14-
console.log(path, info);
15-
}
16-
17-
if (importsExports.defaultExport) {
18-
let pos = importsExports.defaultExport;
19-
let text = code.slice(pos.start, pos.end);
20-
justImports += text;
21-
}
22-
23-
for (let [path, info] of Object.entries(importsExports.namedImports)) {
24-
console.log(path, info);
25-
}
26-
27-
return preprocessor(justImports, options);
7+
const importsExports = parseImportsExports(code, {
8+
ignoreDynamicImports: true,
9+
ignoreRegexpLiterals: true,
10+
ignoreRequires: true,
11+
ignoreCommonJsExports: true,
12+
});
13+
14+
let justImports = '';
15+
16+
for (let [, info] of Object.entries({
17+
...importsExports.namedImports,
18+
})) {
19+
let pos = info[0];
20+
justImports += code.slice(pos.start, pos.end + 1);
21+
22+
let spaces = '';
23+
for (let i = 0; i < pos.end - pos.start; i++) {
24+
spaces += ' ';
25+
}
26+
27+
code = replaceAt(code, pos.start, spaces);
28+
}
29+
30+
let output = preprocessor(justImports, options);
31+
let result = output + code;
32+
33+
console.log('---------------');
34+
console.log(result);
35+
console.log('---------------');
36+
return result;
2837
};
2938

3039
export function emberPreprocessor(code: string, options: PrettierOptions) {
3140
return sortImports(code, options);
3241
}
42+
function replaceAt(str: string, index: number, replacement: string) {
43+
return (
44+
str.substring(0, index) +
45+
replacement +
46+
str.substring(index + replacement.length)
47+
);
48+
}

0 commit comments

Comments
 (0)