Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@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 2.2.0 to 3.0.0

6

CHANGELOG.md

@@ -0,1 +1,5 @@

## 3.0.0
Changes the format of the `highlights` response from twoslash. It's very likely that no-one is using this, because none of the shiki infra is - but it's better to get this correct.
## 2.2.0

@@ -5,3 +9,3 @@

Prior to this change, any `// @something` which wasn't already in `ExampleOptions` would be passed to the TS compiler (maybe causing an exception if it's not a compiler option.) Now you can tell twoslash "I want to do something with this tag, let it through"
Prior to this change, any `// @something` which wasn't already in `ExampleOptions` would be passed to the TS compiler (maybe causing an exception if it's not a compiler option.) Now you can tell twoslash "I want to do something with this tag, let it through"

@@ -8,0 +12,0 @@ The use-case I'm thinking of is that shiki-twoslash may want to have it's own commands which sit on-top of twoslash, but I want to make sure that the internals of twoslash are all aligned correctly (think `--cut--` and the complex multi-file LSP logic) under the hood.

14

dist/index.d.ts

@@ -45,9 +45,15 @@ declare type LZ = typeof import("lz-string");

extension: string;
/** Sample requests to highlight a particular part of the code */
/** Requests to highlight a particular part of the code */
highlights: {
kind: "highlight";
position: number;
/** The index of the text in the file */
start: number;
/** What line is the highlighted identifier on? */
line: number;
/** At what index in the line does the caret represent */
offset: number;
/** The text of the token which is highlighted */
text?: string;
/** The length of the token */
length: number;
description: string;
line: number;
}[];

@@ -54,0 +60,0 @@ /** An array of LSP responses identifiers in the sample */

@@ -385,10 +385,10 @@ 'use strict';

var length = line.lastIndexOf("^") - _start + 1;
var position = contentOffset + _start;
var description = highlightMatch[1] ? highlightMatch[1].trim() : "";
highlights.push({
kind: "highlight",
position: position,
offset: _start + contentOffset,
length: length,
description: description,
line: _i
text: description,
line: _i + removedLines - 1,
start: _start
});

@@ -973,7 +973,7 @@ stripLine("having a highlight");

highlights.forEach(function (highlight) {
highlight.position -= cutIndex;
highlight.start -= cutIndex;
highlight.line -= lineOffset;
});
highlights = highlights.filter(function (e) {
return e.position > -1;
return e.start > -1;
});

