UNPKG

prettier-plugin-phpdoc

Version:
13 lines (12 loc) 12.3 kB
"use strict";var T=Object.defineProperty;var $=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var E=Object.prototype.hasOwnProperty;var M=(n,e)=>{for(var i in e)T(n,i,{get:e[i],enumerable:!0})},O=(n,e,i,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of R(e))!E.call(n,s)&&s!==i&&T(n,s,{get:()=>e[s],enumerable:!(t=$(e,s))||t.enumerable});return n};var V=n=>O(T({},"__esModule",{value:!0}),n);var B={};M(B,{options:()=>w,parsers:()=>j});module.exports=V(B);function d(n){return n.replace(/\s+$/,"")}function m(n){return n.replace(/^\s+/,"")}var g=class{constructor(e){this._text=e;this.pos=0;this._lastResult=null}maybe(e,i=!0){let t=this.pos;if(i)for(;t<this._text.length&&[" "," ",` `].includes(this._text[t]);)t++;for(let s of e)if(!(typeof s=="string"&&s.length>this._text.length-t))if(typeof s=="string"){if(this._text.slice(t,t+s.length)===s)return t+=s.length,this.pos=t,this._lastResult=s,s}else{let r=this._text.slice(t).match(s);if(r)return t+=r[0].length,this.pos=t,this._lastResult=r[0],r[0]}return null}_peek(e){let i=this.pos,t=this.maybe(e);return this.pos=i,t}_expect(e,i){let t=this.maybe(e);if(t===null){let s=e.join(" or ");throw new Error(`expected ${s} at offset ${this.pos} at ${i.join(".")}: ${this._text.slice(this.pos,25)||"<eol>"}`)}return t}_maybeGeneric(e){if(!this.maybe(["<"]))return null;let i=[];do{if(this.maybe([">"]))return i;let t=this.parseType([...e,"generic"]);t.kind==="simple-value"&&!this._peek([",",">"])?i.push({kind:"modifier-keyword",modifier:t,target:this.parseType([...e,"modifier"])}):i.push(t)}while(this.maybe([","]));return this._expect([">"],e),i}parseType(e,i=!0){let t=null;if(this.maybe(["?"]))t={kind:"maybe",type:this.parseType([...e,"maybe"])};else if(this.maybe([/^array-[\\a-zA-Z0-9\-\_]+/]))t={kind:"simple-value",value:this._lastResult};else if(this.maybe(["array"]))if(this.maybe(["{"]))for(t={entries:[],kind:"tuple-dict"};!this.maybe(["}"]);){let r=this.parseType([...e,"array"]),p=!!this.maybe(["?"]);if(this.maybe([":"])){let a=this.parseType([...e,"array-value"]);t.entries.push({key:r,value:a,optional:p})}else t.entries.push({key:null,value:r});if(!this.maybe([","])){this._expect(["}"],[...e,"array"]);break}}else if(this.maybe(["<"])){let r=this.parseType([...e,"array-record"]);if(this.maybe([",",">"])===","){let p=this.parseType([...e,"array-record-value"]);this._expect([">"],[...e,"array-value"]),t={kind:"record",key:r,value:p}}else t={kind:"arrayList",type:r,name:"array"}}else t={value:"array",kind:"simple-value"};else if(this.maybe(["list"]))this.maybe(["<"])?(t={kind:"arrayList",type:this.parseType([...e,"list"]),name:"list"},this._expect([">"],[...e,"array"])):t={value:"list",kind:"simple-value"};else if(this.maybe(['"',"'"])){let r=this._lastResult==='"'?"double":"single",p="";for(;this._text[this.pos]!==this._lastResult&&this._text[this.pos-1]!=="\\"&&this.pos<this._text.length;)p+=this._text[this.pos],this.pos++;if(this.pos===this._text.length)throw new Error("unterminated string");this._expect([this._lastResult],[...e,"string"]),t={kind:"string",value:p,quote:r}}else if(this.maybe([/^((&)?\$)?(-?)[\\a-zA-Z0-9\-\_]+/])){t={kind:"simple-value",value:this._lastResult};let p=this._maybeGeneric([...e,"primitive"]);p&&(t={kind:"with-generics",node:t,generics:p}),this.maybe(["::"])&&(t={kind:"static-access",class:t,member:this.parseType([...e,"static-access"],!1)})}else if(this.maybe(["("])){let r=this.parseType([...e,"parentheses"]);this._expect([")"],[...e,"parentheses"]),t={kind:"parentheses",type:r}}else if(this.maybe(["..."])){t={kind:"spread"};let r=this._maybeGeneric([...e,"spread"]);r&&(t={kind:"with-generics",node:t,generics:r})}else throw new Error(`unexpected token at ${e.join(".")}: ${this._text.slice(this.pos,25)}`);if(this.maybe(["("])){let r=[];for(;!this.maybe([")"]);){let a=this.parseType([...e,"parentheses"]);if(this.maybe([/^(&)?\$[a-zA-Z0-9]+/])?r.push({name:this._lastResult,type:a}):r.push({type:a}),!this.maybe([","])){this._expect([")"],[...e,"parentheses"]);break}}this._expect([":"],[...e,"parentheses"]);let p=this.parseType([...e,"return-type"]);t={kind:"callable",node:t,parameters:r,returnType:p}}for(;this.maybe(["["],!1);){let r=null;this.maybe(["]"])||(r=this.parseType([...e,"array-index"]),this._expect(["]"],[...e,"array-index"])),t={kind:"array-square-bracket",type:t,indexType:r}}let s=[t];for(;i&&this.maybe(["|"]);)s.push(this.parseType([...e,"union"],!1));if(s.length===1?t=s[0]:t={kind:"union",types:s},this.maybe(["is"])&&e.length>1){let r=this.parseType([...e,"conditional:is"]);this._expect(["?"],[...e,"conditional"]);let p=this.parseType([...e,"conditional:true"]);this._expect([":"],[...e,"conditional"]);let a=this.parseType([...e,"conditional:false"]);t={kind:"conditional-type",check:t,extends:r,trueType:p,falseType:a}}return t}};function v(n,e){let i=new g(n),t=i.parseType([typeof e.loc?.start.line=="number"?`Line:${e.loc.start.line}`:"comment"]);for(;i.maybe([" "," ",` `],!1););return{parsedType:t,rest:n.slice(i.pos)}}var G=["method","param","property","return","type","throws","var","phpstan-var","phpstan-param"];function q(n){n=n.replace(/^\/\*\*?/,"").replace(/\s*\*\/$/,""),n=n.replace(/^\s*\*+/gm,"");let e=n.split(` `),i=[];for(let t=0;t<e.length;t++){let s=d(e[t]);if(s[0]===" "&&(s=s.slice(1)),m(s).startsWith("@")){let[r,...p]=m(s).split(/\s/),a=p.join(" ");i.push({type:1,tag:r,description:a})}else{let r=i[i.length-1];r?.type===1?r.description+=` `+s:i.push({type:0,content:s})}}return i}function L(n){return q(n.value).map(i=>{if(i.type===0)return i;let{tag:t,description:s}=i;if(!G.includes(t.slice(1)))return{...i,parsedType:null};let{parsedType:r,rest:p}=v(s,n);return{...i,parsedType:r,description:p}})}function S(n){let e=n+1;return Object.values(N).includes(e)?e:null}var N=(s=>(s[s.Always=0]="Always",s[s.L1=1]="L1",s[s.L2=2]="L2",s[s.L3=3]="L3",s))(N||{}),_=class{constructor(e,i){this._wrapLevel=e;this._options=i;this._lines=[];this._line="";this._indent=0}get _whiteSpace(){return this._options.useTabs?" ":" ".repeat(this._options.tabWidth)}_hasLevel(e){return e<=this._wrapLevel}_changeIndent(e,i){this._hasLevel(e)&&(i==="+1"?this._indent++:this._indent--)}_newline(e){this._hasLevel(e)&&(this._lines.push(this._line),this._indent?this._line=" "+this._whiteSpace.repeat(this._indent):this._line="")}_print(e){this._line+=e}_preprocessType(e){if(typeof e!="object"||!e)return e;if("kind"in e){if(e.kind==="maybe")this._options.expandNull&&(e={kind:"union",types:[{kind:"simple-value",value:"null"},e.type]});else if(e.kind==="union"){let t=[];for(let s=0;s<e.types.length;s++){let r=e.types[s];r.kind==="simple-value"&&r.value==="null"?t.unshift(this.preprocessType(r)):this._options.expandNull&&r.kind==="maybe"?(t.unshift(this.preprocessType({kind:"simple-value",value:"null"})),t.push(this.preprocessType(r.type))):t.push(this.preprocessType(r))}e={...e,types:t}}}if(Array.isArray(e))return e.map(t=>this.preprocessType(t));let i={};for(let t in e)i[t]=this.preprocessType(e[t]);return i}preprocessType(e){return this._preprocessType(e)}printType(e){if(e.kind==="maybe")this._options.expandNull?this.printType({kind:"union",types:[{kind:"simple-value",value:"null"},e.type]}):(this._print("?"),this.printType(e.type));else if(e.kind==="simple-value")this._print(e.value);else if(e.kind==="with-generics"){this.printType(e.node),this._changeIndent(2,"+1"),this._print("<"),this._newline(2);for(let i=0;i<e.generics.length;i++)this.printType(e.generics[i]),(i!==e.generics.length-1||this._hasLevel(2))&&this._print(", "),i!==e.generics.length-1&&this._newline(2);this._changeIndent(2,"-1"),this._newline(2),this._print(">")}else if(e.kind==="static-access")this.printType(e.class),this._print("::"),this.printType(e.member);else if(e.kind==="tuple-dict"){if(this._print("array{"),e.entries.length){let i=e.entries.every(t=>t.key)?1:3;e.entries.length>2&&(i=0),this._changeIndent(i,"+1"),this._newline(i);for(let t=0;t<e.entries.length;t++){let s=e.entries[t];s.key?(this.printType(s.key),s.optional&&this._print("?"),this._print(": "),this.printType(s.value)):this.printType(e.entries[t].value),(t!==e.entries.length-1||this._hasLevel(i))&&this._print(", "),t!==e.entries.length-1&&this._newline(i)}this._changeIndent(i,"-1"),this._newline(i)}this._print("}")}else if(e.kind==="record")this._print("array<"),this.printType(e.key),this._print(", "),this.printType(e.value),this._print(">");else if(e.kind==="arrayList")this._print(`${e.name}<`),this.printType(e.type),this._print(">");else if(e.kind==="array-square-bracket")this.printType(e.type),this._print("["),e.indexType&&this.printType(e.indexType),this._print("]");else if(e.kind==="union"){let i=[];for(let t=0;t<e.types.length;t++){let s=e.types[t];s.kind==="simple-value"&&s.value==="null"?i.unshift(s):this._options.expandNull&&s.kind==="maybe"?(i.unshift({kind:"simple-value",value:"null"}),i.push(s.type)):i.push(s)}for(let t=0;t<i.length;t++)t>0&&this._print(" | "),this.printType(i[t])}else if(e.kind==="callable"){this.printType(e.node),this._print("(");for(let i=0;i<e.parameters.length;i++)i>0&&this._print(", "),this.printType(e.parameters[i].type),e.parameters[i].name&&this._print(" "+e.parameters[i].name);this._print("): "),e.returnType.kind==="union"?this.printType({kind:"parentheses",type:e.returnType}):this.printType(e.returnType)}else if(e.kind==="string"){let i=e.quote==="single"?"'":'"';this._print(i+e.value+i)}else e.kind==="parentheses"?(this._print("("),this.printType(e.type),this._print(")")):e.kind==="conditional-type"?(this._changeIndent(3,"+1"),this._newline(3),this.printType(e.check),this._print(" is "),this.printType(e.extends),this._newline(3),this._print(" ? "),this.printType(e.trueType),this._newline(3),this._print(" : "),this.printType(e.falseType),this._changeIndent(3,"-1")):e.kind==="spread"?this._print("..."):e.kind==="modifier-keyword"?(this.printType(e.modifier),this._print(" "),this.printType(e.target)):D(e)}getLines(){return this._lines.push(this._line),this._lines}};function D(n){throw new Error("Unexpected object: "+n)}function W(n,e,i){let t=new _(i,e);return t.printType(t.preprocessType(n)),t.getLines()}function b(n,e,i){let t=n.split(` `),s=[];for(let r=0;r<t.length;r++){let p=r===0&&i?i:"";for(let a of t[r].split(" "))e.shouldWrap&&(p+a).replace(/\t/g,()=>" ".repeat(e.tabWidth)).length>e.width&&(s.push(d(p)),p=""),p+=a+" ";s.push(d(p))}return s}function I(n,e){let i=e.split(` `);return n.length===0?i:(n[n.length-1]+=i[0],[...n,...i.slice(1)])}function C(n,e,i){let t=e.printWidth-(i.length+3),s=!1,r={width:t,tabWidth:e.tabWidth,shouldWrap:e.wrapText},p=[];for(let o=0;o<n.length;o++){let l=n[o];if(l.type===0)p.push(...b(l.content,r));else if(!l.parsedType)p.push(...b(`${l.tag} ${l.description}`,r));else{!s&&n[o-1]?.type===0&&(p.push(""),s=!0);let y=l.description.split(" "),u=l.parsedType?2:1,A=y.slice(0,u).join(" "),c=0,h=[];do h=W(l.parsedType,e,c),c=S(c);while(c&&[`${l.tag} ${h[0]}`,...h].some(f=>f.length>t));if(p.push(`${l.tag} ${h[0]}`),p.push(...h.slice(1)),l.description&&(p=I(p,` ${A}`)),y.length>u){let f=b(` ${y.slice(u).join(" ")}`,r,p[p.length-1]);p.pop(),p.push(...f)}}}let a=[];for(let o=0;o<p.length;o++)p[o-1]===""&&p[o]===""||a.push(p[o]);return a}var U=" * ",k="/**",x=" */";function P(n,e,i){if(!e.comments)return e;let t=n.split(` `);return e.comments=e.comments.map(s=>{if(s.kind==="commentline"||!s.value.startsWith("/**"))return s;let r="";s.loc?.start.column&&(r=t[s.loc.start.line-1].slice(0,s.loc.start.column));let p=s.value.includes(` `),a=L(s),o=r+`${k} ${x}`,l=C(a,i,o);return p||l.length>1||l[0].length+o.length>i.printWidth?s.value=`${k} ${l.map(y=>U+y).join(` `)} ${x}`:s.value=`${k} ${l[0]}${x}`,s}),e}var K=()=>({get php(){let n=(()=>{try{return require("@prettier/plugin-php/standalone")}catch(i){throw new Error(`PHP parser not found, please install @prettier/plugin-php. Error: ${i.message}`)}})(),e=n.parsers.php.parse;return{...n.parsers.php,parse:e?(i,t)=>{let s=e(i,t);return"then"in s?s.then(r=>P(i,r,t)):P(i,s,t)}:P}}});var w={wrapText:{category:"Global",type:"boolean",description:"Wether to wrap text or not",default:!1},expandNull:{category:"Global",type:"boolean",description:"Wether to expand ?int (and other types) to int|null or not",default:!1}};var j=K();0&&(module.exports={options,parsers});