UNPKG

eslint-plugin-vue-type-assertion-parens

Version:

ESLint plugin to enforce parentheses around TypeScript type assertions in Vue templates

1 lines 3.98 kB
{"version":3,"sources":["../src/rules/type-assertion-parens.ts","../src/index.ts"],"sourcesContent":["\nimport { Rule } from 'eslint';\nimport { defineTemplateBodyVisitor } from 'eslint-plugin-vue/lib/utils/index.js';\n\nconst rule: Rule.RuleModule = {\n meta: {\n type: \"layout\",\n docs: {\n description: \"Enforce parentheses around type assertions in Vue templates\",\n recommended: false,\n url: \"https://github.com/stepanroznik/eslint-plugin-vue-type-assertion-parens#readme\",\n },\n fixable: \"code\",\n schema: [],\n messages: {\n missingParens: \"Type assertion in Vue template should be wrapped in parentheses.\",\n },\n },\n\n create(context: Rule.RuleContext) {\n const sourceCode = context.getSourceCode();\n\n function isPartOfChainedAssertion(node: any) {\n // Check if this node is the left side of another type assertion\n // (meaning it's part of a chain like: expr as Type1 as Type2)\n return node.parent && node.parent.type === 'TSAsExpression' && node.parent.expression === node;\n }\n\n function isWrappedInParens(node: any) {\n // Get the source text around the node\n const startIndex = node.range[0] - 1;\n const endIndex = node.range[1];\n \n if (startIndex < 0 || endIndex >= sourceCode.text.length) {\n return false;\n }\n \n const charBefore = sourceCode.text[startIndex];\n const charAfter = sourceCode.text[endIndex];\n \n return charBefore === '(' && charAfter === ')';\n }\n\n function checkAssertion(node: any) {\n // Skip if this assertion is part of a chained assertion\n // (we only want to enforce parentheses on the outermost assertion)\n if (isPartOfChainedAssertion(node)) return;\n \n if (isWrappedInParens(node)) return;\n\n context.report({\n node,\n messageId: \"missingParens\",\n fix(fixer) {\n return fixer.replaceText(node, `(${sourceCode.getText(node)})`);\n },\n });\n }\n\n return defineTemplateBodyVisitor(context, {\n // Handle TypeScript 'as' assertions\n // Note: Angle bracket assertions (<Type>value) are not possible in Vue templates\n // due to conflicts with HTML syntax\n TSAsExpression: checkAssertion,\n });\n },\n};\n\nexport default rule;\n","import typeAssertionParens from './rules/type-assertion-parens';\n\nconst plugin = {\n rules: {\n 'type-assertion-parens': typeAssertionParens,\n },\n configs: {\n recommended: {\n plugins: ['vue-type-assertion-parens'],\n rules: {\n 'vue-type-assertion-parens/type-assertion-parens': 'error',\n },\n },\n },\n};\n\nexport = plugin;\n"],"mappings":";;;;;;;;;AAEA,SAAS,iCAAiC;AAF1C,IAIM,MAgEC;AApEP;AAAA;AAAA;AAIA,IAAM,OAAwB;AAAA,MAC5B,MAAM;AAAA,QACJ,MAAM;AAAA,QACN,MAAM;AAAA,UACJ,aAAa;AAAA,UACb,aAAa;AAAA,UACb,KAAK;AAAA,QACP;AAAA,QACA,SAAS;AAAA,QACT,QAAQ,CAAC;AAAA,QACT,UAAU;AAAA,UACR,eAAe;AAAA,QACjB;AAAA,MACF;AAAA,MAEA,OAAO,SAA2B;AAChC,cAAM,aAAa,QAAQ,cAAc;AAEzC,iBAAS,yBAAyB,MAAW;AAG3C,iBAAO,KAAK,UAAU,KAAK,OAAO,SAAS,oBAAoB,KAAK,OAAO,eAAe;AAAA,QAC5F;AAEA,iBAAS,kBAAkB,MAAW;AAEpC,gBAAM,aAAa,KAAK,MAAM,CAAC,IAAI;AACnC,gBAAM,WAAW,KAAK,MAAM,CAAC;AAE7B,cAAI,aAAa,KAAK,YAAY,WAAW,KAAK,QAAQ;AACxD,mBAAO;AAAA,UACT;AAEA,gBAAM,aAAa,WAAW,KAAK,UAAU;AAC7C,gBAAM,YAAY,WAAW,KAAK,QAAQ;AAE1C,iBAAO,eAAe,OAAO,cAAc;AAAA,QAC7C;AAEA,iBAAS,eAAe,MAAW;AAGjC,cAAI,yBAAyB,IAAI,EAAG;AAEpC,cAAI,kBAAkB,IAAI,EAAG;AAE7B,kBAAQ,OAAO;AAAA,YACb;AAAA,YACA,WAAW;AAAA,YACX,IAAI,OAAO;AACT,qBAAO,MAAM,YAAY,MAAM,IAAI,WAAW,QAAQ,IAAI,CAAC,GAAG;AAAA,YAChE;AAAA,UACF,CAAC;AAAA,QACH;AAEA,eAAO,0BAA0B,SAA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