eslint-svelte3-preprocess
Version:
svelte preprocess for eslint-plugin-svelte3
92 lines (91 loc) • 2.87 kB
JavaScript
import esTree from "@typescript-eslint/typescript-estree";
import svelteCompiler from "svelte/compiler";
export const preprocess = async (src, filename, preprocessors) => {
let markup;
let module;
let instance;
let style;
const result = await svelteCompiler.preprocess(src, [
{
markup: ({ content }) => {
markup = {
original: content,
};
return {
code: content,
};
},
script: ({ content, attributes }) => {
// Supported scenarios
// type="text/typescript"
// lang="typescript"
// lang="ts"
if (attributes.lang === "ts" ||
attributes.lang === "typescript" ||
attributes.type === "text/typescript") {
const ast = esTree.parse(content, { loc: true });
const obj = {
ast,
original: content,
ext: "ts",
};
if (attributes.context) {
module = obj;
}
else {
instance = obj;
}
}
return {
code: content,
};
},
style: ({ content }) => {
style = {
original: content,
};
return {
code: content,
};
},
},
...(Array.isArray(preprocessors) ? preprocessors : [preprocessors]),
{
markup: ({ content }) => {
if (markup) {
markup.result = content;
markup.diff = markup.original.length - content.length;
}
return {
code: content,
};
},
script: ({ content, attributes }) => {
const obj = attributes.context ? module : instance;
if (obj) {
obj.result = content;
obj.diff = obj.original.length - content.length;
}
return {
code: content,
};
},
style: ({ content }) => {
if (style) {
style.result = content;
style.diff = style.original.length - content.length;
}
return {
code: content,
};
},
},
], { filename: filename || "unknown" });
return {
...result,
instance,
markup,
module,
style,
};
};