@@ -980,0 +980,0 @@ queries.forEach(function (q) {

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

"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@typescript/vfs");function t(){return(t=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}function n(e){return(n=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function r(e,t){return(r=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function i(e,t,n){return(i=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}()?Reflect.construct:function(e,t,n){var i=[null];i.push.apply(i,t);var o=new(Function.bind.apply(e,i));return n&&r(o,n.prototype),o}).apply(null,arguments)}function o(e){var t="function"==typeof Map?new Map:void 0;return(o=function(e){if(null===e||-1===Function.toString.call(e).indexOf("[native code]"))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,o)}function o(){return i(e,arguments,n(this).constructor)}return o.prototype=Object.create(e.prototype,{constructor:{value:o,enumerable:!1,writable:!0,configurable:!0}}),r(o,e)})(e)}function s(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function a(e,t){var n;if("undefined"==typeof Symbol||null==e[Symbol.iterator]){if(Array.isArray(e)||(n=function(e,t){if(e){if("string"==typeof e)return s(e,void 0);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?s(e,void 0):void 0}}(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var r=0;return function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(n=e[Symbol.iterator]()).next.bind(n)}function c(e,t){switch(t){case"number":return+e;case"string":return e;case"boolean":return"true"===e.toLowerCase()||0===e.length}throw new p("Unknown primitive value in compiler flag","The only recognized primitives are number, string and boolean. Got "+t+" with "+e+".","This is likely a typo.")}var l=!1;try{l="undefined"!=typeof localStorage}catch(e){}var u="undefined"!=typeof process,f=l&&localStorage.getItem("DEBUG")||u&&process.env.DEBUG?console.log:function(e){return""},p=function(e){var t,n;function i(t,n,r,i){var o,s="\n## "+t+"\n\n"+n+"\n";return r&&(s+="\n"+r),i&&(s+="\n"+i),(o=e.call(this,s)||this).title=void 0,o.description=void 0,o.recommendation=void 0,o.code=void 0,o.title=t,o.description=n,o.recommendation=r,o.code=i,o}return n=e,(t=i).prototype=Object.create(n.prototype),t.prototype.constructor=t,r(t,n),i}(o(Error));function h(e){for(var t=[],n=[],r=0,i=0,o=0,s=function(s){var c=e[s],l=function(){i=r,r+=c.length+1},u=function(t){f("Removing line "+s+" for "+t),o++,e.splice(s,1),s--};if(c.includes("//")){var p=/^\s*\/\/\s*\^+( .+)?$/.exec(c),h=/^\s*\/\/\s*\^\?\s*$/.exec(c),d=/^\s*\/\/ prettier-ignore$/.exec(c),v=/^\s*\/\/\s*\^\|$/.exec(c);if(null!==h){var m=c.indexOf("^");n.push({kind:"query",offset:m,text:void 0,docs:void 0,line:s+o-1}),u("having a query")}else if(null!==p){var g=c.indexOf("^"),y=c.lastIndexOf("^")-g+1,w=i+g,x=p[1]?p[1].trim():"";t.push({kind:"highlight",position:w,length:y,description:x,line:s}),u("having a highlight")}else if(null!==d)u("being a prettier ignore");else if(null!==v){var b=c.indexOf("^");n.push({kind:"completion",offset:b,text:void 0,docs:void 0,line:s+o-1}),u("having a completion query")}else l()}else l();a=s},a=0;a<e.length;a++)s(a);return{highlights:t,queries:n}}function d(e,t,n){var r=n.get(t.toLowerCase());if(f("Get "+e+" mapped option: "+t+" => "+r),void 0===r){var i=Array.from(n.keys());throw new p("Invalid inline compiler value","Got "+t+" for "+e+" but it is not a supported value by the TS compiler.","Allowed values: "+i.join(","))}return r}function v(e,t,n,r){f("Setting "+e+" to "+t);for(var i,o=function(){var r=i.value;if(r.name.toLowerCase()===e.toLowerCase()){switch(r.type){case"number":case"string":case"boolean":n[r.name]=c(t,r.type);break;case"list":var o=r.element.type,s=t.split(",");n[r.name]=s.map("string"==typeof o?function(e){return c(e,o)}:function(e){return d(r.name,e,o)});break;default:n[r.name]=d(r.name,t,r.type)}return{v:void 0}}},s=a(r.optionDeclarations);!(i=s()).done;){var l=o();if("object"==typeof l)return l.v}throw new p("Invalid inline compiler flag","There isn't a TypeScript compiler flag called '"+e+"'.","This is likely a typo, you can check all the compiler flags in the TSConfig reference, or check the additional Twoslash flags in the npm page for @typescript/twoslash.")}var m=/^\/\/\s?@(\w+)$/,g=/^\/\/\s?@(\w+):\s?(.+)$/,y={errors:[],noErrors:!1,showEmit:!1,showEmittedFile:void 0,noStaticSemanticInfo:!1,emit:!1,noErrorValidation:!1};exports.TwoslashError=p,exports.twoslasher=function(n,r,i){var o,s,c;void 0===i&&(i={});var l=null!=(o=i.tsModule)?o:require("typescript"),u=null!=(s=i.lzstringModule)?s:require("lz-string"),d=n,w=function(e){var t={js:"js",javascript:"js",ts:"ts",typescript:"ts",tsx:"tsx",jsx:"jsx",json:"json",jsn:"json"};if(t[e])return t[e];throw new p("Unknown TypeScript extension given to Twoslash","Received "+e+" but Twoslash only accepts: "+Object.keys(t)+" ","")}(r),x="index."+w;f("\n\nLooking at code: \n```"+w+"\n"+n+"\n```\n");var b=t({strict:!0,target:l.ScriptTarget.ES2016,allowJs:!0},null!=(c=i.defaultCompilerOptions)?c:{});!function(e){if(e.includes("// @errors "))throw new p("You have '// @errors ' (with a space)","You want '// @errors: ' (with a colon)","This is a pretty common typo");if(e.includes("// @filename "))throw new p("You have '// @filename ' (with a space)","You want '// @filename: ' (with a colon)","This is a pretty common typo")}(n);var j=(n=function(e){return(e=e.replace(/¨D/g,"$")).replace(/¨T/g,"~")}(n)).split(/\r\n?|\n/g),E=i.customTags?function(e,t){for(var n=[],r=0;r<e.length;){var i;(i=g.exec(e[r]))&&t.includes(i[1])&&(n.push({name:i[1],line:r,annotation:e[r].split("@"+i[1]+": ")[1]}),e.splice(r,1)),r++}return n}(j,i.customTags):[],S=t({},function(e){for(var n=t({},y),r=0;r<e.length;r++){var i=void 0;(i=m.exec(e[r]))?i[1]in n&&(n[i[1]]=!0,f("Setting options."+i[1]+" to true"),e.splice(r,1),r--):(i=g.exec(e[r]))&&i[1]in n&&(n[i[1]]=i[2],f("Setting options."+i[1]+" to "+i[2]),e.splice(r,1),r--)}return"errors"in n&&"string"==typeof n.errors&&(n.errors=n.errors.split(" ").map(Number),f("Setting options.error to ",n.errors)),n}(j),i.defaultOptions),T=function(e,n,r){for(var i=t({},n),o=0;o<e.length;){var s=void 0;if(s=m.exec(e[o]))i[s[1]]=!0,v(s[1],"true",i,r);else{if(!(s=g.exec(e[o]))){o++;continue}if("filename"===s[1]){o++;continue}v(s[1],s[2],i,r)}e.splice(o,1)}return i}(j,b,l);S.showEmittedFile||(S.showEmittedFile=T.jsx&&T.jsx===l.JsxEmit.Preserve?"index.jsx":"index.js");var k=function(){var e=require("path");return(i.vfsRoot||process.cwd()).split(e.sep).join(e.posix.sep)},O=!!i.fsMap,F=O&&i.fsMap?i.fsMap:new Map,P=O?e.createSystem(F):e.createFSBackedSystem(F,k(),l),q=O?"/":k()+"/",C=e.createVirtualTypeScriptEnvironment(P,[],l,T,i.customTransformers),A=C.languageService;n=j.join("\n");for(var I,M=[],L=[],_=[],R=function(e,t,n){for(var r,i=e.split(/\r\n?|\n/g),o=e.includes("@filename: "+t)?"global.ts":t,s=[],c=[],l=a(i);!(r=l()).done;){var u=r.value;u.includes("// @filename: ")?(c.push([n+o,s]),o=u.split("// @filename: ")[1].trim(),s=[]):s.push(u)}return c.push([n+o,s]),c.filter((function(e){return e[1].length>0&&(e[1].length>1||""!==e[1][0])}))}(n,x,q),$=["js","jsx","ts","tsx"],D=R.map((function(e){return e[0]})),U=function(){var e=I.value,t=e[0],n=e[1],r=t.split(".").pop()||"",i=T.resolveJsonModule&&"json"===r;if(!$.includes(r)&&!i)return"continue";var o=n.join("\n");C.createFile(t,o);var s=h(n);_=_.concat(s.highlights);var a=s.queries.map((function(e,n){var r=C.getSourceFile(t),i=l.getPositionOfLineAndCharacter(r,e.line,e.offset);switch(e.kind){case"query":var o=A.getQuickInfoAtPosition(t,i);if(!o||!o.displayParts)throw new p("Invalid QuickInfo query","The request on line "+e.line+" in "+t+" for quickinfo via ^? returned no from the compiler.","This is likely that the x positioning is off.");return{kind:"query",text:o.displayParts.map((function(e){return e.text})).join(""),docs:o.documentation?o.documentation.map((function(e){return e.text})).join("<br/>"):void 0,line:e.line-n,offset:e.offset,file:t};case"completion":var s=A.getCompletionsAtPosition(t,i-1,{});if(!s&&!S.noErrorValidation)throw new p("Invalid completion query","The request on line "+e.line+" in "+t+" for completions via ^| returned no completions from the compiler.","This is likely that the positioning is off.");var a=function(e,t){e=String(e),t=Number(t)>>>0;var n=e.slice(0,t+1).search(/\S+$/),r=e.slice(t).search(/\s/);return r<0?{word:e.slice(n),startPos:n}:{word:e.slice(n,r+t),startPos:n}}(r.text,i-1),c=r.text.slice(a.startPos,i).split(".").pop()||"";return{kind:"completions",completions:(null==s?void 0:s.entries)||[],completionPrefix:c,line:e.line-n,offset:e.offset,file:t}}}));M=M.concat(a);var c=n.join("\n");C.updateFile(t,c)},N=a(R);!(I=N()).done;)U();var B=n.split(/\r\n?|\n/g);h(B),n=B.join("\n"),S.emit&&D.forEach((function(e){var t=e.split(".").pop()||"";$.includes(t)&&A.getEmitOutput(e).outputFiles.forEach((function(e){P.writeFile(e.name,e.text)}))}));var G=[],V=[];D.forEach((function(e){var t=e.split(".").pop()||"";if($.includes(t)){S.noErrors||(G=G.concat(A.getSemanticDiagnostics(e),A.getSyntacticDiagnostics(e)));var r=C.sys.readFile(e),i=C.getSourceFile(e);if(!i)throw new p("Could not find a TypeScript sourcefile for '"+e+"' in the Twoslash vfs","It's a little hard to provide useful advice on this error. Maybe you imported something which the compiler doesn't think is a source file?","");if(!S.showEmit){for(var o,s=-1==n.indexOf(r)?0:n.indexOf(r),c=n.slice(0,s).split("\n").length-1,u=a(S.noStaticSemanticInfo?[]:function(e,t){var n=[];return function r(i){e.forEachChild(i,(function(i){if(e.isIdentifier(i)){var o=i.getStart(t,!1);n.push({span:e.createTextSpan(o,i.end-o),text:i.getText(t)})}r(i)}))}(t),n}(l,i));!(o=u()).done;){var f=o.value,h=f.span,d=A.getQuickInfoAtPosition(e,h.start);if(d&&d.displayParts){var v=d.displayParts.map((function(e){return e.text})).join(""),m=f.text,g=d.documentation?d.documentation.map((function(e){return e.text})).join("\n"):void 0,y=h.start+s,w=l.createSourceFile("_.ts",n,l.ScriptTarget.ES2015),x=l.getLineAndCharacterOfPosition(w,y);V.push({text:v,docs:g,start:y,length:h.length,line:x.line,character:x.character,targetString:m})}}M.filter((function(t){return t.file===e})).forEach((function(e){var t=l.getPositionOfLineAndCharacter(i,e.line,e.offset)+s;switch(e.kind){case"query":L.push({docs:e.docs,kind:"query",start:t+s,length:e.text.length,text:e.text,offset:e.offset,line:e.line+c+1});break;case"completions":L.push({completions:e.completions,kind:"completions",start:t+s,completionsPrefix:e.completionPrefix,length:1,offset:e.offset,line:e.line+c+1})}}))}}}));var Q=G.filter((function(e){return e.file&&D.includes(e.file.fileName)}));!S.noErrorValidation&&Q.length&&function(e,t,n,r,i){var o=e.filter((function(e){return!t.errors.includes(e.code)})),s=Array.from(new Set(o.map((function(e){return e.code})))).join(" ");if(o.length){var a=new Set(e.map((function(e){return e.code}))),c="// @errors: "+Array.from(a).join(" "),l=t.errors.length?"\nThe existing annotation specified "+t.errors.join(" "):"\nExpected: "+c,u={},f=[];o.forEach((function(e){var t,n=(null==(t=e.file)?void 0:t.fileName)&&e.file.fileName.replace(i,"");if(n){var r=u[n];r?r.push(e):u[n]=[e]}else f.push(e)}));var h=function(e,t){return e+"\n "+t.map((function(e){return"["+e.code+"] "+e.start+" - "+("string"==typeof e.messageText?e.messageText:e.messageText.messageText)})).join("\n ")},d=[];f.length&&d.push(h("Ambient Errors",f)),Object.keys(u).forEach((function(e){d.push(h(e,u[e]))}));var v=d.join("\n\n"),m=new p("Errors were thrown in the sample, but not included in an errors tag","These errors were not marked as being expected: "+s+". "+l,"Compiler Errors:\n\n"+v);throw m.code="## Code\n\n'''"+n+"\n"+r+"\n'''",m}}(Q,S,r,d,q);for(var Y,z=[],J=a(Q);!(Y=J()).done;){var H=Y.value,K=C.sys.readFile(H.file.fileName),W=n.indexOf(K),X=l.flattenDiagnosticMessageText(H.messageText,"\n"),Z="err-"+H.code+"-"+H.start+"-"+H.length,ee=l.getLineAndCharacterOfPosition(H.file,H.start);z.push({category:H.category,code:H.code,length:H.length,start:H.start?H.start+W:void 0,line:ee.line,character:ee.character,renderedMessage:X,id:Z})}if(S.showEmit){var te=S.showEmittedFile||x,ne=q+te.replace(".jsx","").replace(".js","").replace(".d.ts","").replace(".map",""),re=D.find((function(e){return e===ne+".ts"||e===ne+".tsx"}));if(!re&&!T.outFile){var ie=D.join(", ");throw new p("Could not find source file to show the emit for","Cannot find the corresponding **source** file "+te+" for completions via ^| returned no quickinfo from the compiler.","Looked for: "+ne+" in the vfs - which contains: "+ie)}T.outFile&&(re=D[0]);var oe=A.getEmitOutput(re),se=oe.outputFiles.find((function(e){return e.name===q+S.showEmittedFile||e.name===S.showEmittedFile}));if(!se){var ae=oe.outputFiles.map((function(e){return e.name})).join(", ");throw new p("Cannot find the output file in the Twoslash VFS","Looking for "+S.showEmittedFile+" in the Twoslash vfs after compiling",'Looked for" '+(q+S.showEmittedFile)+" in the vfs - which contains "+ae+".")}n=se.text,r=se.name.split(".").pop(),_=[],M=[],V=[]}var ce="https://www.typescriptlang.org/play/#code/"+u.compressToEncodedURIComponent(d),le="// ---cut---\n";if(n.includes(le)){var ue=n.indexOf(le)+le.length,fe=n.substr(0,ue).split("\n").length-1;n=n.split(le).pop(),V.forEach((function(e){e.start-=ue,e.line-=fe})),V=V.filter((function(e){return e.start>-1})),z.forEach((function(e){e.start&&(e.start-=ue),e.line&&(e.line-=fe)})),z=z.filter((function(e){return e.start&&e.start>-1})),_.forEach((function(e){e.position-=ue,e.line-=fe})),_=_.filter((function(e){return e.position>-1})),L.forEach((function(e){return e.line-=fe})),L=L.filter((function(e){return e.line>-1})),E.forEach((function(e){return e.line-=fe})),E=E.filter((function(e){return e.line>-1}))}return{code:n,extension:r,highlights:_,queries:L,staticQuickInfos:V,errors:z,playgroundURL:ce,tags:E}};
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@typescript/vfs");function t(){return(t=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}function n(e){return(n=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function r(e,t){return(r=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function i(e,t,n){return(i=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}()?Reflect.construct:function(e,t,n){var i=[null];i.push.apply(i,t);var o=new(Function.bind.apply(e,i));return n&&r(o,n.prototype),o}).apply(null,arguments)}function o(e){var t="function"==typeof Map?new Map:void 0;return(o=function(e){if(null===e||-1===Function.toString.call(e).indexOf("[native code]"))return e;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,o)}function o(){return i(e,arguments,n(this).constructor)}return o.prototype=Object.create(e.prototype,{constructor:{value:o,enumerable:!1,writable:!0,configurable:!0}}),r(o,e)})(e)}function s(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function a(e,t){var n;if("undefined"==typeof Symbol||null==e[Symbol.iterator]){if(Array.isArray(e)||(n=function(e,t){if(e){if("string"==typeof e)return s(e,void 0);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?s(e,void 0):void 0}}(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var r=0;return function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(n=e[Symbol.iterator]()).next.bind(n)}function c(e,t){switch(t){case"number":return+e;case"string":return e;case"boolean":return"true"===e.toLowerCase()||0===e.length}throw new p("Unknown primitive value in compiler flag","The only recognized primitives are number, string and boolean. Got "+t+" with "+e+".","This is likely a typo.")}var l=!1;try{l="undefined"!=typeof localStorage}catch(e){}var u="undefined"!=typeof process,f=l&&localStorage.getItem("DEBUG")||u&&process.env.DEBUG?console.log:function(e){return""},p=function(e){var t,n;function i(t,n,r,i){var o,s="\n## "+t+"\n\n"+n+"\n";return r&&(s+="\n"+r),i&&(s+="\n"+i),(o=e.call(this,s)||this).title=void 0,o.description=void 0,o.recommendation=void 0,o.code=void 0,o.title=t,o.description=n,o.recommendation=r,o.code=i,o}return n=e,(t=i).prototype=Object.create(n.prototype),t.prototype.constructor=t,r(t,n),i}(o(Error));function h(e){for(var t=[],n=[],r=0,i=0,o=0,s=function(s){var c=e[s],l=function(){i=r,r+=c.length+1},u=function(t){f("Removing line "+s+" for "+t),o++,e.splice(s,1),s--};if(c.includes("//")){var p=/^\s*\/\/\s*\^+( .+)?$/.exec(c),h=/^\s*\/\/\s*\^\?\s*$/.exec(c),d=/^\s*\/\/ prettier-ignore$/.exec(c),v=/^\s*\/\/\s*\^\|$/.exec(c);if(null!==h){var m=c.indexOf("^");n.push({kind:"query",offset:m,text:void 0,docs:void 0,line:s+o-1}),u("having a query")}else if(null!==p){var g=c.indexOf("^"),y=c.lastIndexOf("^")-g+1,w=p[1]?p[1].trim():"";t.push({kind:"highlight",offset:g+i,length:y,text:w,line:s+o-1,start:g}),u("having a highlight")}else if(null!==d)u("being a prettier ignore");else if(null!==v){var x=c.indexOf("^");n.push({kind:"completion",offset:x,text:void 0,docs:void 0,line:s+o-1}),u("having a completion query")}else l()}else l();a=s},a=0;a<e.length;a++)s(a);return{highlights:t,queries:n}}function d(e,t,n){var r=n.get(t.toLowerCase());if(f("Get "+e+" mapped option: "+t+" => "+r),void 0===r){var i=Array.from(n.keys());throw new p("Invalid inline compiler value","Got "+t+" for "+e+" but it is not a supported value by the TS compiler.","Allowed values: "+i.join(","))}return r}function v(e,t,n,r){f("Setting "+e+" to "+t);for(var i,o=function(){var r=i.value;if(r.name.toLowerCase()===e.toLowerCase()){switch(r.type){case"number":case"string":case"boolean":n[r.name]=c(t,r.type);break;case"list":var o=r.element.type,s=t.split(",");n[r.name]=s.map("string"==typeof o?function(e){return c(e,o)}:function(e){return d(r.name,e,o)});break;default:n[r.name]=d(r.name,t,r.type)}return{v:void 0}}},s=a(r.optionDeclarations);!(i=s()).done;){var l=o();if("object"==typeof l)return l.v}throw new p("Invalid inline compiler flag","There isn't a TypeScript compiler flag called '"+e+"'.","This is likely a typo, you can check all the compiler flags in the TSConfig reference, or check the additional Twoslash flags in the npm page for @typescript/twoslash.")}var m=/^\/\/\s?@(\w+)$/,g=/^\/\/\s?@(\w+):\s?(.+)$/,y={errors:[],noErrors:!1,showEmit:!1,showEmittedFile:void 0,noStaticSemanticInfo:!1,emit:!1,noErrorValidation:!1};exports.TwoslashError=p,exports.twoslasher=function(n,r,i){var o,s,c;void 0===i&&(i={});var l=null!=(o=i.tsModule)?o:require("typescript"),u=null!=(s=i.lzstringModule)?s:require("lz-string"),d=n,w=function(e){var t={js:"js",javascript:"js",ts:"ts",typescript:"ts",tsx:"tsx",jsx:"jsx",json:"json",jsn:"json"};if(t[e])return t[e];throw new p("Unknown TypeScript extension given to Twoslash","Received "+e+" but Twoslash only accepts: "+Object.keys(t)+" ","")}(r),x="index."+w;f("\n\nLooking at code: \n```"+w+"\n"+n+"\n```\n");var b=t({strict:!0,target:l.ScriptTarget.ES2016,allowJs:!0},null!=(c=i.defaultCompilerOptions)?c:{});!function(e){if(e.includes("// @errors "))throw new p("You have '// @errors ' (with a space)","You want '// @errors: ' (with a colon)","This is a pretty common typo");if(e.includes("// @filename "))throw new p("You have '// @filename ' (with a space)","You want '// @filename: ' (with a colon)","This is a pretty common typo")}(n);var j=(n=function(e){return(e=e.replace(/¨D/g,"$")).replace(/¨T/g,"~")}(n)).split(/\r\n?|\n/g),E=i.customTags?function(e,t){for(var n=[],r=0;r<e.length;){var i;(i=g.exec(e[r]))&&t.includes(i[1])&&(n.push({name:i[1],line:r,annotation:e[r].split("@"+i[1]+": ")[1]}),e.splice(r,1)),r++}return n}(j,i.customTags):[],S=t({},function(e){for(var n=t({},y),r=0;r<e.length;r++){var i=void 0;(i=m.exec(e[r]))?i[1]in n&&(n[i[1]]=!0,f("Setting options."+i[1]+" to true"),e.splice(r,1),r--):(i=g.exec(e[r]))&&i[1]in n&&(n[i[1]]=i[2],f("Setting options."+i[1]+" to "+i[2]),e.splice(r,1),r--)}return"errors"in n&&"string"==typeof n.errors&&(n.errors=n.errors.split(" ").map(Number),f("Setting options.error to ",n.errors)),n}(j),i.defaultOptions),T=function(e,n,r){for(var i=t({},n),o=0;o<e.length;){var s=void 0;if(s=m.exec(e[o]))i[s[1]]=!0,v(s[1],"true",i,r);else{if(!(s=g.exec(e[o]))){o++;continue}if("filename"===s[1]){o++;continue}v(s[1],s[2],i,r)}e.splice(o,1)}return i}(j,b,l);S.showEmittedFile||(S.showEmittedFile=T.jsx&&T.jsx===l.JsxEmit.Preserve?"index.jsx":"index.js");var k=function(){var e=require("path");return(i.vfsRoot||process.cwd()).split(e.sep).join(e.posix.sep)},O=!!i.fsMap,F=O&&i.fsMap?i.fsMap:new Map,P=O?e.createSystem(F):e.createFSBackedSystem(F,k(),l),q=O?"/":k()+"/",C=e.createVirtualTypeScriptEnvironment(P,[],l,T,i.customTransformers),A=C.languageService;n=j.join("\n");for(var I,M=[],L=[],_=[],R=function(e,t,n){for(var r,i=e.split(/\r\n?|\n/g),o=e.includes("@filename: "+t)?"global.ts":t,s=[],c=[],l=a(i);!(r=l()).done;){var u=r.value;u.includes("// @filename: ")?(c.push([n+o,s]),o=u.split("// @filename: ")[1].trim(),s=[]):s.push(u)}return c.push([n+o,s]),c.filter((function(e){return e[1].length>0&&(e[1].length>1||""!==e[1][0])}))}(n,x,q),$=["js","jsx","ts","tsx"],D=R.map((function(e){return e[0]})),U=function(){var e=I.value,t=e[0],n=e[1],r=t.split(".").pop()||"",i=T.resolveJsonModule&&"json"===r;if(!$.includes(r)&&!i)return"continue";var o=n.join("\n");C.createFile(t,o);var s=h(n);_=_.concat(s.highlights);var a=s.queries.map((function(e,n){var r=C.getSourceFile(t),i=l.getPositionOfLineAndCharacter(r,e.line,e.offset);switch(e.kind){case"query":var o=A.getQuickInfoAtPosition(t,i);if(!o||!o.displayParts)throw new p("Invalid QuickInfo query","The request on line "+e.line+" in "+t+" for quickinfo via ^? returned no from the compiler.","This is likely that the x positioning is off.");return{kind:"query",text:o.displayParts.map((function(e){return e.text})).join(""),docs:o.documentation?o.documentation.map((function(e){return e.text})).join("<br/>"):void 0,line:e.line-n,offset:e.offset,file:t};case"completion":var s=A.getCompletionsAtPosition(t,i-1,{});if(!s&&!S.noErrorValidation)throw new p("Invalid completion query","The request on line "+e.line+" in "+t+" for completions via ^| returned no completions from the compiler.","This is likely that the positioning is off.");var a=function(e,t){e=String(e),t=Number(t)>>>0;var n=e.slice(0,t+1).search(/\S+$/),r=e.slice(t).search(/\s/);return r<0?{word:e.slice(n),startPos:n}:{word:e.slice(n,r+t),startPos:n}}(r.text,i-1),c=r.text.slice(a.startPos,i).split(".").pop()||"";return{kind:"completions",completions:(null==s?void 0:s.entries)||[],completionPrefix:c,line:e.line-n,offset:e.offset,file:t}}}));M=M.concat(a);var c=n.join("\n");C.updateFile(t,c)},N=a(R);!(I=N()).done;)U();var B=n.split(/\r\n?|\n/g);h(B),n=B.join("\n"),S.emit&&D.forEach((function(e){var t=e.split(".").pop()||"";$.includes(t)&&A.getEmitOutput(e).outputFiles.forEach((function(e){P.writeFile(e.name,e.text)}))}));var G=[],V=[];D.forEach((function(e){var t=e.split(".").pop()||"";if($.includes(t)){S.noErrors||(G=G.concat(A.getSemanticDiagnostics(e),A.getSyntacticDiagnostics(e)));var r=C.sys.readFile(e),i=C.getSourceFile(e);if(!i)throw new p("Could not find a TypeScript sourcefile for '"+e+"' in the Twoslash vfs","It's a little hard to provide useful advice on this error. Maybe you imported something which the compiler doesn't think is a source file?","");if(!S.showEmit){for(var o,s=-1==n.indexOf(r)?0:n.indexOf(r),c=n.slice(0,s).split("\n").length-1,u=a(S.noStaticSemanticInfo?[]:function(e,t){var n=[];return function r(i){e.forEachChild(i,(function(i){if(e.isIdentifier(i)){var o=i.getStart(t,!1);n.push({span:e.createTextSpan(o,i.end-o),text:i.getText(t)})}r(i)}))}(t),n}(l,i));!(o=u()).done;){var f=o.value,h=f.span,d=A.getQuickInfoAtPosition(e,h.start);if(d&&d.displayParts){var v=d.displayParts.map((function(e){return e.text})).join(""),m=f.text,g=d.documentation?d.documentation.map((function(e){return e.text})).join("\n"):void 0,y=h.start+s,w=l.createSourceFile("_.ts",n,l.ScriptTarget.ES2015),x=l.getLineAndCharacterOfPosition(w,y);V.push({text:v,docs:g,start:y,length:h.length,line:x.line,character:x.character,targetString:m})}}M.filter((function(t){return t.file===e})).forEach((function(e){var t=l.getPositionOfLineAndCharacter(i,e.line,e.offset)+s;switch(e.kind){case"query":L.push({docs:e.docs,kind:"query",start:t+s,length:e.text.length,text:e.text,offset:e.offset,line:e.line+c+1});break;case"completions":L.push({completions:e.completions,kind:"completions",start:t+s,completionsPrefix:e.completionPrefix,length:1,offset:e.offset,line:e.line+c+1})}}))}}}));var Q=G.filter((function(e){return e.file&&D.includes(e.file.fileName)}));!S.noErrorValidation&&Q.length&&function(e,t,n,r,i){var o=e.filter((function(e){return!t.errors.includes(e.code)})),s=Array.from(new Set(o.map((function(e){return e.code})))).join(" ");if(o.length){var a=new Set(e.map((function(e){return e.code}))),c="// @errors: "+Array.from(a).join(" "),l=t.errors.length?"\nThe existing annotation specified "+t.errors.join(" "):"\nExpected: "+c,u={},f=[];o.forEach((function(e){var t,n=(null==(t=e.file)?void 0:t.fileName)&&e.file.fileName.replace(i,"");if(n){var r=u[n];r?r.push(e):u[n]=[e]}else f.push(e)}));var h=function(e,t){return e+"\n "+t.map((function(e){return"["+e.code+"] "+e.start+" - "+("string"==typeof e.messageText?e.messageText:e.messageText.messageText)})).join("\n ")},d=[];f.length&&d.push(h("Ambient Errors",f)),Object.keys(u).forEach((function(e){d.push(h(e,u[e]))}));var v=d.join("\n\n"),m=new p("Errors were thrown in the sample, but not included in an errors tag","These errors were not marked as being expected: "+s+". "+l,"Compiler Errors:\n\n"+v);throw m.code="## Code\n\n'''"+n+"\n"+r+"\n'''",m}}(Q,S,r,d,q);for(var Y,z=[],J=a(Q);!(Y=J()).done;){var H=Y.value,K=C.sys.readFile(H.file.fileName),W=n.indexOf(K),X=l.flattenDiagnosticMessageText(H.messageText,"\n"),Z="err-"+H.code+"-"+H.start+"-"+H.length,ee=l.getLineAndCharacterOfPosition(H.file,H.start);z.push({category:H.category,code:H.code,length:H.length,start:H.start?H.start+W:void 0,line:ee.line,character:ee.character,renderedMessage:X,id:Z})}if(S.showEmit){var te=S.showEmittedFile||x,ne=q+te.replace(".jsx","").replace(".js","").replace(".d.ts","").replace(".map",""),re=D.find((function(e){return e===ne+".ts"||e===ne+".tsx"}));if(!re&&!T.outFile){var ie=D.join(", ");throw new p("Could not find source file to show the emit for","Cannot find the corresponding **source** file "+te+" for completions via ^| returned no quickinfo from the compiler.","Looked for: "+ne+" in the vfs - which contains: "+ie)}T.outFile&&(re=D[0]);var oe=A.getEmitOutput(re),se=oe.outputFiles.find((function(e){return e.name===q+S.showEmittedFile||e.name===S.showEmittedFile}));if(!se){var ae=oe.outputFiles.map((function(e){return e.name})).join(", ");throw new p("Cannot find the output file in the Twoslash VFS","Looking for "+S.showEmittedFile+" in the Twoslash vfs after compiling",'Looked for" '+(q+S.showEmittedFile)+" in the vfs - which contains "+ae+".")}n=se.text,r=se.name.split(".").pop(),_=[],M=[],V=[]}var ce="https://www.typescriptlang.org/play/#code/"+u.compressToEncodedURIComponent(d),le="// ---cut---\n";if(n.includes(le)){var ue=n.indexOf(le)+le.length,fe=n.substr(0,ue).split("\n").length-1;n=n.split(le).pop(),V.forEach((function(e){e.start-=ue,e.line-=fe})),V=V.filter((function(e){return e.start>-1})),z.forEach((function(e){e.start&&(e.start-=ue),e.line&&(e.line-=fe)})),z=z.filter((function(e){return e.start&&e.start>-1})),_.forEach((function(e){e.start-=ue,e.line-=fe})),_=_.filter((function(e){return e.start>-1})),L.forEach((function(e){return e.line-=fe})),L=L.filter((function(e){return e.line>-1})),E.forEach((function(e){return e.line-=fe})),E=E.filter((function(e){return e.line>-1}))}return{code:n,extension:r,highlights:_,queries:L,staticQuickInfos:V,errors:z,playgroundURL:ce,tags:E}};
//# sourceMappingURL=twoslash.cjs.production.min.js.map

@@ -381,10 +381,10 @@ import { createSystem, createFSBackedSystem, createVirtualTypeScriptEnvironment } from '@typescript/vfs';

var length = line.lastIndexOf("^") - _start + 1;
var position = contentOffset + _start;
var description = highlightMatch[1] ? highlightMatch[1].trim() : "";
highlights.push({
kind: "highlight",
position: position,
offset: _start + contentOffset,
length: length,
description: description,
line: _i
text: description,
line: _i + removedLines - 1,
start: _start
});

@@ -969,7 +969,7 @@ stripLine("having a highlight");

highlights.forEach(function (highlight) {
highlight.position -= cutIndex;
highlight.start -= cutIndex;
highlight.line -= lineOffset;
});
highlights = highlights.filter(function (e) {
return e.position > -1;
return e.start > -1;
});

@@ -976,0 +976,0 @@ queries.forEach(function (q) {

@@ -391,10 +391,10 @@

var length = line.lastIndexOf("^") - _start + 1;
var position = contentOffset + _start;
var description = highlightMatch[1] ? highlightMatch[1].trim() : "";
highlights.push({
kind: "highlight",
position: position,
offset: _start + contentOffset,
length: length,
description: description,
line: _i
text: description,
line: _i + removedLines - 1,
start: _start
});

@@ -979,7 +979,7 @@ stripLine("having a highlight");

highlights.forEach(function (highlight) {
highlight.position -= cutIndex;
highlight.start -= cutIndex;
highlight.line -= lineOffset;
});
highlights = highlights.filter(function (e) {
return e.position > -1;
return e.start > -1;
});

@@ -986,0 +986,0 @@ queries.forEach(function (q) {

{
"name": "@typescript/twoslash",
"version": "2.2.0",
"version": "3.0.0",
"license": "MIT",

@@ -5,0 +5,0 @@ "author": "TypeScript team",

@@ -571,6 +571,7 @@ # TypeScript TwoSlash

> "kind": "highlight",
> "position": 134,
> "offset": 134,
> "length": 10,
> "description": "",
> "line": 5
> "text": "",
> "line": 4,
> "start": 18
> }

@@ -836,9 +837,15 @@ > ],

extension: string
/** Sample requests to highlight a particular part of the code */
/** Requests to highlight a particular part of the code */
highlights: {
kind: "highlight"
position: number
/** The index of the text in the file */
start: number
/** What line is the highlighted identifier on? */
line: number
/** At what index in the line does the caret represent */
offset: number
/** The text of the token which is highlighted */
text?: string
/** The length of the token */
length: number
description: string
line: number
}[]

@@ -845,0 +852,0 @@ /** An array of LSP responses identifiers in the sample */

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