Отключить преобразователи машинописи по умолчанию - PullRequest
0 голосов
/ 04 апреля 2019

Я хочу написать собственный транспортер, используя api преобразователя машинописного текста, который выводит машинописный текст вместо javascript. Для этого мне нужно отключить преобразователи по умолчанию (машинопись → ecma2017 → ecma2016 → ...).

Возможно ли это? Я бы предпочел использовать tsc напрямую, но если мне придется использовать API компилятора вручную, это тоже хорошо.

1 Ответ

1 голос
/ 04 апреля 2019

Нет ts.ScriptTarget.TypeScript, поэтому вам нужно использовать API компилятора.

Вот основная идея (не проверено, но должно помочь вам начать с этого):

import * as ts from "typescript";

// setup
const printer = ts.createPrinter();
const sourceFiles: ts.SourceFile[] = ...;
const transformerFactory: ts.TransformerFactory<ts.SourceFile> = ...;

// transform the source files
const transformationResult = ts.transform(sourceFiles, [transformerFactory]);

// log the diagnostics if they exist
if (transformationResult.diagnostics) {
    // output diagnostics (ts.formatDiagnosticsWithColorAndContext is nice to use)
}

// print the transformed ASTs and write the result out to files
// note: replace fs.writeFile with something that actually works
const fileWrites = transformationResult.transformed
    .map(file => fs.writeFile(file.fileName, printer.printFile(file));
Promise.all(fileWrites)
    .then(() => console.log("finished"))
    .catch(err => console.error(err));
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...