Skip to content

Commit c0011ad

Browse files
committed
remove ts based jsx formatting
1 parent e1e71ac commit c0011ad

File tree

2 files changed

+4
-74
lines changed

2 files changed

+4
-74
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
"README.md"
1616
],
1717
"peerDependencies": {
18-
"rollup": "^1.20.0 || ^2.0.0",
19-
"typescript": "^5.5.4"
18+
"rollup": "^1.20.0 || ^2.0.0"
2019
},
2120
"devDependencies": {
2221
"rollup": "^2.23.0"

src/index.js

Lines changed: 3 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,9 @@ export default function afterEffectsJsx(options = { wrap: false }) {
111111
.prepend('function get() {\n')
112112
.append(`\nreturn {${exports.join(',\n\t')}}\n}`);
113113

114-
const asString = format(magicString.toString()).replace(
115-
`function get() {`,
116-
`get() {`
117-
);
114+
const asString = magicString
115+
.toString()
116+
.replace(`function get() {`, `get() {`);
118117

119118
magicString = new MagicString(asString);
120119
} else {
@@ -192,71 +191,3 @@ export default function afterEffectsJsx(options = { wrap: false }) {
192191
},
193192
};
194193
}
195-
196-
// extra: use ts to format... though currently not a dep of `rollup-plugin-ae-jsx`
197-
198-
// https://github.com/vvakame/typescript-formatter/tree/master/lib
199-
import * as ts from 'typescript';
200-
201-
const defaultOptions = {
202-
baseIndentSize: 0,
203-
indentSize: 2,
204-
tabSize: 2,
205-
indentStyle: ts.IndentStyle.Smart,
206-
newLineCharacter: '\r\n',
207-
convertTabsToSpaces: true,
208-
insertSpaceAfterCommaDelimiter: true,
209-
insertSpaceAfterSemicolonInForStatements: true,
210-
insertSpaceBeforeAndAfterBinaryOperators: true,
211-
insertSpaceAfterConstructor: false,
212-
insertSpaceAfterKeywordsInControlFlowStatements: true,
213-
insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
214-
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
215-
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
216-
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true,
217-
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
218-
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false,
219-
insertSpaceAfterTypeAssertion: false,
220-
insertSpaceBeforeFunctionParenthesis: false,
221-
placeOpenBraceOnNewLineForFunctions: false,
222-
placeOpenBraceOnNewLineForControlBlocks: false,
223-
insertSpaceBeforeTypeAnnotation: false,
224-
};
225-
226-
class LanguageServiceHost {
227-
constructor() {
228-
this.files = {};
229-
// for ts.LanguageServiceHost
230-
this.getCompilationSettings = () => ts.getDefaultCompilerOptions();
231-
this.getScriptFileNames = () => Object.keys(this.files);
232-
this.getScriptVersion = (_fileName) => '0';
233-
this.getScriptSnapshot = (fileName) => this.files[fileName];
234-
this.getCurrentDirectory = () => process.cwd();
235-
this.getDefaultLibFileName = (options) => ts.getDefaultLibFilePath(options);
236-
}
237-
addFile(fileName, text) {
238-
this.files[fileName] = ts.ScriptSnapshot.fromString(text);
239-
}
240-
}
241-
242-
function format(text, fileName = 'temp.ts', options = defaultOptions) {
243-
const host = new LanguageServiceHost();
244-
host.addFile(fileName, text);
245-
246-
const languageService = ts.createLanguageService(host);
247-
const edits = languageService.getFormattingEditsForDocument(
248-
fileName,
249-
options
250-
);
251-
252-
edits
253-
.sort((a, b) => a.span.start - b.span.start)
254-
.reverse()
255-
.forEach((edit) => {
256-
const head = text.slice(0, edit.span.start);
257-
const tail = text.slice(edit.span.start + edit.span.length);
258-
text = `${head}${edit.newText}${tail}`;
259-
});
260-
261-
return text;
262-
}

0 commit comments

Comments
 (0)