Socket
Socket
Sign inDemoInstall

@typescript/twoslash

Package Overview
Dependencies
Maintainers
9
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@typescript/twoslash - npm Package Compare versions

Comparing version 0.5.0 to 0.5.1

2

CHANGELOG.md

@@ -6,2 +6,4 @@ ## 0.5.0

- when `noStaticSemanticInfo` is enabled then you an still run queries
- better multi-file support
- `emit` handbook option actually emits all the JS/DTS files back to the vfs

@@ -8,0 +10,0 @@ ## 0.4.0

2

dist/index.d.ts

@@ -20,3 +20,3 @@ declare type LZ = typeof import("lz-string");

/**
* When mixed with showEmit, lets you choose the file to present instead of the source - defaults to index.js which
* Must be used with showEmit, lets you choose the file to present instead of the source - defaults to index.js which
* means when you just use `showEmit` above it shows the transpiled JS.

@@ -23,0 +23,0 @@ */

@@ -426,27 +426,4 @@ 'use strict';

var queries = [];
var highlights = []; // TODO: This doesn't handle a single file with a name
var fileContent = code.split("// @filename: ");
var noFilepaths = fileContent.length === 1;
var makeDefault = [defaultFileName, code.split(/\r\n?|\n/g)];
var makeMultiFile = function makeMultiFile(filenameSplit) {
var _filenameSplit$split = filenameSplit.split(/\r\n?|\n/g),
filename = _filenameSplit$split[0],
content = _filenameSplit$split.slice(1);
var firstLine = "// @filename: " + filename;
return [filename, [firstLine].concat(content)];
};
/**
* Oof, some hard to grok code in this section. To ensure _one_ code path for both
* default and multi-file code samples it's all coerced into an array of
* [name, lines_of_code] basically for each set.
*/
var unfilteredNameContent = noFilepaths ? [makeDefault] : fileContent.map(makeMultiFile);
var nameContent = unfilteredNameContent.filter(function (n) {
return n[0].length;
});
var highlights = [];
var nameContent = splitTwoslashCodeInfoFiles(code, defaultFileName);
/** All of the referenced files in the markup */

@@ -559,5 +536,8 @@

if (handbookOptions.emit) {
var _env$languageService$;
(_env$languageService$ = env.languageService.getProgram()) === null || _env$languageService$ === void 0 ? void 0 : _env$languageService$.emit();
filenames.forEach(function (f) {
var output = ls.getEmitOutput(f);
output.outputFiles.forEach(function (output) {
system.writeFile(output.name, output.text);
});
});
} // Code should now be safe to compile, so we're going to split it into different files

@@ -809,3 +789,42 @@

var splitTwoslashCodeInfoFiles = function splitTwoslashCodeInfoFiles(code, defaultFileName) {
var lines = code.split(/\r\n?|\n/g);
var nameForFile = code.includes("@filename: " + defaultFileName) ? "global.ts" : defaultFileName;
var currentFileContent = [];
var fileMap = [];
for (var _iterator5 = lines, _isArray5 = Array.isArray(_iterator5), _i8 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) {
var _ref5;
if (_isArray5) {
if (_i8 >= _iterator5.length) break;
_ref5 = _iterator5[_i8++];
} else {
_i8 = _iterator5.next();
if (_i8.done) break;
_ref5 = _i8.value;
}
var line = _ref5;
if (line.includes("// @filename: ")) {
fileMap.push([nameForFile, currentFileContent]);
nameForFile = line.split("// @filename: ")[1].trim();
currentFileContent = [];
} else {
currentFileContent.push(line);
}
}
fileMap.push([nameForFile, currentFileContent]); // Basically, strip these:
// ["index.ts", []]
// ["index.ts", [""]]
var nameContent = fileMap.filter(function (n) {
return n[1].length > 0 && (n[1].length > 1 || n[1][0] !== "");
});
return nameContent;
};
exports.twoslasher = twoslasher;
//# sourceMappingURL=twoslash.cjs.development.js.map

