eslint-plugin-svelte
Version:
ESLint plugin for Svelte using AST
52 lines (51 loc) • 1.49 kB
JavaScript
import { loadModule } from '../../../utils/load-module.js';
import { getSourceCode } from '../../../utils/compat.js';
/**
* Transpile with typescript
*/
export function transform(node, text, context) {
const ts = loadTs(context);
if (!ts) {
return null;
}
let inputRange;
if (node.endTag) {
inputRange = [node.startTag.range[1], node.endTag.range[0]];
}
else {
inputRange = [node.startTag.range[1], node.range[1]];
}
const code = text.slice(...inputRange);
try {
const output = ts.transpileModule(code, {
reportDiagnostics: false,
compilerOptions: {
target: getSourceCode(context).parserServices.program?.getCompilerOptions()?.target ||
ts.ScriptTarget.ESNext,
module: ts.ModuleKind.ESNext,
importsNotUsedAsValues: ts.ImportsNotUsedAsValues.Preserve,
preserveValueImports: true,
verbatimModuleSyntax: true,
sourceMap: true
}
});
return {
inputRange,
output: output.outputText,
mappings: JSON.parse(output.sourceMapText).mappings
};
}
catch {
return null;
}
}
/** Check if project has TypeScript. */
export function hasTypeScript(context) {
return Boolean(loadTs(context));
}
/**
* Load typescript
*/
function loadTs(context) {
return loadModule(context, 'typescript');
}