UNPKG

@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.73 kB
"use strict";var s=Object.defineProperty;var c=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var S=(e,r)=>{for(var i in r)s(e,i,{get:r[i],enumerable:!0})},g=(e,r,i,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let l of y(r))!f.call(e,l)&&l!==i&&s(e,l,{get:()=>r[l],enumerable:!(n=c(r,l))||n.enumerable});return e};var T=e=>g(s({},"__esModule",{value:!0}),e);var k={};S(k,{default:()=>A});module.exports=T(k);var o=require("@typescript-eslint/utils"),m=o.ESLintUtils.RuleCreator(e=>`https://github.com/ubiquity-os/no-empty-strings/docs/rules/${e}`);function h(e){return e.replace(/\uFEFF/gu,"").replace(/\u200B/gu,"").replace(/\u200C/gu,"").replace(/\u200D/gu,"").replace(/\u200E/gu,"").replace(/\u200F/gu,"").length===0}function b(e){return e.trim().length===0}function E(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 N(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 v(e,r){if(!r||r.length===0)return!1;let i=e;for(;i;){if(r.indexOf(i.type)!==-1)return!0;i=i.parent??null}return!1}var x=m({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(r,[i]){function n(l,t){if(v(t,i.allowContexts)||E(t)||N(t))return;let a=l;if(!(i.ignoreZeroWidth===!0&&h(a))){if(a.length===0){r.report({node:t,messageId:"emptyStringNotAllowed"});return}i.checkWhitespaceOnly&&b(a)&&r.report({node:t,messageId:"emptyStringNotAllowed"})}}return{Literal:function(t){typeof t.value=="string"&&(t.parent&&t.parent.type==="JSXAttribute"||n(String(t.value),t))},TemplateLiteral:function(t){if(t.expressions&&t.expressions.length>0||!i.checkNoSubstitutionTemplates)return;let a=t.quasis??[];if(a.length!==1)return;let u=a[0]?.value?.cooked??"";n(u,t)},JSXAttribute:function(t){t.value&&t.value.type==="Literal"&&typeof t.value.value=="string"&&n(String(t.value.value),t.value)}}}}),p=x;var w={rules:{"no-empty-strings":p}},A=w; //# sourceMappingURL=index.cjs.map