@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 6.63 kB
JavaScript
import*as Common from"../../core/common/common.js";import*as Platform from"../../core/platform/platform.js";import*as SDK from"../../core/sdk/sdk.js";import*as TextUtils from"../text_utils/text_utils.js";import*as Workspace from"../workspace/workspace.js";import{ContentProviderBasedProject}from"./ContentProviderBasedProject.js";import{IgnoreListManager}from"./IgnoreListManager.js";import{NetworkProject}from"./NetworkProject.js";export class CompilerScriptMapping{#e;#o;#t=new Map;#r;#s;#c=new Map;#n=new Map;#a=new Platform.MapUtilities.Multimap;constructor(e,o,t){this.#e=e.sourceMapManager(),this.#o=t,this.#r=new ContentProviderBasedProject(o,"jsSourceMaps:stub:"+e.target().id(),Workspace.Workspace.projectTypes.Service,"",!0),this.#s=[this.#e.addEventListener(SDK.SourceMapManager.Events.SourceMapWillAttach,this.sourceMapWillAttach,this),this.#e.addEventListener(SDK.SourceMapManager.Events.SourceMapFailedToAttach,this.sourceMapFailedToAttach,this),this.#e.addEventListener(SDK.SourceMapManager.Events.SourceMapAttached,this.sourceMapAttached,this),this.#e.addEventListener(SDK.SourceMapManager.Events.SourceMapDetached,this.sourceMapDetached,this)]}addStubUISourceCode(e){const o=this.#r.addContentProvider(Common.ParsedURL.ParsedURL.concatenate(e.sourceURL,":sourcemap"),TextUtils.StaticContentProvider.StaticContentProvider.fromString(e.sourceURL,Common.ResourceType.resourceTypes.Script,"\n\n\n\n\n// Please wait a bit.\n// Compiled script is not shown while source map is being loaded!"),"text/javascript");this.#t.set(e,o)}removeStubUISourceCode(e){const o=this.#t.get(e);this.#t.delete(e),o&&this.#r.removeUISourceCode(o.url())}getLocationRangesForSameSourceLocation(e){const o=e.debuggerModel,t=e.script();if(!t)return[];const r=this.#e.sourceMapForClient(t);if(!r)return[];const{lineNumber:s,columnNumber:c}=t.rawLocationToRelativeLocation(e),n=r.findEntry(s,c);if(!n||!n.sourceURL)return[];const a=this.#n.get(r);if(!a)return[];const i=a.uiSourceCodeForURL(n.sourceURL);if(!i)return[];if(!this.#a.hasValue(i,r))return[];return r.findReverseRanges(n.sourceURL,n.sourceLineNumber,n.sourceColumnNumber).map((({startLine:e,startColumn:r,endLine:s,endColumn:c})=>{const n=t.relativeLocationToRawLocation({lineNumber:e,columnNumber:r}),a=t.relativeLocationToRawLocation({lineNumber:s,columnNumber:c});return{start:o.createRawLocation(t,n.lineNumber,n.columnNumber),end:o.createRawLocation(t,a.lineNumber,a.columnNumber)}}))}uiSourceCodeForURL(e,o){const t=o?Workspace.Workspace.projectTypes.ContentScripts:Workspace.Workspace.projectTypes.Network;for(const o of this.#c.values()){if(o.type()!==t)continue;const r=o.uiSourceCodeForURL(e);if(r)return r}return null}rawLocationToUILocation(e){const o=e.script();if(!o)return null;const{lineNumber:t,columnNumber:r}=o.rawLocationToRelativeLocation(e),s=this.#t.get(o);if(s)return new Workspace.UISourceCode.UILocation(s,t,r);const c=this.#e.sourceMapForClient(o);if(!c)return null;const n=this.#n.get(c);if(!n)return null;const a=c.findEntry(t,r);if(!a||!a.sourceURL)return null;const i=n.uiSourceCodeForURL(a.sourceURL);return i&&this.#a.hasValue(i,c)?i.uiLocation(a.sourceLineNumber,a.sourceColumnNumber):null}uiLocationToRawLocations(e,o,t){const r=[];for(const s of this.#a.get(e)){const c=s.sourceLineMapping(e.url(),o,t);if(!c)continue;const n=this.#e.clientForSourceMap(s);if(!n)continue;const a=n.relativeLocationToRawLocation(c);r.push(n.debuggerModel.createRawLocation(n,a.lineNumber,a.columnNumber))}return r}uiLocationRangeToRawLocationRanges(e,o){if(!this.#a.has(e))return null;const t=[];for(const r of this.#a.get(e)){const s=this.#e.clientForSourceMap(r);if(s)for(const c of r.reverseMapTextRanges(e.url(),o)){const e=s.relativeLocationToRawLocation(c.start),o=s.relativeLocationToRawLocation(c.end),r=s.debuggerModel.createRawLocation(s,e.lineNumber,e.columnNumber),n=s.debuggerModel.createRawLocation(s,o.lineNumber,o.columnNumber);t.push({start:r,end:n})}}return t}getMappedLines(e){if(!this.#a.has(e))return null;const o=new Set;for(const t of this.#a.get(e))for(const r of t.mappings())r.sourceURL===e.url()&&o.add(r.sourceLineNumber);return o}sourceMapWillAttach(e){const{client:o}=e.data;this.addStubUISourceCode(o),this.#o.updateLocations(o),IgnoreListManager.instance().isUserIgnoreListedURL(o.sourceURL,{isContentScript:o.isContentScript()})&&this.#e.cancelAttachSourceMap(o)}sourceMapFailedToAttach(e){const{client:o}=e.data;this.removeStubUISourceCode(o),this.#o.updateLocations(o)}sourceMapAttached(e){const{client:o,sourceMap:t}=e.data,r=new Set([o]);this.removeStubUISourceCode(o);const s=o.target(),c=`jsSourceMaps:${o.isContentScript()?"extensions":""}:${s.id()}`;let n=this.#c.get(c);if(!n){const e=o.isContentScript()?Workspace.Workspace.projectTypes.ContentScripts:Workspace.Workspace.projectTypes.Network;n=new ContentProviderBasedProject(this.#r.workspace(),c,e,"",!1),NetworkProject.setTargetForProject(n,s),this.#c.set(c,n)}this.#n.set(t,n);for(const e of t.sourceURLs()){const s=Common.ResourceType.resourceTypes.SourceMapScript,c=n.createUISourceCode(e,s);t.hasIgnoreListHint(e)&&c.markKnownThirdParty();const a=t.embeddedContentByURL(e),i=null!==a?TextUtils.StaticContentProvider.StaticContentProvider.fromString(e,s,a):new SDK.CompilerSourceMappingContentProvider.CompilerSourceMappingContentProvider(e,s,o.createPageResourceLoadInitiator());let u=null;if(null!==a){const e=new TextEncoder;u=new Workspace.UISourceCode.UISourceCodeMetadata(null,e.encode(a).length)}const p=Common.ResourceType.ResourceType.mimeFromURL(e)??s.canonicalMimeType();this.#a.set(c,t),NetworkProject.setInitialFrameAttribution(c,o.frameId);const d=n.uiSourceCodeForURL(e);if(null!==d){for(const o of this.#a.get(d)){this.#a.delete(d,o);const s=this.#e.clientForSourceMap(o);s&&(NetworkProject.removeFrameAttribution(d,s.frameId),t.compatibleForURL(e,o)&&(this.#a.set(c,o),NetworkProject.addFrameAttribution(c,s.frameId)),r.add(s))}n.removeUISourceCode(e)}n.addUISourceCodeWithProvider(c,i,u,p)}Promise.all([...r].map((e=>this.#o.updateLocations(e)))).then((()=>this.sourceMapAttachedForTest(t)))}sourceMapDetached(e){const{client:o,sourceMap:t}=e.data,r=this.#n.get(t);if(r){for(const e of r.uiSourceCodes())this.#a.delete(e,t)&&(NetworkProject.removeFrameAttribution(e,o.frameId),this.#a.has(e)||r.removeUISourceCode(e.url()));this.#n.delete(t),this.#o.updateLocations(o)}}scriptsForUISourceCode(e){const o=[];for(const t of this.#a.get(e)){const e=this.#e.clientForSourceMap(t);e&&o.push(e)}return o}sourceMapAttachedForTest(e){}dispose(){Common.EventTarget.removeEventListeners(this.#s);for(const e of this.#c.values())e.dispose();this.#r.dispose()}}