@@ -1,2 +0,2 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=(e=require("debug"))&&"object"==typeof e&&"default"in e?e.default:e,r=require("@typescript/vfs");function n(){return(n=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function i(e,t){switch(t){case"number":return+e;case"string":return e;case"boolean":return"true"===e.toLowerCase()||0===e.length}throw new Error("Unknown primitive type "+t+" with - "+e)}var o=t("twoslasher");function a(e){for(var t=[],r=[],n=0,i=0,a=0,s=function(s){var c=e[s],f=function(){i=n,n+=c.length+1},u=function(t){o("Removing line "+s+" for "+t),a++,e.splice(s,1),s--};if(c.includes("//")){var p=/^\/\/\s*\^+( .+)?$/.exec(c),d=/^\/\/\s*\^\?\s*$/.exec(c),h=/^\s*\/\/ prettier-ignore$/.exec(c),g=/^\/\/\s*\^\|$/.exec(c);if(null!==d){var m=c.indexOf("^");r.push({kind:"query",offset:m,text:void 0,docs:void 0,line:s+a-1}),u("having a query")}else if(null!==p){var v=c.indexOf("^"),x=c.lastIndexOf("^")-v+1,y=i+v,w=p[1]?p[1].trim():"";t.push({kind:"highlight",position:y,length:x,description:w,line:s}),u("having a highlight")}else if(null!==h)u("being a prettier ignore");else if(null!==g){var E=c.indexOf("^");r.push({kind:"completion",offset:E,text:void 0,docs:void 0,line:s+a-1}),u("having a completion query")}else f()}else f();l=s},l=0;l<e.length;l++)s(l);return{highlights:t,queries:r}}function s(e,t,r,n){o("Setting "+e+" to "+t);var a=function(){if(l){if(c>=s.length)return"break";f=s[c++]}else{if((c=s.next()).done)return"break";f=c.value}var n=f;if(n.name.toLowerCase()===e.toLowerCase()){switch(n.type){case"number":case"string":case"boolean":r[n.name]=i(t,n.type);break;case"list":r[n.name]=t.split(",").map((function(e){return i(e,n.element.type)}));break;default:var a=n.type;if(r[n.name]=a.get(t.toLowerCase()),o("Set "+n.name+" to "+r[n.name]),void 0===r[n.name]){var u=Array.from(a.keys());throw new Error("Invalid value "+t+" for "+n.name+". Allowed values: "+u.join(","))}}return{v:void 0}}},s=n.optionDeclarations,l=Array.isArray(s),c=0;e:for(s=l?s:s[Symbol.iterator]();;){var f,u=a();switch(u){case"break":break e;default:if("object"==typeof u)return u.v}}throw new Error("No compiler setting named '"+e+"' exists!")}var l=/^\/\/\s?@(\w+)$/,c=/^\/\/\s?@(\w+):\s?(.+)$/,f={errors:[],noErrors:!1,showEmit:!1,showEmittedFile:"index.js",noStaticSemanticInfo:!1,emit:!1,noErrorValidation:!1};exports.twoslasher=function(e,t,i,u,p,d){var h=null!=u?u:require("typescript"),g=null!=p?p:require("lz-string"),m=e,v=function(e){switch(e){case"js":case"javascript":return"js";case"ts":case"typescript":return"ts";case"tsx":return"tsx";case"jsn":return"json"}throw new Error("Cannot handle the file extension:"+e)}(t),x="index."+v;o("\n\nLooking at code: \n```"+v+"\n"+e+"\n```\n");var y={strict:!0,target:h.ScriptTarget.ES2016,allowJs:!0};!function(e){if(e.includes("// @errors "))throw new Error("You have '@errors ' - you're missing the colon after errors");if(e.includes("// @filename "))throw new Error("You have '@filename ' - you're missing the colon after filename")}(e);var w=(e=function(e){return(e=e.replace(/¨D/g,"$")).replace(/¨T/g,"~")}(e)).split(/\r\n?|\n/g),E=n({},function(e){for(var t=n({},f),r=0;r<e.length;r++){var i=void 0;(i=l.exec(e[r]))?i[1]in t&&(t[i[1]]=!0,o("Setting options."+i[1]+" to true"),e.splice(r,1),r--):(i=c.exec(e[r]))&&i[1]in t&&(t[i[1]]=i[2],o("Setting options."+i[1]+" to "+i[2]),e.splice(r,1),r--)}return"errors"in t&&"string"==typeof t.errors&&(t.errors=t.errors.split(" ").map(Number),o("Setting options.error to ",t.errors)),t}(w),{},i),b=function(e,t,r){for(var i=n({},t),o=0;o<e.length;){var a=void 0;if(a=l.exec(e[o]))i[a[1]]=!0,s(a[1],"true",i,r);else{if(!(a=c.exec(e[o]))){o++;continue}if("filename"===a[1]){o++;continue}s(a[1],a[2],i,r)}e.splice(o,1)}return i}(w,y,h),S=null!=d?d:function(e){return r.createDefaultMapFromNodeModules(e)}(b),k=r.createSystem(S),j=r.createVirtualTypeScriptEnvironment(k,[],h,b),P=j.languageService;e=w.join("\n");var A=[],O=[],C=[],F=e.split("// @filename: "),q=1===F.length,T=[x,e.split(/\r\n?|\n/g)],L=(q?[T]:F.map((function(e){var t=e.split(/\r\n?|\n/g),r=t[0],n=t.slice(1);return[r,["// @filename: "+r].concat(n)]}))).filter((function(e){return e[0].length})),I=L.map((function(e){return e[0]})),$=function(){var e,t;if(N){if(M>=D.length)return"break";Q=D[M++]}else{if((M=D.next()).done)return"break";Q=M.value}var r=Q[0],n=Q[1],i=n.join("\n");j.createFile(r,i);var o=a(n);(e=C).push.apply(e,o.highlights);var s=o.queries.map((function(e,t){var n,i,o=j.getSourceFile(r),a=h.getPositionOfLineAndCharacter(o,e.line,e.offset);switch(e.kind){case"query":var s=P.getQuickInfoAtPosition(r,a),l=P.getDefinitionAtPosition(r,a),c="Could not get LSP result: "+[(n=j.getSourceFile(r).text)[(i=a)-3],n[i-2],n[i-1],">",n[i],"<",n[i+1],n[i+2],n[i+3]].filter(Boolean).join(""),f=void 0;return s&&l&&s.displayParts&&(c=s.displayParts.map((function(e){return e.text})).join(""),f=s.documentation?s.documentation.map((function(e){return e.text})).join("<br/>"):void 0),{kind:"query",text:c,docs:f,line:e.line-t,offset:e.offset,file:r};case"completion":var u=P.getCompletionsAtPosition(r,a-1,{});if(!u)throw new Error("Twoslash: The ^| query at line "+e.line+" in "+r+" did not return any completions");var p=function(e,t){e=String(e),t=Number(t)>>>0;var r=e.slice(0,t+1).search(/\S+$/),n=e.slice(t).search(/\s/);return n<0?{word:e.slice(r),startPos:r}:{word:e.slice(r,n+t),startPos:r}}(o.text,a-1),d=o.text.slice(p.startPos,a).split(".").pop()||"";return{kind:"completions",completions:u.entries,completionPrefix:d,line:e.line-t,offset:e.offset,file:r}}}));(t=A).push.apply(t,s);var l=n.join("\n");j.updateFile(r,l)},D=L,N=Array.isArray(D),M=0;for(D=N?D:D[Symbol.iterator]();;){var Q;if("break"===$())break}var R,U=e.split(/\r\n?|\n/g);a(U),e=U.join("\n"),E.emit&&(null===(R=j.languageService.getProgram())||void 0===R||R.emit());var V=[],_=[];I.forEach((function(t){E.noErrors||(V.push.apply(V,P.getSemanticDiagnostics(t)),V.push.apply(V,P.getSyntacticDiagnostics(t)));var r=j.sys.readFile(t),n=j.getSourceFile(t);if(!n)throw new Error("No sourcefile found for "+t+" in twoslash");if(!E.showEmit){var i=-1==e.indexOf(r)?0:e.indexOf(r),o=e.slice(0,i).split("\n").length-1,a=E.noStaticSemanticInfo?[]:function(e,t){var r=[];return function n(i){e.forEachChild(i,(function(i){if(e.isIdentifier(i)){var o=i.getStart(t,!1);r.push({span:e.createTextSpan(o,i.end-o),text:i.getText(t)})}n(i)}))}(t),r}(h,n),s=Array.isArray(a),l=0;for(a=s?a:a[Symbol.iterator]();;){var c;if(s){if(l>=a.length)break;c=a[l++]}else{if((l=a.next()).done)break;c=l.value}var f=c,u=f.span,p=P.getQuickInfoAtPosition(t,u.start);if(p&&p.displayParts){var d=p.displayParts.map((function(e){return e.text})).join(""),g=f.text,m=p.documentation?p.documentation.map((function(e){return e.text})).join("\n"):void 0,v=u.start+i,x=h.createSourceFile("_.ts",e,h.ScriptTarget.ES2015),y=h.getLineAndCharacterOfPosition(x,v);_.push({text:d,docs:m,start:v,length:u.length,line:y.line,character:y.character,targetString:g})}}A.filter((function(e){return e.file===t})).forEach((function(e){var t=h.getPositionOfLineAndCharacter(n,e.line,e.offset)+i;switch(e.kind){case"query":O.push({docs:e.docs,kind:"query",start:t+i,length:e.text.length,text:e.text,offset:e.offset,line:e.line+o+1});break;case"completions":O.push({completions:e.completions,kind:"completions",start:t+i,completionsPrefix:e.completionPrefix,length:1,offset:e.offset,line:e.line+o+1})}}))}}));var Y=V.filter((function(e){return e.file&&I.includes(e.file.fileName)}));!E.noErrorValidation&&Y.length&&function(e,t,r,n){var i=e.filter((function(e){return!t.errors.includes(e.code)})),o=i.map((function(e){return e.code})).join(" ");if(i.length){var a="// @errors: "+e.map((function(e){return e.code})).join(" "),s=t.errors.length?" - the annotation specified "+t.errors:"\n\nExpected:\n"+a,l=i.map((function(e){return"["+e.code+"] - "+("string"==typeof e.messageText?e.messageText:e.messageText.messageText)})).join("\n ");throw new Error("Errors were thrown in the sample, but not included in an errors tag: "+o+s+"\n\n "+l+"\n\n## Code\n\n'''"+r+"\n"+n+"\n'''")}}(Y,E,t,m);var z=[],B=Y,J=Array.isArray(B),G=0;for(B=J?B:B[Symbol.iterator]();;){var H;if(J){if(G>=B.length)break;H=B[G++]}else{if((G=B.next()).done)break;H=G.value}var K=H,W=j.sys.readFile(K.file.fileName),X=e.indexOf(W),Z=h.flattenDiagnosticMessageText(K.messageText,"\n").replace(/</g,"&lt;"),ee="err-"+K.code+"-"+K.start+"-"+K.length,te=h.getLineAndCharacterOfPosition(K.file,K.start);z.push({category:K.category,code:K.code,length:K.length,start:K.start?K.start+X:void 0,line:te.line,character:te.character,renderedMessage:Z,id:ee})}if(E.showEmit){var re=E.showEmittedFile||x,ne=re.replace(".js","").replace(".d.ts","").replace(".map",""),ie=I.find((function(e){return e===ne+".ts"||e===ne+".tsx"}));if(!ie){var oe=I.join(", ");throw new Error("Cannot find the corresponding source file for "+re+" "+E.showEmittedFile+" - in "+oe)}var ae=P.getEmitOutput(ie),se=ae.outputFiles.find((function(e){return e.name===E.showEmittedFile}));if(!se){var le=ae.outputFiles.map((function(e){return e.name})).join(", ");throw new Error("Cannot find the file "+E.showEmittedFile+" - in "+le)}e=se.text,t=se.name.split(".").pop(),C=[],A=[],_=[]}var ce="https://www.typescriptlang.org/play/#code/"+g.compressToEncodedURIComponent(m),fe="// ---cut---\n";if(e.includes(fe)){var ue=e.indexOf(fe)+fe.length,pe=e.substr(0,ue).split("\n").length-1;e=e.split(fe).pop(),_.forEach((function(e){e.start-=ue,e.line-=pe})),_=_.filter((function(e){return e.start>-1})),z.forEach((function(e){e.start&&(e.start-=ue),e.line&&(e.line-=pe)})),z=z.filter((function(e){return e.start&&e.start>-1})),C.forEach((function(e){e.position-=ue,e.line-=pe})),C=C.filter((function(e){return e.position>-1})),O.forEach((function(e){return e.line-=pe})),O=O.filter((function(e){return e.line>-1}))}return{code:e,extension:t,highlights:C,queries:O,staticQuickInfos:_,errors:z,playgroundURL:ce}};
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=(e=require("debug"))&&"object"==typeof e&&"default"in e?e.default:e,r=require("@typescript/vfs");function n(){return(n=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function i(e,t){switch(t){case"number":return+e;case"string":return e;case"boolean":return"true"===e.toLowerCase()||0===e.length}throw new Error("Unknown primitive type "+t+" with - "+e)}var o=t("twoslasher");function a(e){for(var t=[],r=[],n=0,i=0,a=0,s=function(s){var c=e[s],f=function(){i=n,n+=c.length+1},u=function(t){o("Removing line "+s+" for "+t),a++,e.splice(s,1),s--};if(c.includes("//")){var p=/^\/\/\s*\^+( .+)?$/.exec(c),h=/^\/\/\s*\^\?\s*$/.exec(c),d=/^\s*\/\/ prettier-ignore$/.exec(c),g=/^\/\/\s*\^\|$/.exec(c);if(null!==h){var m=c.indexOf("^");r.push({kind:"query",offset:m,text:void 0,docs:void 0,line:s+a-1}),u("having a query")}else if(null!==p){var v=c.indexOf("^"),y=c.lastIndexOf("^")-v+1,x=i+v,w=p[1]?p[1].trim():"";t.push({kind:"highlight",position:x,length:y,description:w,line:s}),u("having a highlight")}else if(null!==d)u("being a prettier ignore");else if(null!==g){var E=c.indexOf("^");r.push({kind:"completion",offset:E,text:void 0,docs:void 0,line:s+a-1}),u("having a completion query")}else f()}else f();l=s},l=0;l<e.length;l++)s(l);return{highlights:t,queries:r}}function s(e,t,r,n){o("Setting "+e+" to "+t);var a=function(){if(l){if(c>=s.length)return"break";f=s[c++]}else{if((c=s.next()).done)return"break";f=c.value}var n=f;if(n.name.toLowerCase()===e.toLowerCase()){switch(n.type){case"number":case"string":case"boolean":r[n.name]=i(t,n.type);break;case"list":r[n.name]=t.split(",").map((function(e){return i(e,n.element.type)}));break;default:var a=n.type;if(r[n.name]=a.get(t.toLowerCase()),o("Set "+n.name+" to "+r[n.name]),void 0===r[n.name]){var u=Array.from(a.keys());throw new Error("Invalid value "+t+" for "+n.name+". Allowed values: "+u.join(","))}}return{v:void 0}}},s=n.optionDeclarations,l=Array.isArray(s),c=0;e:for(s=l?s:s[Symbol.iterator]();;){var f,u=a();switch(u){case"break":break e;default:if("object"==typeof u)return u.v}}throw new Error("No compiler setting named '"+e+"' exists!")}var l=/^\/\/\s?@(\w+)$/,c=/^\/\/\s?@(\w+):\s?(.+)$/,f={errors:[],noErrors:!1,showEmit:!1,showEmittedFile:"index.js",noStaticSemanticInfo:!1,emit:!1,noErrorValidation:!1};exports.twoslasher=function(e,t,i,u,p,h){var d=null!=u?u:require("typescript"),g=null!=p?p:require("lz-string"),m=e,v=function(e){switch(e){case"js":case"javascript":return"js";case"ts":case"typescript":return"ts";case"tsx":return"tsx";case"jsn":return"json"}throw new Error("Cannot handle the file extension:"+e)}(t),y="index."+v;o("\n\nLooking at code: \n```"+v+"\n"+e+"\n```\n");var x={strict:!0,target:d.ScriptTarget.ES2016,allowJs:!0};!function(e){if(e.includes("// @errors "))throw new Error("You have '@errors ' - you're missing the colon after errors");if(e.includes("// @filename "))throw new Error("You have '@filename ' - you're missing the colon after filename")}(e);var w=(e=function(e){return(e=e.replace(/¨D/g,"$")).replace(/¨T/g,"~")}(e)).split(/\r\n?|\n/g),E=n({},function(e){for(var t=n({},f),r=0;r<e.length;r++){var i=void 0;(i=l.exec(e[r]))?i[1]in t&&(t[i[1]]=!0,o("Setting options."+i[1]+" to true"),e.splice(r,1),r--):(i=c.exec(e[r]))&&i[1]in t&&(t[i[1]]=i[2],o("Setting options."+i[1]+" to "+i[2]),e.splice(r,1),r--)}return"errors"in t&&"string"==typeof t.errors&&(t.errors=t.errors.split(" ").map(Number),o("Setting options.error to ",t.errors)),t}(w),{},i),b=function(e,t,r){for(var i=n({},t),o=0;o<e.length;){var a=void 0;if(a=l.exec(e[o]))i[a[1]]=!0,s(a[1],"true",i,r);else{if(!(a=c.exec(e[o]))){o++;continue}if("filename"===a[1]){o++;continue}s(a[1],a[2],i,r)}e.splice(o,1)}return i}(w,x,d),S=null!=h?h:function(e){return r.createDefaultMapFromNodeModules(e)}(b),k=r.createSystem(S),j=r.createVirtualTypeScriptEnvironment(k,[],d,b),P=j.languageService;e=w.join("\n");var A=[],F=[],O=[],C=function(e,t){var r=e.split(/\r\n?|\n/g),n=e.includes("@filename: "+t)?"global.ts":t,i=[],o=[],a=r,s=Array.isArray(a),l=0;for(a=s?a:a[Symbol.iterator]();;){var c;if(s){if(l>=a.length)break;c=a[l++]}else{if((l=a.next()).done)break;c=l.value}var f=c;f.includes("// @filename: ")?(o.push([n,i]),n=f.split("// @filename: ")[1].trim(),i=[]):i.push(f)}return o.push([n,i]),o.filter((function(e){return e[1].length>0&&(e[1].length>1||""!==e[1][0])}))}(e,y),q=C.map((function(e){return e[0]})),T=function(){var e,t;if(I){if($>=L.length)return"break";D=L[$++]}else{if(($=L.next()).done)return"break";D=$.value}var r=D[0],n=D[1],i=n.join("\n");j.createFile(r,i);var o=a(n);(e=O).push.apply(e,o.highlights);var s=o.queries.map((function(e,t){var n,i,o=j.getSourceFile(r),a=d.getPositionOfLineAndCharacter(o,e.line,e.offset);switch(e.kind){case"query":var s=P.getQuickInfoAtPosition(r,a),l=P.getDefinitionAtPosition(r,a),c="Could not get LSP result: "+[(n=j.getSourceFile(r).text)[(i=a)-3],n[i-2],n[i-1],">",n[i],"<",n[i+1],n[i+2],n[i+3]].filter(Boolean).join(""),f=void 0;return s&&l&&s.displayParts&&(c=s.displayParts.map((function(e){return e.text})).join(""),f=s.documentation?s.documentation.map((function(e){return e.text})).join("<br/>"):void 0),{kind:"query",text:c,docs:f,line:e.line-t,offset:e.offset,file:r};case"completion":var u=P.getCompletionsAtPosition(r,a-1,{});if(!u)throw new Error("Twoslash: The ^| query at line "+e.line+" in "+r+" did not return any completions");var p=function(e,t){e=String(e),t=Number(t)>>>0;var r=e.slice(0,t+1).search(/\S+$/),n=e.slice(t).search(/\s/);return n<0?{word:e.slice(r),startPos:r}:{word:e.slice(r,n+t),startPos:r}}(o.text,a-1),h=o.text.slice(p.startPos,a).split(".").pop()||"";return{kind:"completions",completions:u.entries,completionPrefix:h,line:e.line-t,offset:e.offset,file:r}}}));(t=A).push.apply(t,s);var l=n.join("\n");j.updateFile(r,l)},L=C,I=Array.isArray(L),$=0;for(L=I?L:L[Symbol.iterator]();;){var D;if("break"===T())break}var N=e.split(/\r\n?|\n/g);a(N),e=N.join("\n"),E.emit&&q.forEach((function(e){P.getEmitOutput(e).outputFiles.forEach((function(e){k.writeFile(e.name,e.text)}))}));var M=[],Q=[];q.forEach((function(t){E.noErrors||(M.push.apply(M,P.getSemanticDiagnostics(t)),M.push.apply(M,P.getSyntacticDiagnostics(t)));var r=j.sys.readFile(t),n=j.getSourceFile(t);if(!n)throw new Error("No sourcefile found for "+t+" in twoslash");if(!E.showEmit){var i=-1==e.indexOf(r)?0:e.indexOf(r),o=e.slice(0,i).split("\n").length-1,a=E.noStaticSemanticInfo?[]:function(e,t){var r=[];return function n(i){e.forEachChild(i,(function(i){if(e.isIdentifier(i)){var o=i.getStart(t,!1);r.push({span:e.createTextSpan(o,i.end-o),text:i.getText(t)})}n(i)}))}(t),r}(d,n),s=Array.isArray(a),l=0;for(a=s?a:a[Symbol.iterator]();;){var c;if(s){if(l>=a.length)break;c=a[l++]}else{if((l=a.next()).done)break;c=l.value}var f=c,u=f.span,p=P.getQuickInfoAtPosition(t,u.start);if(p&&p.displayParts){var h=p.displayParts.map((function(e){return e.text})).join(""),g=f.text,m=p.documentation?p.documentation.map((function(e){return e.text})).join("\n"):void 0,v=u.start+i,y=d.createSourceFile("_.ts",e,d.ScriptTarget.ES2015),x=d.getLineAndCharacterOfPosition(y,v);Q.push({text:h,docs:m,start:v,length:u.length,line:x.line,character:x.character,targetString:g})}}A.filter((function(e){return e.file===t})).forEach((function(e){var t=d.getPositionOfLineAndCharacter(n,e.line,e.offset)+i;switch(e.kind){case"query":F.push({docs:e.docs,kind:"query",start:t+i,length:e.text.length,text:e.text,offset:e.offset,line:e.line+o+1});break;case"completions":F.push({completions:e.completions,kind:"completions",start:t+i,completionsPrefix:e.completionPrefix,length:1,offset:e.offset,line:e.line+o+1})}}))}}));var R=M.filter((function(e){return e.file&&q.includes(e.file.fileName)}));!E.noErrorValidation&&R.length&&function(e,t,r,n){var i=e.filter((function(e){return!t.errors.includes(e.code)})),o=i.map((function(e){return e.code})).join(" ");if(i.length){var a="// @errors: "+e.map((function(e){return e.code})).join(" "),s=t.errors.length?" - the annotation specified "+t.errors:"\n\nExpected:\n"+a,l=i.map((function(e){return"["+e.code+"] - "+("string"==typeof e.messageText?e.messageText:e.messageText.messageText)})).join("\n ");throw new Error("Errors were thrown in the sample, but not included in an errors tag: "+o+s+"\n\n "+l+"\n\n## Code\n\n'''"+r+"\n"+n+"\n'''")}}(R,E,t,m);var U=[],V=R,_=Array.isArray(V),Y=0;for(V=_?V:V[Symbol.iterator]();;){var z;if(_){if(Y>=V.length)break;z=V[Y++]}else{if((Y=V.next()).done)break;z=Y.value}var B=z,J=j.sys.readFile(B.file.fileName),G=e.indexOf(J),H=d.flattenDiagnosticMessageText(B.messageText,"\n").replace(/</g,"&lt;"),K="err-"+B.code+"-"+B.start+"-"+B.length,W=d.getLineAndCharacterOfPosition(B.file,B.start);U.push({category:B.category,code:B.code,length:B.length,start:B.start?B.start+G:void 0,line:W.line,character:W.character,renderedMessage:H,id:K})}if(E.showEmit){var X=E.showEmittedFile||y,Z=X.replace(".js","").replace(".d.ts","").replace(".map",""),ee=q.find((function(e){return e===Z+".ts"||e===Z+".tsx"}));if(!ee){var te=q.join(", ");throw new Error("Cannot find the corresponding source file for "+X+" "+E.showEmittedFile+" - in "+te)}var re=P.getEmitOutput(ee),ne=re.outputFiles.find((function(e){return e.name===E.showEmittedFile}));if(!ne){var ie=re.outputFiles.map((function(e){return e.name})).join(", ");throw new Error("Cannot find the file "+E.showEmittedFile+" - in "+ie)}e=ne.text,t=ne.name.split(".").pop(),O=[],A=[],Q=[]}var oe="https://www.typescriptlang.org/play/#code/"+g.compressToEncodedURIComponent(m),ae="// ---cut---\n";if(e.includes(ae)){var se=e.indexOf(ae)+ae.length,le=e.substr(0,se).split("\n").length-1;e=e.split(ae).pop(),Q.forEach((function(e){e.start-=se,e.line-=le})),Q=Q.filter((function(e){return e.start>-1})),U.forEach((function(e){e.start&&(e.start-=se),e.line&&(e.line-=le)})),U=U.filter((function(e){return e.start&&e.start>-1})),O.forEach((function(e){e.position-=se,e.line-=le})),O=O.filter((function(e){return e.position>-1})),F.forEach((function(e){return e.line-=le})),F=F.filter((function(e){return e.line>-1}))}return{code:e,extension:t,highlights:O,queries:F,staticQuickInfos:Q,errors:U,playgroundURL:oe}};
//# sourceMappingURL=twoslash.cjs.production.min.js.map

@@ -420,27 +420,4 @@ import debug from 'debug';

var queries = [];
var highlights = []; // TODO: This doesn't handle a single file with a name
var fileContent = code.split("// @filename: ");
var noFilepaths = fileContent.length === 1;
var makeDefault = [defaultFileName, code.split(/\r\n?|\n/g)];
var makeMultiFile = function makeMultiFile(filenameSplit) {
var _filenameSplit$split = filenameSplit.split(/\r\n?|\n/g),
filename = _filenameSplit$split[0],
content = _filenameSplit$split.slice(1);
var firstLine = "// @filename: " + filename;
return [filename, [firstLine].concat(content)];
};
/**
* Oof, some hard to grok code in this section. To ensure _one_ code path for both
* default and multi-file code samples it's all coerced into an array of
* [name, lines_of_code] basically for each set.
*/
var unfilteredNameContent = noFilepaths ? [makeDefault] : fileContent.map(makeMultiFile);
var nameContent = unfilteredNameContent.filter(function (n) {
return n[0].length;
});
var highlights = [];
var nameContent = splitTwoslashCodeInfoFiles(code, defaultFileName);
/** All of the referenced files in the markup */

@@ -553,5 +530,8 @@

if (handbookOptions.emit) {
var _env$languageService$;
(_env$languageService$ = env.languageService.getProgram()) === null || _env$languageService$ === void 0 ? void 0 : _env$languageService$.emit();
filenames.forEach(function (f) {
var output = ls.getEmitOutput(f);
output.outputFiles.forEach(function (output) {
system.writeFile(output.name, output.text);
});
});
} // Code should now be safe to compile, so we're going to split it into different files

@@ -803,3 +783,42 @@

var splitTwoslashCodeInfoFiles = function splitTwoslashCodeInfoFiles(code, defaultFileName) {
var lines = code.split(/\r\n?|\n/g);
var nameForFile = code.includes("@filename: " + defaultFileName) ? "global.ts" : defaultFileName;
var currentFileContent = [];
var fileMap = [];
for (var _iterator5 = lines, _isArray5 = Array.isArray(_iterator5), _i8 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) {
var _ref5;
if (_isArray5) {
if (_i8 >= _iterator5.length) break;
_ref5 = _iterator5[_i8++];
} else {
_i8 = _iterator5.next();
if (_i8.done) break;
_ref5 = _i8.value;
}
var line = _ref5;
if (line.includes("// @filename: ")) {
fileMap.push([nameForFile, currentFileContent]);
nameForFile = line.split("// @filename: ")[1].trim();
currentFileContent = [];
} else {
currentFileContent.push(line);
}
}
fileMap.push([nameForFile, currentFileContent]); // Basically, strip these:
// ["index.ts", []]
// ["index.ts", [""]]
var nameContent = fileMap.filter(function (n) {
return n[1].length > 0 && (n[1].length > 1 || n[1][0] !== "");
});
return nameContent;
};
export { twoslasher };
//# sourceMappingURL=twoslash.esm.js.map
{
"name": "@typescript/twoslash",
"version": "0.5.0",
"version": "0.5.1",
"license": "MIT",

@@ -29,3 +29,3 @@ "author": "TypeScript team",

"tslib": "^1.10.0",
"typescript": "3.9.2"
"typescript": "*"
},

@@ -32,0 +32,0 @@ "jest": {

@@ -106,3 +106,3 @@ # TypeScript TwoSlash

/**
* When mixed with showEmit, lets you choose the file to present instead of the source - defaults to index.js which
* Must be used with showEmit, lets you choose the file to present instead of the source - defaults to index.js which
* means when you just use `showEmit` above it shows the transpiled JS.

@@ -109,0 +109,0 @@ */

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc