@ubiquity-os/eslint-plugin-no-empty-strings
Version:
ESLint rule to disallow empty string literals in runtime/value contexts for TypeScript and JSX/TSX.
3 lines (2 loc) • 2.28 kB
JavaScript
import{ESLintUtils as p}from"@typescript-eslint/utils";var u=p.RuleCreator(e=>`https://github.com/ubiquity-os/no-empty-strings/docs/rules/${e}`);function c(e){return e.replace(/\uFEFF/gu,"").replace(/\u200B/gu,"").replace(/\u200C/gu,"").replace(/\u200D/gu,"").replace(/\u200E/gu,"").replace(/\u200F/gu,"").length===0}function y(e){return e.trim().length===0}function f(e){for(;e;){if(e.type==="TSTypeAnnotation"||e.type==="TSTypeAliasDeclaration"||e.type==="TSLiteralType"||e.type==="TSTypeReference"||e.type==="TSInterfaceDeclaration"||e.type==="TSPropertySignature")return!0;e=e.parent??null}return!1}function S(e){for(;e;){if(e.type==="ImportDeclaration"||e.type==="ExportAllDeclaration"||e.type==="ExportNamedDeclaration"||e.type==="ImportExpression")return!0;e=e.parent??null}return!1}function g(e,i){if(!i||i.length===0)return!1;let r=e;for(;r;){if(i.indexOf(r.type)!==-1)return!0;r=r.parent??null}return!1}var T=u({name:"no-empty-strings",meta:{type:"problem",docs:{description:"Disallow empty string literals in runtime/value contexts"},messages:{emptyStringNotAllowed:"Empty string literal is not allowed."},schema:[{type:"object",properties:{checkWhitespaceOnly:{type:"boolean"},checkNoSubstitutionTemplates:{type:"boolean"},ignoreZeroWidth:{type:"boolean"},allowContexts:{type:"array",items:{type:"string"}}},additionalProperties:!1}]},defaultOptions:[{checkWhitespaceOnly:!1,checkNoSubstitutionTemplates:!0,ignoreZeroWidth:!1,allowContexts:[]}],create:function(i,[r]){function a(n,t){if(g(t,r.allowContexts)||f(t)||S(t))return;let l=n;if(!(r.ignoreZeroWidth===!0&&c(l))){if(l.length===0){i.report({node:t,messageId:"emptyStringNotAllowed"});return}r.checkWhitespaceOnly&&y(l)&&i.report({node:t,messageId:"emptyStringNotAllowed"})}}return{Literal:function(t){typeof t.value=="string"&&(t.parent&&t.parent.type==="JSXAttribute"||a(String(t.value),t))},TemplateLiteral:function(t){if(t.expressions&&t.expressions.length>0||!r.checkNoSubstitutionTemplates)return;let l=t.quasis??[];if(l.length!==1)return;let o=l[0]?.value?.cooked??"";a(o,t)},JSXAttribute:function(t){t.value&&t.value.type==="Literal"&&typeof t.value.value=="string"&&a(String(t.value.value),t.value)}}}}),s=T;var m={rules:{"no-empty-strings":s}},v=m;export{v as default};
//# sourceMappingURL=index.js.map