eslint-plugin-vue-type-assertion-parens
Version:
ESLint plugin to enforce parentheses around TypeScript type assertions in Vue templates
88 lines (86 loc) • 2.99 kB
JavaScript
var __getOwnPropNames = Object.getOwnPropertyNames;
var __esm = (fn, res) => function __init() {
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
};
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
// src/rules/type-assertion-parens.ts
import { defineTemplateBodyVisitor } from "eslint-plugin-vue/lib/utils/index.js";
var rule, type_assertion_parens_default;
var init_type_assertion_parens = __esm({
"src/rules/type-assertion-parens.ts"() {
"use strict";
rule = {
meta: {
type: "layout",
docs: {
description: "Enforce parentheses around type assertions in Vue templates",
recommended: false,
url: "https://github.com/stepanroznik/eslint-plugin-vue-type-assertion-parens#readme"
},
fixable: "code",
schema: [],
messages: {
missingParens: "Type assertion in Vue template should be wrapped in parentheses."
}
},
create(context) {
const sourceCode = context.getSourceCode();
function isPartOfChainedAssertion(node) {
return node.parent && node.parent.type === "TSAsExpression" && node.parent.expression === node;
}
function isWrappedInParens(node) {
const startIndex = node.range[0] - 1;
const endIndex = node.range[1];
if (startIndex < 0 || endIndex >= sourceCode.text.length) {
return false;
}
const charBefore = sourceCode.text[startIndex];
const charAfter = sourceCode.text[endIndex];
return charBefore === "(" && charAfter === ")";
}
function checkAssertion(node) {
if (isPartOfChainedAssertion(node)) return;
if (isWrappedInParens(node)) return;
context.report({
node,
messageId: "missingParens",
fix(fixer) {
return fixer.replaceText(node, `(${sourceCode.getText(node)})`);
}
});
}
return defineTemplateBodyVisitor(context, {
// Handle TypeScript 'as' assertions
// Note: Angle bracket assertions (<Type>value) are not possible in Vue templates
// due to conflicts with HTML syntax
TSAsExpression: checkAssertion
});
}
};
type_assertion_parens_default = rule;
}
});
// src/index.ts
var require_index = __commonJS({
"src/index.ts"(exports, module) {
init_type_assertion_parens();
var plugin = {
rules: {
"type-assertion-parens": type_assertion_parens_default
},
configs: {
recommended: {
plugins: ["vue-type-assertion-parens"],
rules: {
"vue-type-assertion-parens/type-assertion-parens": "error"
}
}
}
};
module.exports = plugin;
}
});
export default require_index();
//# sourceMappingURL=index.mjs.map