-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
organize.js
38 lines (32 loc) · 1.07 KB
/
organize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const { sep, posix } = require('path');
const { applyTextChanges } = require('./apply-text-changes');
const { getLanguageService } = require('./get-language-service');
/**
* Organize the given code.
*
* @param {string} code
* @param {import('prettier').ParserOptions} options
*/
module.exports.organize = (
code,
{ filepath = 'file.ts', organizeImportsSkipDestructiveCodeActions, parentParser, parser },
) => {
if (parentParser === 'vue' || parentParser === 'svelte') {
// we do the preprocessing from the parent parser instead, so we skip the child parsers
return code;
}
if (sep !== posix.sep) {
filepath = filepath.split(sep).join(posix.sep);
}
/** @todo remove this */
if (parser === 'svelte') {
filepath = filepath + '.vue';
}
const languageService = getLanguageService(parser, filepath, code);
const fileChanges = languageService.organizeImports(
{ type: 'file', fileName: filepath, skipDestructiveCodeActions: organizeImportsSkipDestructiveCodeActions },
{},
{},
)[0];
return fileChanges ? applyTextChanges(code, fileChanges.textChanges) : code;
};