12345 |
- /*
- All material copyright ESRI, All Rights Reserved, unless otherwise specified.
- See https://js.arcgis.com/4.24/esri/copyright.txt for details.
- */
- import{Syntax as t}from"./syntax.js";class e{constructor(){this.attach=!1,this.comments=[],this.stack=[],this.leading=[],this.trailing=[]}insertInnerComments(e,n){if(e.type===t.BlockStatement&&0===e.body.length){const t=[];for(let e=this.leading.length-1;e>=0;--e){const s=this.leading[e];n.end.offset>=s.start&&(t.unshift(s.comment),this.leading.splice(e,1),this.trailing.splice(e,1))}t.length&&(e.innerComments=t)}}findTrailingComments(t){let e=[];if(this.trailing.length>0){for(let n=this.trailing.length-1;n>=0;--n){const s=this.trailing[n];s.start>=t.end.offset&&e.unshift(s.comment)}return this.trailing.length=0,e}const n=this.stack[this.stack.length-1];if(n&&n.node.trailingComments){const s=n.node.trailingComments[0];s&&s.range[0]>=t.end.offset&&(e=n.node.trailingComments,delete n.node.trailingComments)}return e}findLeadingComments(t){const e=[];let n;for(;this.stack.length>0;){const e=this.stack[this.stack.length-1];if(!(e&&e.start>=t.start.offset))break;n=e.node,this.stack.pop()}if(n){for(let s=(n.leadingComments?n.leadingComments.length:0)-1;s>=0;--s){const i=n.leadingComments[s];i.range[1]<=t.start.offset&&(e.unshift(i),n.leadingComments.splice(s,1))}return n.leadingComments&&0===n.leadingComments.length&&delete n.leadingComments,e}for(let s=this.leading.length-1;s>=0;--s){const n=this.leading[s];n.start<=t.start.offset&&(e.unshift(n.comment),this.leading.splice(s,1))}return e}visitNode(e,n){if(e.type===t.Program&&e.body.length>0)return;this.insertInnerComments(e,n);const s=this.findTrailingComments(n),i=this.findLeadingComments(n);i.length>0&&(e.leadingComments=i),s.length>0&&(e.trailingComments=s),this.stack.push({node:e,start:n.start.offset})}visitComment(t,e){if(this.comments.push(t),this.attach){const n={comment:{type:t.type,value:t.value,range:[e.start.offset,e.end.offset]},start:e.start.offset};t.loc&&(n.comment.loc=t.loc),this.leading.push(n),this.trailing.push(n)}}visit(t,e){"Line"===t.type||"Block"===t.type?this.visitComment(t,e):this.attach&&this.visitNode(t,e)}}export{e as CommentHandler};
|