| 1 |
"use strict";import{format,isEOL}from"./format";import{parseTree,findNodeAtLocation}from"./parser";export function removeProperty(t,e,n){return setProperty(t,e,void 0,n)}export function setProperty(t,e,n,o){const r=e.slice(),f=parseTree(t,[]);let i,l;for(;r.length>0&&(l=r.pop(),i=findNodeAtLocation(f,r),void 0===i&&void 0!==n);)n="string"==typeof l?{[l]:n}:[n];if(i){if("object"===i.type&&"string"==typeof l&&Array.isArray(i.children)){const e=findNodeAtLocation(i,[l]);if(void 0!==e){if(void 0===n){if(!e.parent)throw new Error("Malformed AST");const n=i.children.indexOf(e.parent);let r,f=e.parent.offset+e.parent.length;if(n>0){let t=i.children[n-1];r=t.offset+t.length}else r=i.offset+1,i.children.length>1&&(f=i.children[1].offset);return withFormatting(t,{offset:r,length:f-r,content:""},o)}return withFormatting(t,{offset:e.offset,length:e.length,content:JSON.stringify(n)},o)}{if(void 0===n)return[];const e=`${JSON.stringify(l)}: ${JSON.stringify(n)}`,r=o.getInsertionIndex?o.getInsertionIndex(i.children.map((t=>t.children[0].value))):i.children.length;let f;if(r>0){let t=i.children[r-1];f={offset:t.offset+t.length,length:0,content:","+e}}else f=0===i.children.length?{offset:i.offset+1,length:0,content:e}:{offset:i.offset+1,length:0,content:e+","};return withFormatting(t,f,o)}}if("array"===i.type&&"number"==typeof l&&Array.isArray(i.children)){const e=l;if(-1===e){const e=`${JSON.stringify(n)}`;let r;if(0===i.children.length)r={offset:i.offset+1,length:0,content:e};else{const t=i.children[i.children.length-1];r={offset:t.offset+t.length,length:0,content:","+e}}return withFormatting(t,r,o)}if(void 0===n&&i.children.length>=0){const e=l,n=i.children[e];let r;if(1===i.children.length)r={offset:i.offset+1,length:i.length-2,content:""};else if(i.children.length-1===e){let t=i.children[e-1],n=t.offset+t.length;r={offset:n,length:i.offset+i.length-2-n,content:""}}else r={offset:n.offset,length:i.children[e+1].offset-n.offset,content:""};return withFormatting(t,r,o)}if(void 0!==n){let e;const r=`${JSON.stringify(n)}`;if(!o.isArrayInsertion&&i.children.length>l){const t=i.children[l];e={offset:t.offset,length:t.length,content:r}}else if(0===i.children.length||0===l)e={offset:i.offset+1,length:0,content:0===i.children.length?r:r+","};else{const t=l>i.children.length?i.children.length:l,n=i.children[t-1];e={offset:n.offset+n.length,length:0,content:","+r}}return withFormatting(t,e,o)}throw new Error(`Can not ${void 0===n?"remove":o.isArrayInsertion?"insert":"modify"} Array index ${e} as length is not sufficient`)}throw new Error(`Can not add ${"number"!=typeof l?"index":"property"} to parent of type ${i.type}`)}if(void 0===n)throw new Error("Can not delete in empty document");return withFormatting(t,{offset:f?f.offset:0,length:f?f.length:0,content:JSON.stringify(n)},o)}function withFormatting(t,e,n){if(!n.formattingOptions)return[e];let o=applyEdit(t,e),r=e.offset,f=e.offset+e.content.length;if(0===e.length||0===e.content.length){for(;r>0&&!isEOL(o,r-1);)r--;for(;f<o.length&&!isEOL(o,f);)f++}const i=format(o,{offset:r,length:f-r},{...n.formattingOptions,keepLines:!1});for(let t=i.length-1;t>=0;t--){const e=i[t];o=applyEdit(o,e),r=Math.min(r,e.offset),f=Math.max(f,e.offset+e.length),f+=e.content.length-e.length}return[{offset:r,length:t.length-(o.length-f)-r,content:o.substring(r,f)}]}export function applyEdit(t,e){return t.substring(0,e.offset)+e.content+t.substring(e.offset+e.length)}export function isWS(t,e){return-1!=="\r\n \t".indexOf(t.charAt(e))} |