eslint-plugin-vue-type-assertion-parens
Version:
ESLint plugin to enforce parentheses around TypeScript type assertions in Vue templates
70 lines (68 loc) • 2.14 kB
JavaScript
;
// src/rules/type-assertion-parens.ts
var import_utils = require("eslint-plugin-vue/lib/utils/index.js");
var 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 (0, import_utils.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
});
}
};
var type_assertion_parens_default = rule;
// src/index.ts
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;
//# sourceMappingURL=index.js.map